学习SpringMVC(六)之ModelAndView

控制器的方法返回值如果是ModelAndView,则其包含视图信息,也包含模型数据信息。

添加模型数据:

model.addObject(attributeName, attributeValue);
model.addAllObjects(modelMap)

设置视图:

void setView(View view)
void setView(String viewName)

在Controller中:

@RequestMapping(value="/springmvc")
@Controller
public class MyModelAndView {	
	@RequestMapping(value="/testModelAndView")
	public ModelAndView testModelAndView(){
		String viewName="success";
		//ModelAndView model=new ModelAndView(viewName);
		ModelAndView model=new ModelAndView();
		model.setViewName(viewName);
		model.addObject("time", new Date());
		return model;
		
	}

}

在success.jsp中:

  time:${requestScope.time }
  <br>
  time:${time}


你可能感兴趣的:(springMVC,ModelAndView)