SpringMVC-MultiActionContriller[실습]- SpringMVC_02_multiaction(조회, 등록, 로그인처리)
프로그래밍/Spring MVC 2012. 6. 27. 16:50
package spring.mvc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import spring.mvc.dto.AddressDTO;
import spring.mvc.dto.CustomerDTO;
public class CustomerController extends MultiActionController{
//id로 고객 조회 일반적으로 ModelAndView를 사용(void, MAP, ModelAndView)
public ModelAndView getCustomerById(HttpServletRequest request, HttpServletResponse response)throws Exception{
//1. 요청 파라미터 조회
String id = request.getParameter("id");
//2. Business 로직 처리 - Model 호출(Business Service)
CustomerDTO cto = new CustomerDTO(id, "홍길동", 20, new AddressDTO("111-222", "서울시 송파구 가락동"));
return new ModelAndView("customer_info","cto",cto);
}
public ModelAndView registerCustomer(HttpServletRequest request, HttpServletResponse response, CustomerDTO cto)throws Exception{
//1, 요청파라미터 조회
System.out.println("등록 정보 : "+cto);
//비지니스 로직
return new ModelAndView("customer_info","cto",cto);
}
public ModelAndView login(HttpServletRequest request, HttpServletResponse response, HttpSession session)throws Exception{
//로그인 처리
//로그인 성공
session.setAttribute("login_info", new CustomerDTO("aaa", "홍길동", 20, new AddressDTO("111-222", "서울시 송파구 가락동")));
return new ModelAndView("login_success");
}
}
package spring.mvc.dto;
public class AddressDTO {
private String zipcode;
private String address;
public AddressDTO() {
super();
// TODO Auto-generated constructor stub
}
public AddressDTO(String zipcode, String address) {
super();
this.zipcode = zipcode;
this.address = address;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "AddressDTO [zipcode=" + zipcode + ", address=" + address + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((zipcode == null) ? 0 : zipcode.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;
AddressDTO other = (AddressDTO) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (zipcode == null) {
if (other.zipcode != null)
return false;
} else if (!zipcode.equals(other.zipcode))
return false;
return true;
}
}
package spring.mvc.dto;
public class CustomerDTO {
private String id;
private String name;
private int age;
private AddressDTO addressDTO;
public CustomerDTO() {
super();
// TODO Auto-generated constructor stub
}
public CustomerDTO(String id, String name, int age, AddressDTO addressDTO) {
super();
this.id = id;
this.name = name;
this.age = age;
this.addressDTO = addressDTO;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public AddressDTO getAddressDTO() {
return addressDTO;
}
public void setAddressDTO(AddressDTO addressDTO) {
this.addressDTO = addressDTO;
}
@Override
public String toString() {
return "CustomerDTO [id=" + id + ", name=" + name + ", age=" + age
+ ", addressDTO=" + addressDTO + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((addressDTO == null) ? 0 : addressDTO.hashCode());
result = prime * result + age;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.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;
CustomerDTO other = (CustomerDTO) obj;
if (addressDTO == null) {
if (other.addressDTO != null)
return false;
} else if (!addressDTO.equals(other.addressDTO))
return false;
if (age != other.age)
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
<%@ 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>
조회정보<br>
ID : ${requestScope.cto.id }<br>
이름 : ${requestScope.cto.name }<br>
나이 : ${requestScope.cto.age }<br>
우편번호 : ${requestScope.cto.addressDTO.zipcode }<br>
주소 : ${requestScope.cto.addressDTO.address }
</body>
</html>
<%@ 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>
로그인 성공<br>
${sessionScope.login_info }
</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">
<!-- HandlerMapping 설정 -->
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- ViewResolver설정 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/res/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- methodNameResolver -->
<bean name="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="command"/>
</bean>
<!-- 컨트롤러 등록 -->
<bean name="/customer.do" class="spring.mvc.controller.CustomerController">
<property name="methodNameResolver" ref="methodNameResolver"/>
</bean>
</beans>
<?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">
<display-name>SpringMVC_02_multiaction</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 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/customer_spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 한글 encoding 처리filter 등록 -->
<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>
</web-app>
<%@ 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>
<form action="customer.do" method="post">
<input type="hidden" name="command" value="getCustomerById">
ID : <input type="text" name="id">
<input type="submit" value="조회">
로그인처리
<a href="customer.do?command=login">로그인</a>
</form>
</body>
</html>
<%@ 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>
등록폼
<form method="post" action="customer.do">
ID : <input type="text" name="id"><br>
이름 : <input type="text" name="name"><br>
나이 : <input type="text" name="age"><br>
우편번호 : <input type="text" name="addressDTO.zipcode"><br>
주소 : <input type="text" name="addressDTO.address"><br>
<input type="hidden" name="command" value="registerCustomer">
<input type="submit" value="등록">
</form>
</body>
</html>
* 결과보기
'프로그래밍 > Spring MVC' 카테고리의 다른 글
SpringMVC annotation[실습] (0) | 2012.06.27 |
---|---|
Annotation기반 Controller (0) | 2012.06.27 |
SpringMVS abstract[실습] - SrpingMVC_01_abstract (0) | 2012.06.27 |
ViewRsolver (0) | 2012.06.27 |
ModelAndView (0) | 2012.06.27 |