spring MVC model

简介:

使用spring MVC的人都知道,控制器是通过Model把数据传到view层的.那么它们具体是通过什么来定位传递的数据呢?

比如控制器中的一个方法:

Java代码   收藏代码
  1. @RequestMapping(value = "/add",method=RequestMethod.GET)  
  2.     public String addInputNews(String practiceWay, Model model) {  
  3.         commonAction(model);  
  4.         List<RoleLevel> roles=this.roleLevelDao.getAll();  
  5.         model.addAttribute("roles",roles);//选择上级  
  6.         model.addAttribute(new RoleLevel());  
  7.         return jspFolder+"/add";  
  8.     }  

 在view层可以通过${roles}来获取数据,即view层通过model中的key进行获取.

那么问题来了,如果是model.addAttribute(roles);//选择上级 呢?没有key?!

经过查spring MVC官方资料,view层可以通过${roleLevelList } 来获取数据.

具体是什么依据呢?

以下是官方资料:

17.12.2 The Model ModelMap (ModelAndView)

The ModelMap class is essentially a glorified Map that can make adding objects that are to be displayed in (or on) a View adhere to a common naming convention. Consider the following Controller implementation; notice that objects are added to the ModelAndView without any associated name specified.

public class DisplayShoppingCartController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {

        List cartItems = // get a List of CartItem objects
        User user = // get the User doing the shopping

        ModelAndView mav = new ModelAndView("displayShoppingCart"); <-- the logical view name

        mav.addObject(cartItems); <-- look ma, no name, just the object
        mav.addObject(user); <-- and again ma!

        return mav;
    }
}

The ModelAndView class uses a ModelMap class that is a custom Map implementation that automatically generates a key for an object when an object is added to it. The strategy for determining the name for an added object is, in the case of a scalar object such as User, to use the short class name of the object's class. The following examples are names that are generated for scalar objects put into a ModelMap instance.

  • An x.y.User instance added will have the name user generated.

  • An x.y.Registration instance added will have the name registration generated.

  • An x.y.Foo instance added will have the name foo generated.

  • Java.util.HashMap instance added will have the name hashMap generated. You probably want to be explicit about the name in this case because hashMap is less than intuitive.

  • Adding null will result in an IllegalArgumentException being thrown. If the object (or objects) that you are adding could be null, then you will also want to be explicit about the name.

The strategy for generating a name after adding a Set or a List is to peek into the collection, take the short class name of the first object in the collection, and use that with List appended to the name. The same applies to arrays although with arrays it is not necessary to peek into the array contents. A few examples will make the semantics of name generation for collections clearer:

  • An x.y.User[] array with zero or more x.y.User elements added will have the name userListgenerated.

  • An x.y.Foo[] array with zero or more x.y.User elements added will have the name fooListgenerated.

  • java.util.ArrayList with one or more x.y.User elements added will have the name userListgenerated.

  • java.util.HashSet with one or more x.y.Foo elements added will have the name fooList generated.

  • An empty java.util.ArrayList will not be added at all (in effect, the addObject(..) call will essentially be a no-op).

相关文章
|
6天前
|
设计模式 前端开发 Java
初识Spring MVC
初识Spring MVC
11 0
|
6天前
|
前端开发 Java 应用服务中间件
Spring MVC框架概述
Spring MVC 是一个基于Java的轻量级Web框架,采用MVC设计模型实现请求驱动的松耦合应用开发。框架包括DispatcherServlet、HandlerMapping、Handler、HandlerAdapter、ViewResolver核心组件。DispatcherServlet协调这些组件处理HTTP请求和响应,Controller处理业务逻辑,Model封装数据,View负责渲染。通过注解@Controller、@RequestMapping等简化开发,支持RESTful请求。Spring MVC具有清晰的角色分配、Spring框架集成、多种视图技术支持以及异常处理等优点。
12 1
|
8天前
|
监控 前端开发 Java
SpringBoot与SpringMVC有哪些区别?
SpringBoot和SpringMVC是Java开发中常用的两个框架,它们都是由Spring框架所提供的,但在功能和使用方式上有着一些区别。
23 2
|
1月前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
23 3
|
1月前
|
存储 前端开发 Java
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
14 1
|
1月前
|
前端开发 Java Spring
数据之桥:深入Spring MVC中传递数据给视图的实用指南
数据之桥:深入Spring MVC中传递数据给视图的实用指南
34 3
|
1月前
|
前端开发 Java 容器
家族传承:Spring MVC中父子容器的搭建与管理指南
家族传承:Spring MVC中父子容器的搭建与管理指南
26 3
|
1月前
|
前端开发 Java API
头头是道:揭示Spring MVC如何获取和处理请求头数据
头头是道:揭示Spring MVC如何获取和处理请求头数据
26 1
|
1月前
|
前端开发 Java API
饼干探秘:深入Spring MVC中获取Cookie数据的技术解析
饼干探秘:深入Spring MVC中获取Cookie数据的技术解析
18 3
|
1月前
|
前端开发 Java Spring
转换之术:解析Spring MVC中类型转换器的实际运用
转换之术:解析Spring MVC中类型转换器的实际运用
22 0