뷰에 모델(Model) 전달
※ @RequestMapping 어노테이션이 적용된 메소드의 파라미터나 리턴 타입으로 ModelAndView, Model, ModelMap, Map, 커맨드 객체 등을 이용해서 모델을 뷰어 전달
◆ @RequestMapping 메소드가 ModelAndView, Model, Map을 리턴하는 경우 이들에 담긴 모델 데이터가 뷰에 전달
● 추가적으로 다음의 항목도 뷰에 함께 전달
=> 커맨드 객체
=> @ModelAttribute 어노테이션이 적용된 메소드가 리턴한 객체
=> 메서드의 Map, Model, ModelMap 타입의 파라미터를 통해 설정된 모델

UserVO.java
|
package sp.mvc.vo;
public class UserVO {
private String userName;
private String address;
private String fruit;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getFruit() {
return fruit;
}
public void setFruit(String fruit) {
this.fruit = fruit;
}
@Override
public String toString() {
return "UserVO [userName=" + userName + ", address=" + address + ", fruit=" + fruit + "]";
}
}
|
UserController.java
|
package sp.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import sp.mvc.vo.UserVO;
@Controller
public class UserController {
@ModelAttribute("popularFruit")
public String[] refPopularFruit(){
return new String[]{"사과", "포도", "수박", "참외"};
}
@RequestMapping(value="/userForm.sp", method=RequestMethod.GET)
public String userForm(){
System.out.println("----- UserController.userForm() : GET -----");
return "user/userForm";
}
@RequestMapping(value="/userSave.sp", method=RequestMethod.POST)
public ModelAndView userSave(UserVO userVo, Model model){
// public ModelAndView userSave(@ModelAttribute("userVo") UserVO userVo, Model model){
System.out.println("----- UserController.userSave() : POST -----");
System.out.println("userInfo : " + userVo.toString());
model.addAttribute("msg", "SUCCESS");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("user/userInfo");
modelAndView.addObject("userVo", userVo);
return modelAndView;
}
@RequestMapping("/userView.sp")
public Model userView(){
System.out.println("----- UserController.userView() -----");
Model model = new ExtendedModelMap();
model.addAttribute("msg", "member info");
return model;
}
}
|
▦ Map, Model, ModelMap을 통한 모델 설정 방법
■ Map, Model, ModelMap 중 한 가지를 파라미터로 전달
■ Map, Model을 리턴
=> Map, Model은 인터페이스 이므로 인터페이스를 구현한 클래스의 객체를 생성해서 리턴
● @ModelAttribute("popularFruit") : @ModelAttribute 어노테이션이 적용된 메소드가 리턴한 객체
● public ModelAndView userSave(UserVO userVo, Model model) : Model 파라미터, ModelAndView 리턴 타입
● public ModelAndView userSave(@ModelAttribute("userVo") UserVO userVo, Model model) : @ModelAttribute 어노테이션, ModelAndView 리턴 타입
● public Model userView() : Model 리턴 타입
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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>spring303</display-name>
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 공통 빈 설정
<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>
-->
<servlet>
<servlet-name>spring303</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/main_config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring303</servlet-name>
<url-pattern>*.sp</url-pattern>
</servlet-mapping>
</web-app>
|
main_config.xml
userForm.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>회원정보 입력</title>
</head>
<body>
<form action="userSave.sp" method="post">
<table border="1">
<tr>
<td>이름</td>
<td><input type="text" name="userName" value=""/></td>
</tr>
<tr>
<td>주소</td>
<td><input type="text" name="address" value=""/></td>
</tr>
<tr>
<td>과일</td>
<td>
<c:forEach var="fruit" items="${popularFruit}">
${fruit},
</c:forEach>
</td>
</tr>
</table>
<input type="submit" name="submit" value="전송"/>
</form>
</body>
</html>
|
userInfo.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>회원정보 출력</title>
</head>
<body>
<h2>${msg}</h2>
<form action="userView.sp" method="post">
<table border="1">
<tr>
<td>이름</td>
<td>${userVo.userName}</td>
</tr>
<tr>
<td>주소</td>
<td>${userVo.address}</td>
</tr>
<tr>
<td>과일</td>
<td>
<c:forEach var="fruit" items="${popularFruit}">
${fruit},
</c:forEach>
</td>
</tr>
</table>
<input type="submit" name="submit" value="뷰"/>
</form>
</body>
</html>
|
userView.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>회원정보 출력</title>
</head>
<body>
<h2>${msg}</h2>
<c:forEach var="fruit" items="${popularFruit}">
${fruit}
</c:forEach>
</body>
</html>
|
로그 결과
|
----- UserController.userView() -----
----- UserController.userSave() : POST -----
userInfo : UserVO [userName=스누피, address=제주도 456, fruit=null]
----- UserController.userView() -----
|



[출처] http://snoopy81.tistory.com/311