package spring.mvc.controller;
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 spring.mvc.dto.CustomerDTO;
@Controller
public class CustomerRegisterController {
//등록 form을 return
@RequestMapping(value="/customer/register.do", method=RequestMethod.GET)
public String registerForm(){
System.out.println("registerForm()");
return "register_form";
}
//등록처리
@RequestMapping(value="/customer/register.do", method=RequestMethod.POST)
public ModelAndView registerCustomer(CustomerDTO cto)throws Exception{
System.out.println("registerCustomer() : ");
System.out.println(cto);
return new ModelAndView("register_success","cto",cto);
}
}
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>
등록폼
<form method="post" action="/SpringMVC_03_anno1/customer/register.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="submit" value="등록">
</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>
등록한 정보<br>
아이디 : ${requestScope.cto.id }<br>
이름 : ${requestScope.cto.name }<br>
나이 : ${requestScope.cto.age }<br>
우편번호 : ${requestScope.cto.addressDTO.zipcode }<br>
주소 : ${requestScope.cto.addressDTO.address }
</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">
<!-- Annotation기반이라 HandlerMapping 빈 생략 DefaultAnnotationHandlerMapping이 자동으로 잡힘 -->
<!-- ViewResolver 빈 등록(id는 아무거나 넣어도 알아서 찾음) -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- controller class를 빈으로 등록 -->
<bean name="helloController" class="spring.mvc.controller.HelloController"/>
<bean class="spring.mvc.controller.CustomerRegisterController"/>
</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">
<!-- DispatcherServlet 등록(front controller) -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<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-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>
<a href="hello.do">hello.do</a><br>
<a href="greeting.do?name=이순신">greeting.do</a><br>
<a href="customer/register.do">등록폼요청</a>
</body>
</html>
* 결과
registerForm()registerCustomer() :
CustomerDTO [id=1, name=1, age=1, addressDTO=AddressDTO [zipcode=1, address=1]]
'프로그래밍 > Spring MVC' 카테고리의 다른 글
Annotation - HttpServletRequest, HttpServletResponse, HttpSession, @CookieValue[실습] (0) | 2012.06.28 |
---|---|
@RequestParam Annotation 사용(value, required, defaultValue)[실습] (0) | 2012.06.28 |
SpringMVC annotation[실습] (0) | 2012.06.27 |
Annotation기반 Controller (0) | 2012.06.27 |
SpringMVC-MultiActionContriller[실습]- SpringMVC_02_multiaction(조회, 등록, 로그인처리) (0) | 2012.06.27 |