-----------------------------------------
회원관리+성적처리 프로젝트 (Struts, iBatis 버전)
1. 성적 테이블, 회원 테이블 조인. FK 설정 필요.
2. 회원(회원번호-PK, 이름, 전화번호) -> 이전과 동일
3. 성적 (성적번호-PK, 회원번호-FK, 국어, 영어, 수학)
-> 이름 대신 회원번호-FK 설정
4. 회원에 회원 등록을 먼저하고, 성적에 성적 입력을 나중에 한다.
5. 입력, 출력, 수정, 삭제 기능을 모두 구현할 것.
----------------------------------------
CREATE TABLE member2 (
mid NUMBER --PK
,name VARCHAR2(10)
,tel VARCHAR2(20)
);
ALTER TABLE member2
ADD CONSTRAINT member2_mid_pk PRIMARY KEY(mid);
CREATE SEQUENCE member2Seq;
//회원 테이블에 자료 입력. INSERT 쿼리.
//쿼리문의 끝에 ; 표시하지 말것.
INSERT INTO member2 (mid, name, tel) VALUES (member2Seq.nextval, 'hong', '111-1111');
INSERT INTO member2 (mid, name, tel) VALUES (member2Seq.nextval, 'park', '222-2222');
COMMIT;
--성적 등록 여부 확인용 쿼리
SELECT mid, name, tel
, (SELECT COUNT(*) FROM score2 WHERE mid=m.mid) AS count
FROM member2 m
ORDER BY mid;
----------------------------------------
CREATE TABLE score2 (
sid NUMBER --PK, 자동 번호 부여
,mid NUMBER --FK
,kor NUMBER(3) --CK (0~100)
,eng NUMBER(3) --CK (0~100)
,mat NUMBER(3) --CK (0~100)
);
--제약 조건 추가
ALTER TABLE score2
ADD CONSTRAINT score2_sid_pk PRIMARY KEY (sid);
ALTER TABLE score2
ADD CONSTRAINT score2_kor2_ck CHECK (kor BETWEEN 0 AND 100);
ALTER TABLE score2
ADD CONSTRAINT score2_eng_ck CHECK (eng BETWEEN 0 AND 100);
ALTER TABLE score2
ADD CONSTRAINT score2_mat_ck CHECK (mat BETWEEN 0 AND 100);
ALTER TABLE score2
ADD CONSTRAINT score2_mid_fk FOREIGN KEY (mid)
REFERENCES member2(mid);
--자동 번호 부여 시퀀스 객체 생성
CREATE SEQUENCE score2Seq;
--INSERT 쿼리 샘플 (쿼리 끝 부분에 ; 표시하지 말 것)
INSERT INTO score2 (sid, mid, kor, eng, mat)
VALUES (score2Seq.nextval, 1, 100, 100, 100)
--SELECT 쿼리 샘플 (쿼리 끝 부분에 ; 표시하지 말 것)
SELECT m.mid AS mid, sid, name, kor, eng, mat
, (kor+eng+mat) AS tot
, (kor+eng+mat)/3 AS ave
, CASE
WHEN ((kor+eng+mat)/3 >= 60) AND (kor<40 OR eng<40 OR
mat<40) THEN '과락'
WHEN ((kor+eng+mat)/3 >= 60) THEN '합격'
ELSE '불합격'
END AS grade
FROM member2 m, score2 s
WHERE m.mid = s.mid(+)
--SELECT 쿼리 전용 뷰 생성(scott 계정)
CREATE OR REPLACE VIEW member2Score2View
AS
SELECT m.mid AS mid, sid, name, kor, eng, mat
, (kor+eng+mat) AS tot
, (kor+eng+mat)/3 AS ave
, CASE
WHEN ((kor+eng+mat)/3 >= 60) AND (kor<40 OR eng<40 OR
mat<40) THEN '과락'
WHEN ((kor+eng+mat)/3 >= 60) THEN '합격'
ELSE '불합격'
END AS grade
FROM member2 m, score2 s
WHERE m.mid = s.mid(+);
--뷰를 이용한 SELECT 쿼리 실행
SELECT mid, sid, name, kor, eng, mat, tot, ave, grade
FROM member2Score2View;
----------------------------------------------
//MemberDTO.java -> 회원 정보 저장용 클래스
//ScoreDTO.java -> 성적 정보 저장용 클래스
//memberScoreIbatis.xml -> iBatis용 쿼리 객체 저장소
//sqlMapConfig.xml -> 데이터베이스 연결 정보 지정, iBatis XML 파일 등록.
//MemberAction.java -> 액션클래스. 데이터베이스 액세스 전용 클래스
//ScoreAction.java -> 액션클래스. 데이터베이스 액세스 전용 클래스
//memberScoreStruts.xml -> Struts2용 서블릿 주소 등록
//struts.xml -> Struts2 XML 파일 등록
//BasicStyle.css
//memberList.jsp -> JSTL, EL 표현으로 대체
//memberInsertForm.jsp
//memberUpdateForm.jsp -> JSTL, EL 표현으로 대체
//scoreList.jsp -> JSTL, EL 표현으로 대체
//scoreInsertForm.jsp
//scoreUpdateForm.jsp -> JSTL, EL 표현으로 대체
//요청주소
http://localhost:8090/MemberScore_20121218/memberList.action
----------------------------------------
//MemberDTO.java -> 회원 정보 저장용 클래스
package com.test;
public class MemberDTO {
private String mid;
private String name, tel;
//성적이 등록된 상태인지 확인하기 위한 변수 추가
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getMid() {
return mid;
}
public void setMid(String mid) {
this.mid = mid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
//ScoreDTO.java -> 성적 정보 저장용 클래스
package com.test;
public class ScoreDTO {
//null 데이터 처리를 위해서 멤버변수의 자료형을 String으로 처리함.
private String mid, sid; //추가
private String name;
private String kor, eng, mat;
private String tot;
private String ave;
private String grade;
//추가
public String getMid() {
return mid;
}
public void setMid(String mid) {
this.mid = mid;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKor() {
return kor;
}
public void setKor(String kor) {
this.kor = kor;
}
public String getEng() {
return eng;
}
public void setEng(String eng) {
this.eng = eng;
}
public String getMat() {
return mat;
}
public void setMat(String mat) {
this.mat = mat;
}
public String getTot() {
return tot;
}
public void setTot(String tot) {
this.tot = tot;
}
public String getAve() {
return ave;
}
public void setAve(String ave) {
this.ave = ave;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
//memberScoreIbatis.xml -> iBatis용 쿼리 객체 저장소
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="mySql">
<!-- 자주 사용하는 클래스에 대한 별칭 지정 가능 -->
<typeAlias alias="memberDTO" type="com.test.MemberDTO"/>
<typeAlias alias="scoreDTO" type="com.test.ScoreDTO"/>
<insert id="addMember" parameterClass="memberDTO">
INSERT INTO member2 (mid, name, tel)
VALUES (member2Seq.nextval, #name#, #tel#)
</insert>
<select id="countMember" resultClass="Integer">
SELECT COUNT(*) AS count FROM member2
</select>
<select id="listsMember" resultClass="memberDTO">
SELECT mid, name, tel, (SELECT COUNT(*) FROM score2 WHERE mid=m.mid) AS count FROM member2 m ORDER BY mid
</select>
<delete id="removeMember" parameterClass="String">
DELETE member2 WHERE mid=#mid#
</delete>
<select id="searchMember" parameterClass="String" resultClass="com.test.MemberDTO" >
SELECT mid, name, tel FROM member2 WHERE mid=#mid#
</select>
<update id="modifyMember" parameterClass="com.test.MemberDTO">
UPDATE member2 SET name=#name#, tel=#tel# WHERE mid=#mid#
</update>
<insert id="addScore" parameterClass="scoreDTO">
INSERT INTO score2 (sid, mid, kor, eng, mat)
VALUES (score2Seq.nextval, #mid#, #kor#, #eng#, #mat#)
</insert>
<select id="listsScore" resultClass="com.test.ScoreDTO">
SELECT mid, sid, name, kor, eng, mat, tot, ave, grade
FROM member2Score2View
</select>
<delete id="removeScore" parameterClass="String">
DELETE score2 WHERE mid=#mid#
</delete>
<select id="searchScore" parameterClass="String" resultClass="com.test.ScoreDTO">
SELECT kor, eng, mat FROM score2 WHERE mid=#mid#
</select>
<update id="modifyScore" parameterClass="com.test.ScoreDTO">
UPDATE score2 SET kor=#kor#, eng=#eng#, mat=#mat# WHERE mid=#mid#
</update>
</sqlMap>
//sqlMapConfig.xml -> 데이터베이스 연결 정보 지정, iBatis XML 파일 등록.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings
cacheModelsEnabled="false"
useStatementNamespaces="true"/>
<!-- Configure a built-in transaction manager. If you're using an
app server, you probably want to use its transaction manager
and a managed datasource -->
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="JDBC.Username" value="scott"/>
<property name="JDBC.Password" value="tiger"/>
</dataSource>
</transactionManager>
<!-- List the SQL Map XML files. They can be loaded from the
classpath, as they are here (com.domain.data...) -->
<sqlMap resource="com/util/sqlMap/memberScoreIbatis.xml"/>
<!-- List more here...
<sqlMap resource="com/mydomain/data/Order.xml"/>
<sqlMap resource="com/mydomain/data/Documents.xml"/>
-->
</sqlMapConfig>
//MemberAction.java -> 액션클래스. 데이터베이스 액세스 전용 클래스
package com.test;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.util.dao.*;
import java.sql.SQLException;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class MemberAction extends ActionSupport
implements Preparable, ModelDriven<MemberDTO> {
private static final long serialVersionUID = 1L;
private MemberDTO dto;
@Override
public MemberDTO getModel() {
return dto;
}
@Override
public void prepare() throws Exception {
dto = new MemberDTO();
}
public String memberList() {
CommonDAO dao = CommonDAOImpl.getInstance();
List<Object> arrayList = dao.getListData("mySql.listsMember");
HttpServletRequest req = ServletActionContext.getRequest();
req.setAttribute("arrayList", arrayList);
return SUCCESS;
}
public String memberInsert() {
CommonDAO dao = CommonDAOImpl.getInstance();
try {
dao.insertData("mySql.addMember", dto);
} catch (SQLException e) {
System.out.println(e.toString());
}
return SUCCESS;
}
}
//ScoreAction.java -> 액션클래스. 데이터베이스 액세스 전용 클래스
package com.test;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.util.dao.CommonDAO;
import com.util.dao.CommonDAOImpl;
public class ScoreAction extends ActionSupport
implements Preparable, ModelDriven<ScoreDTO> {
private ScoreDTO dto;
@Override
public ScoreDTO getModel() {
return dto;
}
@Override
public void prepare() throws Exception {
dto = new ScoreDTO();
}
public String scoreList() {
CommonDAO dao = CommonDAOImpl.getInstance();
List<Object> arrayList = dao.getListData("mySql.listsScore");
HttpServletRequest req = ServletActionContext.getRequest();
req.setAttribute("arrayList", arrayList);
return SUCCESS;
}
public String scoreInsertForm() {
CommonDAO dao = CommonDAOImpl.getInstance();
Object newdto = dao.getReadData("mySql.searchMember", dto.getMid());
HttpServletRequest req = ServletActionContext.getRequest();
req.setAttribute("dto", newdto);
return SUCCESS;
}
public String scoreInsert() {
CommonDAO dao = CommonDAOImpl.getInstance();
try {
dao.insertData("mySql.addScore", dto);
} catch (SQLException e) {
System.out.println(e.toString());
}
return SUCCESS;
}
}
//memberScoreStruts.xml -> Struts2용 서블릿 주소 등록
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="myStruts" extends="struts-default" namespace="" >
<action name="memberList"
class="com.test.MemberAction"
method="memberList">
<result>/memberList.jsp</result>
</action>
<action name="memberInsertForm">
<!-- type 속성이 없는 부분은 forward 액션 처리 -->
<result>/memberInsertForm.jsp</result>
</action>
<action name="memberInsert"
class="com.test.MemberAction"
method="memberInsert">
<!-- type="redirectAction" 속성은 리다이렉트 액션 처리할 때 사용 -->
<result type="redirectAction">memberList.action</result>
</action>
<action name="scoreList"
class="com.test.ScoreAction"
method="scoreList">
<result>/scoreList.jsp</result>
</action>
<action name="scoreInsertForm"
class="com.test.ScoreAction"
method="scoreInsertForm">
<result>/scoreInsertForm.jsp</result>
</action>
<action name="scoreInsert"
class="com.test.ScoreAction"
method="scoreInsert">
<result type="redirectAction">scoreList.action</result>
</action>
</package>
</struts>
//struts.xml -> Struts2 XML 파일 등록
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default" namespace="" >
<global-results>
<result name="error">/exception/error.jsp</result>
</global-results>
</package>
<include file="memberScoreStruts.xml"></include>
</struts>
//BasicStyle.css
@CHARSET "EUC-KR";
body {
color:#2f3743;
font-size:10pt;
}
a {
text-decoration:none;
color:gray;
}
a:hover {
text-decoration:underline;
color:red;
}
table.style00 td, table.style00 th
{
padding: 3px;
border-right-style: solid;
border-bottom-style: solid;
border-width: 1px;
border-color: #6980b8;
}
table.style00 td:hover
{
background-color:#d2d2d2;
}
#result {
overflow:auto; /* DIV 태그 내부에 스크롤 생성 */
height:100px;
}
//memberList.jsp -> JSTL, EL 표현으로 대체
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>회원 관리+성적 관리</title>
<link rel="stylesheet" type="text/css" href="BasicStyle.css">
<style type="text/css">
.style1 {
text-align: center;
}
</style>
<script type="text/javascript">
function deleteForm(mid) {
if (confirm("선택한 자료를 삭제할까요?")) {
window.location.href="memberDelete.action?mid="+mid;
}
}
function msg() {
if (false) {
alert("성적 삭제를 한 후에만 회원 삭제를 할 수 있습니다.");
}
}
</script>
</head>
<body onload="msg()">
<div>
<div>
[<a href="memberList.action">회원관리</a>]
[<a href="scoreList.action">성적관리</a>]
<hr>
</div>
<div>
<p>[<a href="memberInsertForm.action">회원등록</a>]</p>
<table style="width:400px;" class="style00">
<tbody>
<tr><th>번호</th><th>이름</th><th>전화</th><th></th><th></th></tr>
<c:forEach var="dto" items="${arrayList}">
<tr>
<td class="style1">${dto.mid}</td>
<td class="style1">${dto.name}</td>
<td class="style1">${dto.tel}</td>
<td class="style1"><a href="">[수정]</a></td>
<td class="style1"><a href="">[삭제]</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</body>
</html>
//memberInsertForm.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>회원 관리+성적 관리</title>
<link rel="stylesheet" type="text/css" href="BasicStyle.css">
<script type="text/javascript">
function myFunc() {
obj = document.getElementById("myForm");
//데이터 검사
var name = document.getElementById("name");
var tel = document.getElementById("tel");
var msg = document.getElementById("msg");
msg.style.display = "none";
//빈 칸 검사
if (name.value == ""
|| tel.value == "") {
msg.style.display = "inline";
return;
}
//데이터 전송
obj.submit();
}
</script>
</head>
<body>
<div>
<div>
[<a href="memberList.action">회원관리</a>]
[<a href="scoreList.action">성적관리</a>]
<hr>
</div>
<div>
<h3>-회원 등록-</h3>
<form action="memberInsert.action" method="post" id="myForm">
<table>
<tbody>
<tr>
<td>이름</td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td>전화</td>
<td><input type="text" name="tel" id="tel"></td>
</tr>
<tr>
<td></td>
<td>
[<a href="javascript:myFunc()">등록</a>]
[<a href="memberList.action">취소</a>]<br>
<span id="msg" style="font-size:10pt; color:red; display:none;">이름, 전화를 입력해야 합니다.</span>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</body>
</html>
//scoreList.jsp -> JSTL, EL 표현으로 대체
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>회원 관리+성적 관리</title>
<link rel="stylesheet" type="text/css" href="BasicStyle.css">
<style type="text/css">
.style1 {
text-align: center;
}
.style2 {
text-align: right;
}
</style>
<script type="text/javascript">
function deleteForm(mid) {
if (confirm("선택한 자료를 삭제할까요?")) {
window.location.href="scoreDelete.action?mid="+mid;
}
}
</script>
</head>
<body>
<div>
<div>
[<a href="memberList.action">회원관리</a>]
[<a href="scoreList.action">성적관리</a>]
<hr>
</div>
<div>
<table style="width:600px;" class="style00">
<tbody>
<tr><th>번호</th><th>이름</th><th>국어</th><th>영어</th><th>수학</th><th>총점</th><th>평균</th><th>판정</th><th></th></tr>
<c:forEach var="dto" items="${arrayList}">
<tr>
<td class="style1">${dto.mid}</td>
<td class="style1">${dto.name}</td>
<td class="style2">
<c:choose>
<c:when test="${empty dto.kor}">X</c:when>
<c:otherwise>${dto.kor}</c:otherwise>
</c:choose>
</td>
<td class="style2">
<c:choose>
<c:when test="${empty dto.eng}">X</c:when>
<c:otherwise>${dto.eng}</c:otherwise>
</c:choose>
</td>
<td class="style2">
<c:choose>
<c:when test="${empty dto.mat}">X</c:when>
<c:otherwise>${dto.mat}</c:otherwise>
</c:choose>
</td>
<td class="style2">
<c:choose>
<c:when test="${empty dto.tot}">X</c:when>
<c:otherwise>${dto.tot}</c:otherwise>
</c:choose>
</td>
<td class="style2">
<c:choose>
<c:when test="${empty dto.ave}">X</c:when>
<c:otherwise><fmt:formatNumber value="${dto.ave}" pattern="0.0"></fmt:formatNumber></c:otherwise>
</c:choose>
</td>
<td class="style1">${dto.grade}</td>
<td class="style1">
<c:choose>
<c:when test="${empty dto.kor}">
<a href="scoreInsertForm.action?mid=${dto.mid}">[입력]</a> [수정] [삭제]
</c:when>
<c:otherwise>
[입력] <a href="scoreUpdateForm.action?mid=${dto.mid}">[수정]</a> <a href="javascript:deleteForm(${dto.mid})">[삭제]</a>
</c:otherwise>
</c:choose>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</body>
</html>
//scoreInsertForm.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>회원 관리+성적 관리</title>
<link rel="stylesheet" type="text/css" href="BasicStyle.css">
<script type="text/javascript">
function myFunc() {
obj = document.getElementById("myForm");
//문제) 데이터 검사 과정 추가
var kor = document.getElementById('kor');
var eng = document.getElementById("eng");
var mat = document.getElementById("mat");
var msg = document.getElementById("msg");
msg.style.display = "none";
//빈칸 검사
if(kor.value == "" || eng.value ==""|| mat.value==""){
msg.style.display = "inline";
return;
}
//숫자 검사
if (kor.value.match(/[^0-9]/) || eng.value.match(/[^0-9]/)||mat.value.match(/[^0-9]/)){
msg.style.display = "inline";
return;
}
//범위 검사
if(parseInt(kor.value)>100 ||parseInt(eng.value)>100 ||parseInt(mat.value)>100 ){
msg.style.display = "inline";
return;
}
//데이터 전송
obj.submit();
}
</script>
</head>
<body>
<div>
<div>
[<a href="memberList.action">회원관리</a>]
[<a href="scoreList.action">성적관리</a>]
<hr>
</div>
<div>
<h3>-성적 입력-</h3>
<form action="scoreInsert.action" method="post" id="myForm">
<%-- 성적 입력을 위해서 반드시 전송해야 되는 값 --%>
<input type="hidden" name="mid" value="${dto.mid}">
<table>
<tbody>
<tr>
<td>이름</td>
<td><span style="font-weight: bold;">${dto.name}</span></td>
</tr>
<tr>
<td>국어</td>
<td><input type="text" name="kor" id="kor"></td>
</tr>
<tr>
<td>영어</td>
<td><input type="text" name="eng" id="eng"></td>
</tr>
<tr>
<td>수학</td>
<td><input type="text" name="mat" id="mat"></td>
</tr>
<tr>
<td></td>
<td>
[<a href="javascript:myFunc()">등록</a>]
[<a href="scoreList.action">취소</a>]<br>
<span id="msg" style="font-size:10pt; color:red; display:none;">국어, 영어, 수학 점수를 0~100 사이의 숫자로 입력해야 합니다.</span>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</body>
</html>
//요청주소
http://localhost:8090/MemberScore_20121218/memberList.action
--------------------------------------------------------
문제) 성적 수정, 삭제와 회원 수정, 삭제 과정은 각자 작성할 것.
//memberScoreStruts.xml -> 액션 주소 추가
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="myStruts" extends="struts-default" namespace="" >
<action name="memberList"
class="com.test.MemberAction"
method="memberList">
<result>/memberList.jsp</result>
</action>
<action name="memberInsertForm">
<!-- type 속성이 없는 부분은 forward 액션 처리 -->
<result>/memberInsertForm.jsp</result>
</action>
<action name="memberInsert"
class="com.test.MemberAction"
method="memberInsert">
<!-- type="redirectAction" 속성은 리다이렉트 액션 처리할 때 사용 -->
<result type="redirectAction">memberList.action</result>
</action>
<action name="memberUpdateForm"
class="com.test.MemberAction"
method="memberUpdateForm">
<result>/memberUpdateForm.jsp</result>
</action>
<action name="memberUpdate"
class="com.test.MemberAction"
method="memberUpdate">
<result type="redirectAction">memberList.action</result>
</action>
<action name="memberDelete"
class="com.test.MemberAction"
method="memberDelete">
<result type="redirectAction">memberList.action</result>
</action>
<action name="scoreList"
class="com.test.ScoreAction"
method="scoreList">
<result>/scoreList.jsp</result>
</action>
<action name="scoreInsertForm"
class="com.test.ScoreAction"
method="scoreInsertForm">
<result>/scoreInsertForm.jsp</result>
</action>
<action name="scoreInsert"
class="com.test.ScoreAction"
method="scoreInsert">
<result type="redirectAction">scoreList.action</result>
</action>
<action name="scoreUpdateForm"
class="com.test.ScoreAction"
method="scoreUpdateForm">
<result>/scoreUpdateForm.jsp</result>
</action>
<action name="scoreUpdate"
class="com.test.ScoreAction"
method="scoreUpdate">
<result type="redirectAction">scoreList.action</result>
</action>
<action name="scoreDelete"
class="com.test.ScoreAction"
method="scoreDelete">
<result type="redirectAction">scoreList.action</result>
</action>
</package>
</struts>
//memberAction.java -> 액션 메소드 추가
package com.test;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.util.dao.*;
import java.sql.SQLException;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class MemberAction extends ActionSupport
implements Preparable, ModelDriven<MemberDTO> {
private static final long serialVersionUID = 1L;
private MemberDTO dto;
@Override
public MemberDTO getModel() {
return dto;
}
@Override
public void prepare() throws Exception {
dto = new MemberDTO();
}
public String memberList() {
CommonDAO dao = CommonDAOImpl.getInstance();
List<Object> arrayList = dao.getListData("mySql.listsMember");
HttpServletRequest req = ServletActionContext.getRequest();
req.setAttribute("arrayList", arrayList);
return SUCCESS;
}
public String memberInsert() {
CommonDAO dao = CommonDAOImpl.getInstance();
try {
dao.insertData("mySql.addMember", dto);
} catch (SQLException e) {
System.out.println(e.toString());
}
return SUCCESS;
}
public String memberUpdateForm() {
CommonDAO dao = CommonDAOImpl.getInstance();
//리턴 받는 객체의 갯수가 한 개인 경우
Object newdto = dao.getReadData("mySql.searchMember", dto.getMid());
HttpServletRequest req = ServletActionContext.getRequest();
req.setAttribute("dto", newdto);
return SUCCESS;
}
public String memberUpdate() {
CommonDAO dao = CommonDAOImpl.getInstance();
try {
dao.updateData("mySql.modifyMember", dto);
} catch (SQLException e) {
System.out.println(e.toString());
}
return SUCCESS;
}
public String memberDelete() {
CommonDAO dao = CommonDAOImpl.getInstance();
try {
dao.deleteData("mySql.removeMember", dto.getMid());
} catch (SQLException e) {
System.out.println(e.toString());
}
return SUCCESS;
}
}
//scoreAction.java -> 액션 메소드 추가
package com.test;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.util.dao.*;
import java.sql.SQLException;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class MemberAction extends ActionSupport
implements Preparable, ModelDriven<MemberDTO> {
private static final long serialVersionUID = 1L;
private MemberDTO dto;
@Override
public MemberDTO getModel() {
return dto;
}
@Override
public void prepare() throws Exception {
dto = new MemberDTO();
}
public String memberList() {
CommonDAO dao = CommonDAOImpl.getInstance();
List<Object> arrayList = dao.getListData("mySql.listsMember");
HttpServletRequest req = ServletActionContext.getRequest();
req.setAttribute("arrayList", arrayList);
return SUCCESS;
}
public String memberInsert() {
CommonDAO dao = CommonDAOImpl.getInstance();
try {
dao.insertData("mySql.addMember", dto);
} catch (SQLException e) {
System.out.println(e.toString());
}
return SUCCESS;
}
public String memberUpdateForm() {
CommonDAO dao = CommonDAOImpl.getInstance();
//리턴 받는 객체의 갯수가 한 개인 경우
Object newdto = dao.getReadData("mySql.searchMember", dto.getMid());
HttpServletRequest req = ServletActionContext.getRequest();
req.setAttribute("dto", newdto);
return SUCCESS;
}
public String memberUpdate() {
CommonDAO dao = CommonDAOImpl.getInstance();
try {
dao.updateData("mySql.modifyMember", dto);
} catch (SQLException e) {
System.out.println(e.toString());
}
return SUCCESS;
}
public String memberDelete() {
CommonDAO dao = CommonDAOImpl.getInstance();
try {
dao.deleteData("mySql.removeMember", dto.getMid());
} catch (SQLException e) {
System.out.println(e.toString());
}
return SUCCESS;
}
}
//memberUpdateForm.jsp -> JSTL, EL 표현으로 대체
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>회원 관리+성적 관리</title>
<link rel="stylesheet" type="text/css" href="BasicStyle.css">
<script type="text/javascript">
function myFunc() {
obj = document.getElementById("myForm");
//데이터 검사
var name = document.getElementById("name");
var tel = document.getElementById("tel");
var msg = document.getElementById("msg");
msg.style.display = "none";
//빈 칸 검사
if (name.value == ""
|| tel.value == "") {
msg.style.display = "inline";
return;
}
//데이터 전송
obj.submit();
}
</script>
</head>
<body>
<div>
<div>
[<a href="memberList.action">회원관리</a>]
[<a href="scoreList.action">성적관리</a>]
<hr>
</div>
<div>
<h3>-회원 수정-</h3>
<form action="memberUpdate.action" method="post" id="myForm">
<%-- 회원 수정을 위해서 반드시 전송해야 되는 값 --%>
<input type="hidden" name="mid" value="${dto.mid}">
<table>
<tbody>
<tr>
<td>이름</td>
<td><input type="text" name="name" id="name" value="${dto.name}"></td>
</tr>
<tr>
<td>전화</td>
<td><input type="text" name="tel" id="tel" value="${dto.tel}"></td>
</tr>
<tr>
<td></td>
<td>
[<a href="javascript:myFunc()">수정</a>]
[<a href="memberList.action">취소</a>]<br>
<span id="msg" style="font-size:10pt; color:red; display:none;">이름, 전화를 입력해야 합니다.</span>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</body>
</html>
//scoreUpdateForm.jsp -> JSTL, EL 표현으로 대체
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>회원 관리+성적 관리</title>
<link rel="stylesheet" type="text/css" href="BasicStyle.css">
<script type="text/javascript">
function myFunc() {
obj = document.getElementById("myForm");
//문제) 데이터 검사 과정 추가
var kor = document.getElementById('kor');
var eng = document.getElementById("eng");
var mat = document.getElementById("mat");
var msg = document.getElementById("msg");
msg.style.display = "none";
//빈칸 검사
if(kor.value == "" || eng.value ==""|| mat.value==""){
msg.style.display = "inline";
return;
}
//숫자 검사
if (kor.value.match(/[^0-9]/) || eng.value.match(/[^0-9]/)||mat.value.match(/[^0-9]/)){
msg.style.display = "inline";
return;
}
//범위 검사
if(parseInt(kor.value)>100 ||parseInt(eng.value)>100 ||parseInt(mat.value)>100 ){
msg.style.display = "inline";
return;
}
//데이터 전송
obj.submit();
}
</script>
</head>
<body>
<div>
<div>
[<a href="memberList.action">회원관리</a>]
[<a href="scoreList.action">성적관리</a>]
<hr>
</div>
<div>
<h3>-성적 수정-</h3>
<form action="scoreUpdate.action" method="post" id="myForm">
<%-- 성적 수정을 위해서 반드시 전송해야 되는 값 --%>
<input type="hidden" name="mid" value="${mdto.mid}">
<table>
<tbody>
<tr>
<td>이름</td>
<td><span style="font-weight: bold;">${mdto.name}</span></td>
</tr>
<tr>
<td>국어</td>
<td><input type="text" name="kor" id="kor" value="${sdto.kor}"></td>
</tr>
<tr>
<td>영어</td>
<td><input type="text" name="eng" id="eng" value="${sdto.eng}"></td>
</tr>
<tr>
<td>수학</td>
<td><input type="text" name="mat" id="mat" value="${sdto.mat}"></td>
</tr>
<tr>
<td></td>
<td>
[<a href="javascript:myFunc()">수정</a>]
[<a href="scoreList.action">취소</a>]<br>
<span id="msg" style="font-size:10pt; color:red; display:none;">국어, 영어, 수학 점수를 0~100 사이의 숫자로 입력해야 합니다.</span>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</body>
</html>
//요청주소
http://localhost:8090/MemberScore_20121218/memberList.action
------------------------------------------------------
'Java > Struts2, iBatis' 카테고리의 다른 글
[20121224] 5일차 (상담게시판 Struts2, iBatis 버젼) (0) | 2012.12.28 |
---|---|
[20121221] 4일차 (상담게시판 스트럿츠2 버젼) (0) | 2012.12.28 |
[20121220] 3일차(상담 게시판 (Struts2, iBatis 버전)) (0) | 2012.12.28 |
[20121217] 1일차 (Struts2, iBatis Framework, 환경설정) (0) | 2012.12.18 |
WRITTEN BY