spring mvc ModelAndView向前台传值

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

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

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

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

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

@RequestMapping("/list")  
public ModelAndView list(HttpServletRequest request)  
    throws Exception  
{  
    Map<String, Object> context = getRootMap();  
    StudentModel model = new StudentModel();  
    context.put("model", model);  
text.put("sp_ids", id);  
    return forword("stu/studentList", context);  
}  

这里,页面将跳转到studentList.jsp页面,id值也会传到前台去。

前台代码如下:

<input id="sp_ids" type="hidden" value="${sp_ids }">  

 

posted @ 2016-01-04 14:35  星辰之力  阅读(2092)  评论(0编辑  收藏  举报