Annotation - HttpServletRequest, HttpServletResponse, HttpSession, @CookieValue[실습]
프로그래밍/Spring MVC 2012. 6. 28. 13:54
package spring.mvc.controller;
import java.util.HashMap;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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;
@Controller
public class ParameterTestController {
@RequestMapping("/requestParam1.do")
public ModelAndView requestParamTest1(@RequestParam(required=true)String id,
@RequestParam(required=true, defaultValue="no name")String name, int age){
System.out.println(id+", "+name+", "+age);
HashMap map = new HashMap();
map.put("id", id);
map.put("name", name);
map.put("age", age);
return new ModelAndView("param_res", map);
}
@RequestMapping("/requestParam2.do")
public ModelAndView requestParamTest2(HttpServletRequest request, HttpServletResponse response, HttpSession session, @RequestParam(value="name") String n){
System.out.println(request);
System.out.println(response);
System.out.println(session);
System.out.println("요청파라미터 : "+n);
String name = request.getParameter("name");
Cookie c = new Cookie("cName","cValue");
response.addCookie(c);
String sessionId = session.getId();
System.out.println(name+", "+sessionId);
return new ModelAndView("param_res");
}
}
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>
${requestScope.id }-${requestScope.name }-${requestScope.age }
<hr>
requestParamTest2<br>
${param.name }<br>
${cookie.cName.value }
</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"/>
<bean class="spring.mvc.controller.ParameterTestController"/>
</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="requestParam1.do?id=aaa&age=20">
요청파라미터 테스트1
</a><br>
<a href="requestParam2.do?name=홍길동">요청파라미터 테스트 2</a><br>
</body>
</html>
* 결과
org.apache.catalina.connector.RequestFacade@77eb97org.apache.catalina.connector.ResponseFacade@10c3a08
org.apache.catalina.session.StandardSessionFacade@1b6235b
요청파라미터 : 홍길동
홍길동, 69209E2D3ACAACDE48122FBD180DC523
'프로그래밍 > Spring MVC' 카테고리의 다른 글
로그인, 로그아웃 처리 (0) | 2012.07.02 |
---|---|
spring, jstl, ibatis 연동하여 사용하기 실습- member_springmvc (0) | 2012.06.29 |
@RequestParam Annotation 사용(value, required, defaultValue)[실습] (0) | 2012.06.28 |
Annotation Get,Post 방식으로 구분하기[실습] - 회원관리 (0) | 2012.06.28 |
SpringMVC annotation[실습] (0) | 2012.06.27 |