dynamic web : member_springmvc
lib : spring api, jstl, ibatis
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
org.springframework.aop-3.0.2.RELEASE.jar
org.springframework.asm-3.0.2.RELEASE.jar
org.springframework.beans-3.0.2.RELEASE.jar
org.springframework.context-3.0.2.RELEASE.jar
org.springframework.core-3.0.2.RELEASE.jar
org.springframework.expression-3.0.2.RELEASE.jar
org.springframework.jdbc-3.0.2.RELEASE.jar
org.springframework.orm-3.0.2.RELEASE.jar
org.springframework.transaction-3.0.2.RELEASE.jar
org.springframework.web-3.0.2.RELEASE.jar
org.springframework.web.servlet-3.0.2.RELEASE.jar
추가하기
* 추가된 결과
* 설정파일 :
ibatis - SqlMapConfig.xml - WEB-INF
member.xml - member.config/member.xml
스프링 설정 - member-spring.xml - WEB-INF
1. 회원 등록하기
WebContent/register_form.jsp
->
Ctr : registerMember(MemberDTO) : ModelAndView
->
Service : registerMember()
->
DAO.메소드
->응답 : 성공 : /res/register_success.jsp
오류 : /res/error.jsp
2. 전체회원조회
WebContent/menu.jsp(/getAllMemberInfo.do로 요청)
->
Ctr : getAllMemberInfo() : ModelAndView
->
Service : getAllMemberList()
->
DAO.메소드
->
->응답 : 성공 : /res/member_list.jsp
오류 : /res/error.jsp
<?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>
<typeAlias alias="mdto" type="member.dto.MemberDTO"/>
<sql id="select" >
select id,
password,
name,
register_number1 as registerNumber1,
register_number2 as registerNumber2,
mileage
from member
</sql>
<resultMap class="mdto" id="memberRM">
<result property="id" columnIndex="1"/>
<result property="password" columnIndex="2"/>
<result property="name" columnIndex="3"/>
<result property="registerNumber1" columnIndex="4"/>
<result property="registerNumber2" columnIndex="5"/>
<result property="mileage" columnIndex="6" nullValue="-1"/>
</resultMap>
<select id="selectMemberById" parameterClass="string" resultMap="memberRM">
<include refid="select"/>
where id=#id#
</select>
<select id="selectMemberByRegisterNumber" parameterClass="hashmap" resultMap="memberRM">
<include refid="select"/>
where register_Number1=#registerNumber1#
and register_Number2=#registerNumber2#
</select>
<insert id="insertMember" parameterClass="mdto">
insert into member
(id, password, name, register_number1, register_number2, mileage)
values(#id#,#password#,#name#,#registerNumber1#,#registerNumber2#,#mileage#)
</insert>
<select id="selectAllMember" resultMap="memberRM">
<include refid="select"/>
</select>
</sqlMap>
<!-- xxxx.insert("insertMember", mdto); -->
<!-- Object obj = xxx.queryForObject("selectMemberById","abcde"); 추후 MemberDTO로 캐스팅해서 사용 -->
package member.controller;
import java.sql.SQLException;
import java.util.ArrayList;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import member.dto.MemberDTO;
import member.exception.DuplicatedIdException;
import member.exception.DuplicatedRegisterNumberException;
import memver.model.service.MemberManageService;
@Controller
public class MemberController {
private MemberManageService service;
public MemberController(MemberManageService service){
this.service = service;
}
@RequestMapping(value="/registerMember.do")
public ModelAndView registerMember(MemberDTO mdto){
ModelAndView mv = null;
mdto.setMileage(1000);
try {
System.out.println(mdto);
service.registerMember(mdto);
System.out.println("****************");
mv = new ModelAndView("/res/register_success","mdto",mdto);
} catch (Exception e) {
e.printStackTrace();
mv = new ModelAndView("/res/error","error_message",e.getMessage());
}
return mv;
}
@RequestMapping(value="/getAllMemberInfo.do")
public ModelAndView getAllMemberInfo(){
ModelAndView mv = null;
try{
System.out.println("getMemberList()실행");
ArrayList<MemberDTO> list = service.getMemberList();
mv = new ModelAndView("/member_list","member_list",list);
}catch (Exception e) {
mv = new ModelAndView("/res/error", "error_message",e.getMessage());
}
return mv;
}
}
package member.dto;
public class MemberDTO {
private String id;
private String password;
private String name;
private String registerNumber1;
private String registerNumber2;
private int mileage;
public MemberDTO() {
super();
}
public MemberDTO(String id, String password, String name,
String registerNumber1, String registerNumber2, int mileage) {
super();
this.id = id;
this.password = password;
this.name = name;
this.registerNumber1 = registerNumber1;
this.registerNumber2 = registerNumber2;
this.mileage = mileage;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRegisterNumber1() {
return registerNumber1;
}
public void setRegisterNumber1(String registerNumber1) {
this.registerNumber1 = registerNumber1;
}
public String getRegisterNumber2() {
return registerNumber2;
}
public void setRegisterNumber2(String registerNumber2) {
this.registerNumber2 = registerNumber2;
}
public int getMileage() {
return mileage;
}
public void setMileage(int mileage) {
this.mileage = mileage;
}
@Override
public String toString() {
return "MemberDTO [id=" + id + ", password=" + password + ", name="
+ name + ", registerNumber1=" + registerNumber1
+ ", registerNumber2=" + registerNumber2 + ", mileage="
+ mileage + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + mileage;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
result = prime * result
+ ((registerNumber1 == null) ? 0 : registerNumber1.hashCode());
result = prime * result
+ ((registerNumber2 == null) ? 0 : registerNumber2.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MemberDTO other = (MemberDTO) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (mileage != other.mileage)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (registerNumber1 == null) {
if (other.registerNumber1 != null)
return false;
} else if (!registerNumber1.equals(other.registerNumber1))
return false;
if (registerNumber2 == null) {
if (other.registerNumber2 != null)
return false;
} else if (!registerNumber2.equals(other.registerNumber2))
return false;
return true;
}
}
package member.exception;
public class DuplicatedIdException extends Exception{
public DuplicatedIdException(){}
public DuplicatedIdException(String message){
super(message);
}
}
package member.exception;
public class DuplicatedRegisterNumberException extends Exception{
public DuplicatedRegisterNumberException(){}
public DuplicatedRegisterNumberException(String message){
super(message);
}
}
package member.model.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import member.dto.MemberDTO;
public class MemberDAO {
private SqlMapClientTemplate sqlMap;
public MemberDAO(SqlMapClientTemplate sqlMap){
this.sqlMap = sqlMap;
}
public MemberDTO selectMemberById(String id) {
// TODO Auto-generated method stub
return (MemberDTO) sqlMap.queryForObject("selectMemberById", id);
}
//주민번호로 조회
public MemberDTO selectMemberByRegisterNumber(String rN1, String rN2) throws SQLException{
HashMap<String, String> map = new HashMap<String, String>();
map.put("registerNumber1", rN1);
map.put("registerNumber2", rN2);
System.out.println("selectMemberByRegisterNumber ");
return (MemberDTO)sqlMap.queryForObject("selectMemberByRegisterNumber",map);
}
//회원데이터 insert메소드
public Object insertMember(MemberDTO mdto) throws SQLException{
return sqlMap.insert("insertMember", mdto);
}
public ArrayList<MemberDTO> selectAllMember() {
return (ArrayList<MemberDTO>) sqlMap.queryForList("selectAllMember");
}
}
package memver.model.service;
import java.sql.SQLException;
import java.util.ArrayList;
import member.dto.MemberDTO;
import member.exception.DuplicatedIdException;
import member.exception.DuplicatedRegisterNumberException;
import member.model.dao.MemberDAO;
//회원 관리 비지니스 로직을 처리하는 Business Service 클래스
public class MemberManageService {
private MemberDAO dao;
public MemberManageService(MemberDAO dao){
this.dao = dao;
}
//가입 처리 메소드
public void registerMember(MemberDTO mto)throws SQLException, DuplicatedIdException, DuplicatedRegisterNumberException{
//1. 등록된 ID가 있는 지 체크
// - 이미 등록된 ID라면 DuplicatedIdException를 발생시킨다.
if(dao.selectMemberById(mto.getId())!=null){
throw new DuplicatedIdException(mto.getId()+"는 이미등록된 아이디 입니다.");
}
//2. 등록된 주민번호가 있는 지 체크
// - 이미 등록된 주민번호라면 DuplicatedRegisterNumberException 발생시킨다.
if(dao.selectMemberByRegisterNumber(mto.getRegisterNumber1(), mto.getRegisterNumber2())!=null){
throw new DuplicatedRegisterNumberException("는 이미 가입된 주민번호 입니다.");
}
//3. 디비에 회원정보 등록 처리
dao.insertMember(mto);
}
//전체 회원목록 조회
public ArrayList<MemberDTO> getMemberList() throws SQLException{
return dao.selectAllMember();
}
// //마일리지 범위로 조회
// public ArrayList<MemberDTO> selectMemberByMileageRange(int startMileage, int endMileage)throws SQLException {
// return dao.selectMemberByMileageRange(startMileage, endMileage);
// }
// //ID로 회원 목록조회
// public MemberDTO getMemberById(String id) throws SQLException{
// return dao.selectMemberById(id);
// }
// public void modifyMember(MemberDTO mto) throws SQLException{
// dao.updateMember(mto);
//
// }
// public void removeMemberById(String id)throws SQLException {
// dao.deleteMemberById(id);
//
// }
// public ArrayList<MemberDTO> getSearchByMemberName(String name) throws SQLException{
// return dao.searchByMemberName(name);
//
// }
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:include page="/menu.jsp"/>
<c:if test="${requestScope.error_message!=null }">
실행 도중 오류가 발생했습니다.<br>
오류 내용 ${error_message }
</c:if>
<a href='/${initParam.context_root }/index.jsp'>메인페이지로 이동</a>
</body>
</html>
<%@page import="member.dto.MemberDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:include page="/menu.jsp"/>
<h2>가입 환영합니다.</h2>
<h3>가입 시 입력한 정보입니다. </h3>
ID : ${requestScope.mdto.id }
Password : ${requestScope.mdto.password }
이름 : ${requestScope.mdto.name }
주민번호 : ${requestScope.mdto.registerNumber1 } - ${requestScope.mdto.registerNumber2 }
마일리지 : ${requestScope.mdto.mileage }<p>
<a href="/${initParam.context_root }/login_form.jsp">로그인</a>
<a href="/${initParam.context_root }/index.jsp">메인페이지</a>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- View Resolver등록 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"/>
</bean>
<!-- DataSource 등록 -->
<bean name="datasource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<!-- SqlMapClientFactoryBean -->
<bean name="sqlMapClientFactoryBean" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="/WEB-INF/SqlMapConfig.xml"/>
<property name="dataSource" ref="datasource"/>
</bean>
<!-- SqlMapClientTemplate -->
<bean name="sqlMapclient" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClientFactoryBean"/>
</bean>
<!-- MemberDAO -->
<bean name="memberDAO" class="member.model.dao.MemberDAO">
<constructor-arg ref="sqlMapclient"/>
</bean>
<!-- MemberManageService -->
<bean name="memberManageService" class="memver.model.service.MemberManageService">
<constructor-arg ref="memberDAO"/>
</bean>
<!-- MemberController -->
<bean name="memberController" class="member.controller.MemberController">
<constructor-arg ref="memberManageService"/>
</bean>
</beans>
<?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>
<sqlMap resource="member/config/member.xml"/>
</sqlMapConfig>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- DispatcherServlet 등록 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/member-spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup><!-- 어플리케이션 시작시점에 load 되게 해준다. -->
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 한글 encoding 처리 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ContextParam 등록하기 -->
<context-param>
<param-name>context_root</param-name>
<param-value>member_springmvc</param-value>
</context-param>
</web-app>
<%@page import="member.dto.MemberDTO"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@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>
<title>Insert title here</title>
<body>
<jsp:include page="/menu.jsp"/>
<h2>모든 회원 목록조회</h2><hr><p>
<table width="500" border="1" cellspacing="0">
<tr align="center">
<td>순번</td><td>아이디</td><td>이름</td><td>주민번호</td><td>마일리지</td>
</tr>
<c:forEach items="${requestScope.member_list }" var="member_list" varStatus="cnt">
<tr align="center">
<td>${cnt.count }</td><td>${member_list.id }</td><td><a href="/servlet_member/SearchMemberByID?id=${member_list.id }">${member_list.name }</a></td><td>${member_list.registerNumber1 }-${member_list.registerNumber2 }</td><td>${member_list.mileage }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<a href='/${initParam.context_root }/index.jsp'>메인페이지</a>
<c:choose>
<c:when test="${sessionScope.memberDTO == null }">
<a href='/${initParam.context_root }/register_form.jsp'>회원가입폼</a>
<a href="/${initParam.context_root }/login_form.jsp">로그인폼</a>
<a href="/${initParam.context_root }/search_form.jsp">검색</a>
</c:when>
<c:otherwise>
<a href='/${initParam.context_root }/controller?command=logout'>로그아웃</a>
<a href="/${initParam.context_root }/controller?command=modify_member_list">회원정보조회</a>
<a href="/${initParam.context_root }/res/modify_form.jsp">회원정보 수정</a>
<a href="/${initParam.context_root }/controller?command=member_remove">회원 탈퇴</a>
</c:otherwise>
</c:choose>
<a href="/${initParam.context_root }/getAllMemberInfo.do">전체 회원정보 조회</a>
<hr width="700" align="left" noshade="noshade">
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function registerCheck(){
var id = document.register_form.id;
var pwd = document.register_form.password;
var name = document.register_form.name;
var registerNumber1 = document.register_form.registerNumber1;
var registerNumber2 = document.register_form.registerNumber2;
if(id.value==""){
alert("아이디를 입력해 주세요.");
id.focus();
return false;
}
if(pwd.value==""){
alert("비밀번호를 입력해 주세요.");
pwd.focus();
return false;
}
if(name.value==""){
alert("이름을 입력해 주세요.");
name.focus();
return false;
}
if(registerNumber1.value==""){
alert("주민번호를 입력해 주세요.");
registerNumber1.focus();
return false;
}
if(registerNumber1.value.length!=6){
alert("주민번호 앞자리 6자리를 입력해 주세요.");
registerNumber1.focus();
return false;
}
if(registerNumber2.value==""){
alert("주민번호를 입력해 주세요.");
registerNumber2.focus();
return false;
}
if(registerNumber2.value.length!=7){
alert("주민번호 뒷자리 7자리를 입력해 주세요.");
registerNumber1.focus();
return false;
}
}
</script>
</head>
<body>
<jsp:include page="/menu.jsp"/>
<form action="/${initParam.context_root }/registerMember.do" method="post" name="register_form" onsubmit="return registerCheck()">
<table border="1">
<tr>
<td align="center">id</td><td><input type="text" name="id" maxlength="10" size="10"></td>
</tr>
<tr>
<td align="center">pwd</td><td><input type="password" name="password" maxlength="4" size="4"></td>
</tr>
<tr>
<td align="center">이름 : </td><td><input type="text" name="name" maxlength="10" size="10"></td>
</tr>
<tr>
<td align="center">주민번호 : </td>
<td>
<input type="text" name="registerNumber1" maxlength="6" size="6">-
<input type="text" name="registerNumber2" maxlength="7" size="7">
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="가입">
<input type="reset" value="다시작성"></td>
</tr>
</table>
</form>
</body>
</html>
* 결과
'프로그래밍 > Spring MVC' 카테고리의 다른 글
회원정보 수정 폼, 회원정보 수정 처리, 이름으로 검색, 마일리지 범위로 검색(Spring MVC, ibatis 이용) (0) | 2012.07.02 |
---|---|
로그인, 로그아웃 처리 (0) | 2012.07.02 |
Annotation - HttpServletRequest, HttpServletResponse, HttpSession, @CookieValue[실습] (0) | 2012.06.28 |
@RequestParam Annotation 사용(value, required, defaultValue)[실습] (0) | 2012.06.28 |
Annotation Get,Post 방식으로 구분하기[실습] - 회원관리 (0) | 2012.06.28 |