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);
	context.put("sp_ids", id);
        return forword("stu/studentList", context);
    }



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

前台代码例如以下:

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

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

原文地址:https://www.cnblogs.com/mfrbuaa/p/4280540.html