2. @RequestMapping - 요청 URL 등록, 처리할 요청방식 지정 - 구문 - RequestMapping("오청 url") - RequestMapping(value="요청URL" method=요청방식) - Controller 클래스에 등록 - Controller 메소드에 등록
3. Controller 클래스 스프링 설정파일에 등록 - <bean>을 이용해 등록 - 자동 스캔 - <context:component-scan base-package="package"/>
4. Controller 메소드에서 요청파라미터 처리 - DTO(Command) 이용 - 요청파라미터와 매칭되는 이름의 property를 가진 DTO - 요청파라미터 name을 이용한 매개 변수 사용 - 같은 이름으로 여러 개 값이 넘어올 경우 String[] 사용 - @RequestParam Annotation 사용 - 속성 - value : 요청파라미터 이름 설정 - required : 핑수 여부. 안넘어오면 400 오류 발생. 기본 : true - defaultValue : 값이 안넘어 올 경우 설정할 기본 값
5. Controller 메소드 이용가능 매개변수 타입 - HttpServletRequest - HttpServletResponse - HttpSession - 요청파라미터 연결 변수 - 요청파라미터를 설정할 DTO 객체 - @CookieValue 적용 매개변수 - 쿠키 값 매핑 - @CookieValue(value="name", required=false) - Map, Model, ModelMap - View에 전달할 모델 데이터 설정시 사용
6. Controller 메소드 설정 가능 return type - Controller 메소드 설정 가능 return type - ModelAndView : View 정보와 응답 데이터 설정 - View에 전달할 값 설정 - Map - Model - View는 요청 URL로 결정됨 - String : View의 이름 리턴 - View 객체 - void - Controller 메소드 내에서 응답을 직접처리 시 사용
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>