`
阅读更多

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

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

@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String addInputNews(String practiceWay, Model model) {
		commonAction(model);
		List<RoleLevel> roles=this.roleLevelDao.getAll();
		model.addAttribute("roles",roles);//选择上级
		model.addAttribute(new RoleLevel());
		return jspFolder+"/add";
	}

 在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).

0
1
分享到:
评论

相关推荐

    Spring-MVC-model(1)

    Spring-MVC-model(1) Spring-MVC-model(1) Spring-MVC-model(1)

    [免费]Spring MVC学习指南(高清)

    Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...

    大优惠 Spring MVC学习指南(第2版)2017.pdf

    Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...

    Spring-MVC-model

    Spring-MVC-model Spring-MVC-model Spring-MVC-model Spring-MVC-model

    Spring MVC, A Tutorial, second edition 【2016】

    The MVC in Spring MVC stands for Model-View-Controller, a design pattern widely used in Graphical User Interface (GUI) development. This pattern is not only common in web development, but is also ...

    Spring MVC学习指南

    以下这个学习案例是我最近学习Spring MVC时跟从一本 书上的示例,原文中的示例代码有一些小错误,不过我 在调试的过程中已经给予了修正,如还有其它错误,还 请各位批评指正。 对于现有较成熟的Model-View-...

    Spring.MVC-A.Tutorial-Spring.MVC学习指南

    Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界主流的Web开发框架,Spring MVC已经成为当前热门的开发技能,同时也广泛用于桌面开发领域。  ...

    全面掌握Spring MVC:从基础到高级的实践指南

    Spring MVC基于Model-View-Controller(MVC)架构模式,优化了Web应用程序的设计和开发。在Spring MVC中,DispatcherServlet作为前端控制器,负责请求的接收和响应结果的处理。处理器映射器(HandlerMapping)和...

    Spring.MVC.A.Tutorial.2nd.Edition.1771970316

    The MVC in Spring MVC stands for Model-View-Controller, a design pattern widely used in Graphical User Interface (GUI) development. This pattern is not only common in web development, but is also ...

    Spring MVC 3.0实战指南.ppt

    《Spring MVC 3.0实战指南》,参考《Spring 3.x企业应用开发实战》。 内容简介: 1、Spring MVC框架简介 2、HTTP请求地址映射 3、HTTP请求数据的绑定 4、数据转换、格式化、校验 5、数据模型控制 6、视图及解析器 7...

    Spring.MVC-A.Tutorial-Spring.MVC学习指南.rar

    Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...

    21道Java Spring MVC综合面试题详解含答案(值得珍藏)

    Spring MVC是Spring框架中的一部分,全称是Spring Web MVC,主要用于实现MVC设计模式的Web框架。它分离了控制器、模型对象、过滤器以及处理程序对象的角色,使得它们更容易进行定制。 Spring MVC的优点包括: 基于...

    Spring MVC学习指南 第2版 高清版

    Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...

    Spring MVC Beginner-s Guide.pdf

    Spring MVC is a model-view-controller framework for Java web applications to simplify the writing and testing of Java web applications, which fully integrates with the Spring dependency injection ...

    spring mvc 3.2 参考文档

    Spring Web model-view-controller (MVC)框架是围绕 DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view resolution)、 区域设置...

    使用Spring MVC和JSP构建的全栈RSS阅读器Web应用程序(95分以上课程大作业).zip

    它采用MVC(Model-View-Controller,模型-视图-控制器)的架构模式,将应用程序分为模型层、视图层和控制器层,提供了处理请求、渲染视图和管理流程的功能。 3. MyBatis框架:MyBatis是一个持久层框架,用于与数据库...

Global site tag (gtag.js) - Google Analytics