소스구조는 #10과 동일하다.
소스구조>
[[ web.xml ]]
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>spring06_members</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 인코딩필터 설정하기 -->
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/-</url-pattern>
</filter-mapping>
<!-- /// applicationContext.xml을 읽어와 빈으로 등록하는 리스너 설정 /// -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- //////////////////// DispatcherServlet설정 //////////////////// -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- /////////////////////////////////////////////////////////////// -->
</web-app>
[[ applicatonContext.xml ]]
; annotation사용으로 변경됨.
<?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.xsd">
<!-- //// 전역적인 자원들이 등록 (Dao,Service,...) ////// -->
<!-- 1. DataSource 설정(url,username,password,..) -->
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<!-- 2. SqlSessionFactory설정(dataSource,Mapper파일설정) -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:test/mybatis/-.xml"/>
</bean>
<!-- 3. SqlSessionTemplate설정(SqlSessionFactory주입) -->
<bean id="sqlSessionTemplate"
class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" index="0"/>
</bean>
<!-- 4. Dao생성(SqlSessionTemplate주입) -->
<bean id="membersDao" class="test.dao.MembersDao">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
</beans>
↓
Namespace에서 context선택.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- //// 전역적인 자원들이 등록 (Dao,Service,...)
MVC관련없는 Bean객체 정의
namespace의 context 선택해야 함.
////// -->
<!-- 1. DataSource 설정(url,username,password,..) -->
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<!-- 2. SqlSessionFactory설정(dataSource,Mapper파일설정) -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:test/mybatis/*.xml"/>
</bean>
<!-- 3. SqlSessionTemplate설정(SqlSessionFactory주입) -->
<bean id="sqlSessionTemplate"
class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" index="0"/>
</bean>
</beans>
[[ mvc-servlet.xml ]]
; annotation사용으로 변경됨.
<?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.xsd">
<!-- /////////// MVC관련 Bean객체 등록(컨트롤러,뷰리절버,..) //////// -->
<!-- //////////// 컨트롤러 등록 /////////////////-->
<bean class="test.controller.InsertController">
<property name="membersDao" ref="membersDao"/>
</bean>
<bean class="test.controller.ListController">
<property name="membersDao" ref="membersDao"/>
</bean>
<bean class="test.controller.DeleteController">
<property name="dao" ref="membersDao"/>
</bean>
<bean class="test.controller.UpdateController">
<property name="dao" ref="membersDao"/>
</bean>
<!--///////// 뷰리절버등록 //////////////
/WEB-INF/views/insert.jsp
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
↓
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- /////////// MVC관련 Bean객체 등록(컨트롤러,뷰리절버,..) //////// -->
<!-- //////////// 컨트롤러 등록 /////////////////-->
<!--///////// 뷰리절버등록 //////////////
/WEB-INF/views/insert.jsp
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
; 컨트롤러 등록이 생략됨.
(1) main.jsp
; 동일.
<%@ 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>
<ul>
<li><a href="insert.do">회원가입</a></li>
<li><a href="list.do">회원목록</a></li>
</ul>
</body>
</html>
(2) insert.jsp
; 동일
<%@ 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>
<h2>회원가입</h2>
<form action="insert.do" method="post">
아이디<input type="text" name="id" /><br/>
비밀번호<input type="password" name="pwd" /><br/>
이메일<input type="text" name="email"/><br/>
전화번호<input type="text" name="phone"/><br/>
<input type="submit" value="전송" />
</form>
</body>
</html>
(3) InsertController.java
; @Autowired설정: setter 메소드를 사용안해도 된다.
; @RequestMapping(value="/...do",method=RequestMethod.GET) Get방식 전송
RequestMethod.POST) POST방식 전송
.do 이름을 동일하게 사용할수 있다. (get,post 방식으로 구분하여)
package test.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import test.dao.MembersDao;
import test.vo.MembersVo;
@Controller
public class InsertController {
@Autowired private MembersDao membersDao;
/*@RequestMapping("/insertForm.do")*/
@RequestMapping(value="/insert.do",method=RequestMethod.GET)
public String insertForm(){
//String타입이 반환되는것은 뷰이름을 의미함.
return "insert";
}
@RequestMapping("/insert.do")
public String insert(MembersVo vo){
//db에 vo저장하기
int n = membersDao.insert(vo);
if(n>0){
//뷰페이지로 이동 ==> 리다이렉트방식으로 페이지 이동하기
return "redirect:/main.jsp";
}else{
return "redirect:/error.jsp";
}
}
}
(4) DeleteController.java
; @Autowired설정.
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import test.dao.MembersDao;
import test.vo.MembersVo;
@Controller
public class DeleteController {
@Autowired private MembersDao dao;
@RequestMapping("/delete.do")
public ModelAndView delete(String id){
System.out.println("id:" + id);
dao.delete(id);
//DB에서 회원정보 얻어오기
List<MembersVo> list = dao.getList();
//결과값(모델)과 뷰이름을 담을 객체 생성
ModelAndView mv = new ModelAndView("memlist");
//모델에 결과값 담기
mv.addObject("list", list);
return mv;
}
}
(5) UpdateController.java
; @Autowired설정.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import test.dao.MembersDao;
import test.vo.MembersVo;
@Controller
public class UpdateController {
@Autowired private MembersDao dao;
@RequestMapping(value="/update.do",method=RequestMethod.GET)
public ModelAndView updateForm(String id){
//DB에서 회원정보 얻어오기
MembersVo vo = dao.getInfo(id);
//결과값(모델)과 뷰이름을 담을 객체 생성
ModelAndView mv = new ModelAndView("update");
//모델에 결과값 담기
mv.addObject("vo", vo);
return mv;
}
@RequestMapping("/update.do")
public String update(MembersVo vo){
int n = dao.update(vo);
System.out.println("id:" + vo.getId());
if ( n>0){
System.out.println("성공");
return "redirect:/list.do";
}else{
System.out.println("실패");
return "redirect:/error.jsp";
}
}
}
(6) ListController.java
; @Autowired설정.
package test.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import test.dao.MembersDao;
import test.vo.MembersVo;
@Controller
public class ListController {
@Autowired private MembersDao membersDao;
@RequestMapping("/list.do")
public ModelAndView list(){
//DB에서 회원정보 얻어오기
List<MembersVo> list = membersDao.getList();
//결과값(모델)과 뷰이름을 담을 객체 생성
ModelAndView mv = new ModelAndView("memlist");
//모델에 결과값 담기
mv.addObject("list", list);
return mv;
}
}
(7) MembersDao.java
; @Autowired를 사용하면 setter 메소드가 필요없다.
; @Repository 사용.
package test.dao;
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import test.vo.MembersVo;
//@Controller,@Service,@Component
@Repository
public class MembersDao {
@Autowired private SqlSessionTemplate sqlSessionTemplate;
public List<MembersVo> getList(){
return (List<MembersVo>)sqlSessionTemplate.selectList("getList");
}
public int insert(MembersVo vo){
return sqlSessionTemplate.insert("insert", vo);
}
public int delete(String id){
return sqlSessionTemplate.delete("delete", id);
}
public int update(MembersVo vo){
return sqlSessionTemplate.update("update", vo);
}
public MembersVo getInfo(String id){
return (MembersVo)sqlSessionTemplate.selectOne("getInfo", id);
}
}
(8) MembersVo.java
;기존과 동일
package test.vo;
import java.sql.Timestamp;
public class MembersVo {
private String id;
private String pwd;
private String email;
private String phone;
private Timestamp regdate;
public MembersVo(){}
public MembersVo(String id, String pwd, String email, String phone, Timestamp regdate) {
super();
this.id = id;
this.pwd = pwd;
this.email = email;
this.phone = phone;
this.regdate = regdate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Timestamp getRegdate() {
return regdate;
}
public void setRegdate(Timestamp regdate) {
this.regdate = regdate;
}
}
(9) MembersMapper.java
;기존과 동일
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="orm.mybatis.MembersMapper">
<insert id="insert" parameterType="test.vo.MembersVo">
insert into Members values ( #{ id }, #{ pwd }, #{ email }, #{ phone }, sysdate )
</insert>
<select id="getList" resultType="test.vo.MembersVo">
select * from members
</select>
<delete id="delete" parameterType="String">
delete from members where id=#{value}
</delete>
<select id="getInfo" resultType="test.vo.MembersVo" parameterType="String">
select * from members where id=#{value}
</select>
<update id="update" parameterType="test.vo.MembersVo">
update members set pwd=#{pwd},email=#{email},phone=#{phone} where id=#{id}
</update>
</mapper>
(10) memlist.jsp
;기존과 동일
<%@ 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>
<table border="1" cellpadding="0" cellspacing="0" width="500px">
<tr height="40px">
<th>아이디</th>
<th>비밀번호</th>
<th>이메일</th>
<th>전화번호</th>
<th>X</th>
<th>M</th>
</tr>
<c:forEach var="list" items="${ list }">
<tr>
<td>${ list.id }</td>
<td>${ list.pwd }</td>
<td>${ list.email }</td>
<td>${ list.phone }</td>
<td><a href="delete.do?id=${list.id}">삭제</a></td>
<td><a href="update.do?id=${list.id}">수정</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
(11) update.jsp
;기존과 동일
<%@ 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>
<h2>회원수정</h2>
<form action="update.do" method="post">
아이디<input type="text" name="id" value="${ vo.id }" disabled/>
<input type="hidden" name="id" value="${ vo.id }"/>
<br/>
비밀번호<input type="password" name="pwd" value="${ vo.pwd }" /><br/>
이메일<input type="text" name="email" value="${ vo.email }" /><br/>
전화번호<input type="text" name="phone" value="${ vo.phone }" /><br/>
<input type="submit" value="전송" />
</form>
</body>
</html>
결과 화면>
'WEB > Spring3.0' 카테고리의 다른 글
SPRING3.0 #12(게시판 annotation사용) (0) | 2013.01.27 |
---|---|
SPRING3.0 #11(게시판) (0) | 2013.01.27 |
SPRING3.0 #10( MVC ) (0) | 2013.01.27 |
SPRING3.0 #09( MVC ) (0) | 2013.01.27 |
SPRING3.0 #08( Mybatis ) (0) | 2013.01.27 |