ModelAndView
org.springframework.web.servelt 패키지에 속해있는 클래스로 컨트롤러의 처리결과를 보여줄 뷰와 전달할 값을 저장할 용도로 쓰인다.
ModelAndView클래스를 보면 다음과 같이 구성되어 있다.
package org.springframework.web.servlet;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
public class ModelAndView {
private Object view;
private ModelMap model;
private HttpStatus status;
private boolean cleared = false;
public ModelAndView() {
}
public ModelAndView(String viewName) {
this.view = viewName;
}
public ModelAndView(View view) {
this.view = view;
}
public ModelAndView(String viewName, Map<String, ?> model) {
this.view = viewName;
if (model != null) {
this.getModelMap().addAllAttributes(model);
}
}
public ModelAndView(View view, Map<String, ?> model) {
this.view = view;
if (model != null) {
this.getModelMap().addAllAttributes(model);
}
}
public ModelAndView(String viewName, HttpStatus status) {
this.view = viewName;
this.status = status;
}
public ModelAndView(String viewName, Map<String, ?> model, HttpStatus status) {
this.view = viewName;
if (model != null) {
this.getModelMap().addAllAttributes(model);
}
this.status = status;
}
public ModelAndView(String viewName, String modelName, Object modelObject) {
this.view = viewName;
this.addObject(modelName, modelObject);
}
public ModelAndView(View view, String modelName, Object modelObject) {
this.view = view;
this.addObject(modelName, modelObject);
}
public void setViewName(String viewName) {
this.view = viewName;
}
public String getViewName() {
return this.view instanceof String ? (String)this.view : null;
}
public void setView(View view) {
this.view = view;
}
public View getView() {
return this.view instanceof View ? (View)this.view : null;
}
public boolean hasView() {
return this.view != null;
}
public boolean isReference() {
return this.view instanceof String;
}
protected Map<String, Object> getModelInternal() {
return this.model;
}
public ModelMap getModelMap() {
if (this.model == null) {
this.model = new ModelMap();
}
return this.model;
}
public Map<String, Object> getModel() {
return this.getModelMap();
}
public void setStatus(HttpStatus status) {
this.status = status;
}
public HttpStatus getStatus() {
return this.status;
}
public ModelAndView addObject(String attributeName, Object attributeValue) {
this.getModelMap().addAttribute(attributeName, attributeValue);
return this;
}
public ModelAndView addObject(Object attributeValue) {
this.getModelMap().addAttribute(attributeValue);
return this;
}
public ModelAndView addAllObjects(Map<String, ?> modelMap) {
this.getModelMap().addAllAttributes(modelMap);
return this;
}
public void clear() {
this.view = null;
this.model = null;
this.cleared = true;
}
public boolean isEmpty() {
return this.view == null && CollectionUtils.isEmpty(this.model);
}
public boolean wasCleared() {
return this.cleared && this.isEmpty();
}
public String toString() {
StringBuilder sb = new StringBuilder("ModelAndView: ");
if (this.isReference()) {
sb.append("reference to view with name '").append(this.view).append("'");
} else {
sb.append("materialized View is [").append(this.view).append(']');
}
sb.append("; model is ").append(this.model);
return sb.toString();
}
}
다음과 같이 map에 데이터를 담거나 직접 값을 전달해 줄 수있다.
@RequestMapping(value="list")
public ModelAndView list(@RequestParam(defaultValue="title") String searchOption,
@RequestParam(defaultValue="") String keyword,
@RequestParam(defaultValue="1") int curPage) throws Exception{
//전달할 정보 가져오기
int count = adminService.countboard(searchOption, keyword);
BoardPage boardPage = new BoardPage(count, curPage);
int start = boardPage.getPageBegin();
int end = boardPage.getPageEnd();
List<WebBoard> list = adminService.Viewlist(start, end, searchOption, keyword);
//전달할 정보 map에 담기
Map<String, Object> map = new HashMap<String, Object>();
map.put("list", list);
map.put("searchOption", searchOption);
map.put("keyword", keyword);
map.put("boardPage", boardPage);
ModelAndView mav = new ModelAndView();
//ModelAndView 객체에 전달할 정보(map)과 뷰 설정하기
mav.addObject("map", map);
//map에 넣지않고 직접 전달해도 된다.
mav.addObject("count",count);
mav.setViewName("/admin/adminmode");
return mav;
}
ModelAndView 외에도 다양하게 컨트롤러의 리턴타입을 정할 수 있음.
@ReqeustMapping("/list/")
public String list(Model model){
int count = adminService.countboard();
model.addAttribute("count", count);
return "/admin/adminmode"
}