spring mvc ModelAndView向前台传值

今天在做项目的时候遇到一个问题,把第一个页面保存的id传到第三个页面中去用,原来是在controller层加了一个全局变量控制的,但是后来发现这个变量实现不了我要的功能,于是查了一下,原来ModelAndView这个类有个构造方法可以传参数到前台,最后问题解决。

ModelAndView有7个构造方法,我们用了ModelAndView(String viewName, Map<String,?> model) 这个方法:

ModelAndView(String viewName, Map<String,?> model) :

第一个参数:指定页面要跳转的view视图路径

第二个参数:指定了要项前台传递的参数,在前台可以这样取值 ${sp_ids }

[javascript] view plain copy print ?
  1. @RequestMapping("/list")  
  2. public ModelAndView list(HttpServletRequest request)  
  3.     throws Exception  
  4. {  
  5.     Map context = getRootMap();  
  6.     StudentModel model = new StudentModel();  
  7.     context.put("model", model);  
  8. text.put("sp_ids", id);  
  9.     return forword("stu/studentList", context);  
  10. }  


[java] view plain copy print ?
  1.   
这里,页面将跳转到studentList.jsp页面,id值也会传到前台去。

前台代码如下:

[html] view plain copy print ?
  1. <input id="sp_ids" type="hidden" value="${sp_ids }">  

通过一个隐藏域来保存,这样就可以用这个id值了

你可能感兴趣的:(spring)