使用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 nameuser
generated. -
An
x.y.Registration
instance added will have the nameregistration
generated. -
An
x.y.Foo
instance added will have the namefoo
generated. -
A
java.util.HashMap
instance added will have the namehashMap
generated. You probably want to be explicit about the name in this case becausehashMap
is less than intuitive. -
Adding
null
will result in anIllegalArgumentException
being thrown. If the object (or objects) that you are adding could benull
, 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 morex.y.User
elements added will have the nameuserList
generated. -
An
x.y.Foo[]
array with zero or morex.y.User
elements added will have the namefooList
generated. -
A
java.util.ArrayList
with one or morex.y.User
elements added will have the nameuserList
generated. -
A
java.util.HashSet
with one or morex.y.Foo
elements added will have the namefooList
generated. -
An empty
java.util.ArrayList
will not be added at all (in effect, theaddObject(..)
call will essentially be a no-op).
相关推荐
Spring MVC 是一个基于Java的轻量级Web应用框架,它为开发者提供了模型-视图-控制器(MVC)架构,使开发人员能够更好地组织和分离应用程序的业务逻辑、数据处理和用户界面。Spring MVC是Spring框架的一个核心组件,...
Spring MVC 是一个基于Java的轻量级Web应用框架,它为构建模型-视图-控制器(Model-View-Controller)架构的应用程序提供了强有力的支持。在"Spring-MVC-model(1)"这个主题中,我们将深入探讨Spring MVC框架中的...
首先,Spring MVC的基础架构包括DispatcherServlet(前端控制器)、Model、View和Controller。DispatcherServlet是整个流程的入口,负责接收请求并分发到相应的Controller。Controller是业务逻辑处理的核心,Model...
Spring MVC是Spring框架的一个核心模块,专为构建Web应用程序而设计。它提供了模型-视图-控制器(MVC)架构,使开发者能够有效地分离业务逻辑、数据处理和用户界面。在"Spring MVC 4.2.3"版本中,我们看到了一系列的...
首先,Spring MVC的核心设计理念是模型-视图-控制器(Model-View-Controller)架构模式。在该模式中,模型负责业务逻辑处理,视图负责展示数据,而控制器则作为模型和视图之间的桥梁,接收用户请求并调用相应的服务...
模型(Model)和视图(View)是MVC模式中的另外两个关键部分。模型持有业务数据,视图则负责呈现这些数据。Spring MVC支持多种视图技术,如JSP、FreeMarker或Thymeleaf,开发者可以根据项目需求选择合适的视图解析器...
Spring MVC通过分离模型(Model)、视图(View)和控制器(Controller)来简化Web开发。以下将详细分析Spring MVC的核心知识点。 一、前言:介绍了选择Spring MVC的原因和优势。Spring MVC简单易用,可以快速提高...
在Spring MVC中,Controller负责处理HTTP请求,Model持有业务数据,而View则负责数据的展示。通过DispatcherServlet作为前端控制器,Spring MVC能够灵活地调度请求到相应的处理器,并且支持多种视图技术如JSP、...
首先,Spring MVC的设计模式基于Model-View-Controller(MVC),它将应用程序的业务逻辑、数据和用户界面进行了分离,使得代码更加清晰、易于维护。在Spring MVC中,Controller处理用户的请求,Model存储数据,而...
Spring MVC 是一个基于Java的轻量级Web应用框架,它是Spring框架的重要组成部分,主要用于构建Web应用程序的后端控制器。Spring MVC的设计目标是提供一个清晰的组件化架构,使得开发者可以独立地开发和测试控制器、...
在Spring MVC中,Model代表业务对象,View负责展示,Controller处理用户请求并协调Model和View。 2. **DispatcherServlet**:Spring MVC的入口点,它是一个前端控制器,接收所有HTTP请求,并根据配置的...
- Spring MVC是基于Model-View-Controller(MVC)设计模式的Web应用框架,提供了一种组织和处理请求的机制。 - 它的核心组件包括DispatcherServlet、HandlerMapping、HandlerAdapter、ModelAndView和ViewResolver...
Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...
Spring MVC的核心概念包括DispatcherServlet、Controller、Model、View和ViewModel。以下将详细阐述这些关键知识点: 1. **DispatcherServlet**:这是Spring MVC的前端控制器,负责接收HTTP请求,解析请求参数,...
- **MVC模式**:Model代表业务数据,View负责渲染视图,Controller处理用户请求并协调Model和View。 2. **Spring Security**: - **认证与授权**:Spring Security提供了一套完整的认证和授权机制。在这个无...
model.addAttribute("message", "Hello, Spring MVC!"); return "hello"; } } ``` - `hello.jsp`:对应的视图,显示消息: ```jsp ${message} ``` 5. **运行与测试** 将项目部署到Tomcat或其他...