springmvc数据处理模型

一、ModelAndView:

   目标方法的返回值可以是ModelAndView类型,其中可以包含视图和模型信息

   springmvc会把ModelAndView的model中数据放在request域对象中

 1 @RequestMapping("/springmvc")
 2 @Controller
 3 public class SpringMVCTest {
 4     private static final String SUCCESS = "success";
 5 /**
 6  * 目标方法的返回值可以是ModelAndView类型
 7  * 其中可以包含视图和模型信息
 8  * springmvc会把ModelAndView的model中数据放在request域对象中
 9  * @return
10  */
11     @RequestMapping("/testModelAndView")
12     public ModelAndView testModelAndView(){
13         String viewName=SUCCESS;
14         ModelAndView modelAndView=new ModelAndView(viewName);
15         //添加模型数据到ModelAndView中
16         modelAndView.addObject("time", new Date());
17         return modelAndView;
18     }

index.jsp

1 <a href="springmvc/testModelAndView">Test ModelAndView<a>

success.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h4>Success Page</h4>
11     time:${requestScope.time}
12 </body>
13 </html>

其他代码参照HelloWorld实例。

运行结果:

以下三种用的较少,日后用到补充。

二、Model及Map

三、@SessionAttributes

四、@ModelAttribute

posted @ 2016-11-12 10:29  王小霞  阅读(476)  评论(0编辑  收藏  举报