SpringMVC传递模型数据到视图

时间:2022-07-22
本文章向大家介绍SpringMVC传递模型数据到视图,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、ModelAndView

要求:处理方法返回值类型为 ModelAndView。在方法体中我们通过该ModelAndView对象添加模型数据。

2、Model/Map/ModelMap

要求:使用org.springframework.ui.Model、org.springframework.ui.ModelMap 或 Java.uti.Map 作为处理方法的入參时,当处理方法返回时,Map中的数据会自动添加到模型中,具体实例将在后面介绍。

3、@SessionAttributes

使用该注解来注解某个类,使得将模型中的某个属性暂存到HttpSession 中,以便多个请求之间可以共享这个属性。

4、@ModelAttribute

该注解即可注解在有返回值的方法上,无返回值的方法上,还可以注解在方法入参上,当入參标注该注解后, 入参的对象就会放到数据模型中,具体将在后面进行介绍。

ModelAndView

  1. 创建ModelAndView并传入数据
@Controller
    public class HelloController {
 
        @RequestMapping(value = "/testModelAndView.do",method = RequestMethod.GET)
        public ModelAndView testModelAndView(){
            String viewName = "hello";//视图名
            ModelAndView modelAndView = new ModelAndView(viewName);
 
            modelAndView.addObject("time",new Date());
            modelAndView.addObject("name","ModelAndView");
            return modelAndView;
        }
    }

在ModelAndView中添加视图名,使用addObject添加数据

  1. 编写Jsp,获取数据
<html>
    <body>
    <h2>method:${requestScope.name}</h2>
 
    ${requestScope.time}<br>
 
    ${requestScope.get("time")}<br>
 
    ${time}
    </body>
    </html>

{requestScope.time},${time}这三种写法效果是一样的

Model/Map/ModelMap

Spring MVC 在调用方法前会创建一个隐含的模型对象作为模型数据的存储容器。

Model和Map使用上基本一样

@RequestMapping(value = "/testModel.do",method = RequestMethod.GET)
    public String testModel(Model model){
        model.addAttribute("time",new Date());
        model.addAttribute("name","Model");
        return "hello";
    }
 
    @RequestMapping(value = "/testMap.do",method = RequestMethod.GET)
    public String testMap(Map<String,Object> map){
        map.put("time",new Date());
        map.put("name","Map");
        return "hello";
    }

Jsp的代码与上面是一样的。

@SessionAttributes

若希望在多个请求之间共用某个模型属性数据,则可以在控制器类上标注一个 @SessionAttributes,Spring MVC将在模型中对应的属性暂存到 HttpSession 中。

注意:@SessionAttributes这个注解只能放到类的上面
@SessionAttributes({"name","time"})
    @Controller
    public class HelloController {
 
        @RequestMapping(value = "/testSessionAttribute.do",method = RequestMethod.GET)
        public String testSessionAttribute(Map<String,Object> map){
            map.put("time",new Date());
            map.put("name","SessionAttribute");
            return "sessionAttribute";
        }
}

jsp代码如下:

 <html>
    <body>
    <h2>method:${requestScope.name}</h2>
    ${requestScope.time}<br>
    ${requestScope.get("time")}<br>
    ${time} <br>
    sessionScope.time:${sessionScope.time}
    </body>
    </html>

@ModelAtrribute

可以用@ModelAttribute来注释方法参数:带有@ModelAttribute注解的方法会将其输入或创建的参数对象添加到Model对象中(若方法中没有显式添加)。

可以用@ModelAttribute标注一个非请求的处理方法(有返回值,无返回值):被@ModelAttribute注释的方法会在此controller每个方法执行前被执行。

@ModelAttribute
    public User addUser(User user){//
        user.setName("Model Attribute in Mehtod ,this method has return value");
        return user;
 
    }
    @RequestMapping(value = "/testModelAttributeInReturnValueMethod.do",method = RequestMethod.GET)
    public String testModelAttributeInReturnValueMethod(){
        return "modelAttributeInRVMethod";
    }

@ ModelAttribute注解在有返回值的方法上,则该返回值会被添加到模型对象中。 jsp如下所示:

  <html>
    <body>
 
    <h2>name:${user.name}</h2>
 
    </body>
    </html>