`
modun
  • 浏览: 149910 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring MVC 3 Action 支持的参数

阅读更多

请求类型

Handler methods that are annotated with @RequestMapping can have very flexible signatures. They may have arguments of the following types, in arbitrary order (except for validation results, which need to follow right after the corresponding command object, if desired):

  • Request or response objects (Servlet API). Choose any specific request or response type, for example ServletRequest orHttpServletRequest.

  • Session object (Servlet API): of type HttpSession. An argument of this type enforces the presence of a corresponding session. As a consequence, such an argument is never null.

    Note
    [Note]

    Session access may not be thread-safe, in particular in a Servlet environment. Consider setting theAnnotationMethodHandlerAdapter's "synchronizeOnSession" flag to "true" if multiple requests are allowed to access a session concurrently.

  • org.springframework.web.context.request.WebRequest or org.springframework.web.context.request.NativeWebRequest. Allows for generic request parameter access as well as request/session attribute access, without ties to the native Servlet/Portlet API.

  • java.util.Locale for the current request locale, determined by the most specific locale resolver available, in effect, the configured LocaleResolver in a Servlet environment.

  • java.io.InputStream / java.io.Reader for access to the request's content. This value is the raw InputStream/Reader as exposed by the Servlet API.

  • java.io.OutputStream / java.io.Writer for generating the response's content. This value is the raw OutputStream/Writer as exposed by the Servlet API.

  • @PathVariable annotated parameters for access to URI template variables. See Section 15.3.2.1, “URI Templates”.

  • @RequestParam annotated parameters for access to specific Servlet request parameters. Parameter values are converted to the declared method argument type. See Section 15.3.2.4, “Binding request parameters to method parameters with @RequestParam”.

  • @RequestHeader annotated parameters for access to specific Servlet request HTTP headers. Parameter values are converted to the declared method argument type.

  • @RequestBody annotated parameters for access to the HTTP request body. Parameter values are converted to the declared method argument type using HttpMessageConverters. See Section 15.3.2.5, “Mapping the request body with the @RequestBody annotation”.

  • HttpEntity<?> parameters for access to the Servlet request HTTP headers and contents. The request stream will be converted to the entity body using HttpMessageConverters. See Section 15.3.2.7, “Using HttpEntity<?>”.

  • java.util.Map / org.springframework.ui.Model / org.springframework.ui.ModelMap for enriching the implicit model that is exposed to the web view.

  • Command or form objects to bind parameters to: as bean properties or fields, with customizable type conversion, depending on@InitBinder methods and/or the HandlerAdapter configuration. See the webBindingInitializer property onAnnotationMethodHandlerAdapter. Such command objects along with their validation results will be exposed as model attributes by default, using the non-qualified command class name in property notation. For example, "orderAddress" for type "mypackage.OrderAddress". Specify a parameter-level ModelAttribute annotation for declaring a specific model attribute name.

  • org.springframework.validation.Errors / org.springframework.validation.BindingResult validation results for a preceding command or form object (the immediately preceding method argument).

  • org.springframework.web.bind.support.SessionStatus status handle for marking form processing as complete, which triggers the cleanup of session attributes that have been indicated by the @SessionAttributes annotation at the handler type level.

    返回类型:

    • ModelAndView object, with the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.

    • Model object, with the view name implicitly determined through a RequestToViewNameTranslator and the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.

    • Map object for exposing a model, with the view name implicitly determined through a RequestToViewNameTranslator and the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.

    • View object, with the model implicitly determined through command objects and @ModelAttribute annotated reference data accessor methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above).

    • String value that is interpreted as the logical view name, with the model implicitly determined through command objects and@ModelAttribute annotated reference data accessor methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above).

    • void if the method handles the response itself (by writing the response content directly, declaring an argument of typeServletResponse / HttpServletResponse for that purpose) or if the view name is supposed to be implicitly determined through aRequestToViewNameTranslator (not declaring a response argument in the handler method signature).

    • If the method is annotated with @ResponseBody, the return type is written to the response HTTP body. The return value will be converted to the declared method argument type using HttpMessageConverters. See Section 15.3.2.6, “Mapping the response body with the @ResponseBody annotation”.

    • HttpEntity<?> or ResponseEntity<?> object to access to the Servlet reponse HTTP headers and contents. The entity body will be converted to the response stream using HttpMessageConverters. See Section 15.3.2.7, “Using HttpEntity<?>”.

    • Any other return type is considered to be a single model attribute to be exposed to the view, using the attribute name specified through @ModelAttribute at the method level (or the default attribute name based on the return type class name). The model is implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.

分享到:
评论

相关推荐

    Spring MVC 教程快速入门 深入分析

    十三、如何给Spring3 MVC中的Action做JUnit单元测试:说明了如何对Spring MVC中的控制器进行单元测试,包括配置测试环境和编写测试代码。 十四、Spring MVC转发与重定向:详细讲解了在Spring MVC中如何使用转发和...

    spring in action mvc 第四版 中文版

    《Spring in Action: Spring MVC 第四版 中文版》是一本深入探讨Spring MVC框架的权威指南,适合Java开发者阅读。本书全面介绍了如何使用Spring MVC来构建健壮、高性能的Web应用程序。Spring MVC是Spring框架的核心...

    Spring MVC学习指南

    ### Spring MVC 学习指南详解 #### 一、Spring MVC 概述 Spring MVC 是 Spring 框架的一部分,主要用于构建基于 Model-View-Controller (MVC) 设计模式的 Web 应用程序。它提供了丰富的功能来简化 Web 开发过程,...

    Spring MVC 入门实例

    如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring MVC 的基本概念. 基于 Spring 的 Web 应用...

    Spring MVC入门教程

    十三、如何给spring3 MVC中的Action做JUnit单元测试? 十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八...

    Spring MVC 学习记录总结1

    3. Spring MVC 工作流程 - 客户端发送请求到达DispatcherServlet。 - DispatcherServlet使用HandlerMapping找到对应的Handler。 - HandlerMapping返回HandlerExecutionChain,包括Handler和可能的...

    Spring MVC 教程 快速入门 深入分析

    十三、如何给spring3 MVC中的Action做JUnit单元测试? 十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八...

    java spring mvc项目,通过URL地址传递对象参数给action.zip

    提供的`Spring MVC中如何传递对象参数 - @ 小浩 - 博客园.url`链接可能指向一篇详细的博客文章,介绍了更多关于使用`@ModelAttribute`和对象参数的实践技巧和注意事项。 总结来说,通过Spring MVC的`@...

    Spring3 MVC 基础学习手册

    Spring3 MVC 是一款基于Java的轻量级Web应用框架,它是Spring框架的一部分,主要用于构建Web应用程序的模型-视图-控制器(MVC)结构。在本文中,我们将深入探讨Spring3 MVC的基础配置、注解使用以及核心概念。 一、...

    spring mvc文件上传实现进度条

    在Spring MVC框架中,文件上传是一项常见的功能,而实现文件上传进度条则能提供更好的用户体验。这个场景通常涉及到前端的JavaScript或jQuery库(如jQuery File Upload)与后端的Spring MVC控制器之间的交互,以及...

    Spring_MVC_实战教程

    在Spring MVC中,这个过程通常是通过`@RequestParam`注解或模型绑定自动完成的,将请求参数映射到控制器方法的参数上。 2. **请求调度**:当请求到达服务器,Spring MVC的`DispatcherServlet`作为前端控制器,负责...

    struts1原理,struts2原理,spring mvc原理,struts1和struts2,struts和spring mvc

    3. **测试**:Spring MVC更容易进行单元测试,因为Controller通常是无状态的,而Struts2的Action有状态,测试相对复杂。 4. **社区支持**:虽然两者都有活跃的社区,但Spring MVC由于Spring框架的广泛使用,社区支持...

    spring MVC 实例代码

    基于注解的spring mvc,dao 层注解:@Repository("userDao"), entity层注解:@Entity,service层注解:@Service("userService"), action层注解:@Controller("userController") @RequestMapping("/user.do")

    spring mvc 教程

    3. **与JSF整合**:通过适配层,可以将JSF组件集成到Spring Web Flow中,从而充分利用JSF丰富的组件库。 4. **与Spring Security整合**:安全性的增强是Spring Web Flow 2.0的重要特点之一,通过简单的配置即可实现...

    Spring MVC的总结梳理

    3. **高度的适应性和非侵入性**:Spring MVC提供了多种控制器子类,如Simple型、Command型、Form型、Wizard型、Multi-Action型等,可以根据具体的应用场景灵活选择。 4. **业务代码的重用**:开发者可以直接使用现有...

    Spring MVC 教程,快速入门,深入分析

    转发与重定向是Web开发中常用的两种请求处理方式,Spring MVC提供了简单的方法来实现带参数的重定向。 处理Ajax请求也是Spring MVC的强大功能之一,通过注解和配置可以方便地与前端进行异步交互。配置文件的编写在...

    struts VS spring mvc

    - **视图层**:同样采用 JSP 或其他视图技术,Spring MVC 支持多种视图技术,如 Thymeleaf、Freemarker 等。 - **模型层**:与 Struts 类似,模型层通常封装业务逻辑和数据访问。Spring MVC 可以轻松地与 Spring ...

    spring mvc 上传文件显示进度

    Spring不直接支持此功能,但我们可以借助第三方库如Apache Commons FileUpload或Servlet 3.0的ProgressListener。 ```java @RestController public class UploadController { @PostMapping("/upload") public...

    Unit-testing Spring MVC--by Spring In Action Author

    文章由Spring in Action的作者撰写,详细探讨了Spring MVC在Spring 2.5版本中的可测试性,并提供了一个实际的例子——`HomePageController`,演示了如何使用注解来简化配置并提高可测试性。 #### Spring MVC的可...

    Spring MVC总结.doc

    Spring MVC支持多种视图技术,如JSP、FreeMarker、Thymeleaf等,可以通过配置`ViewResolver`来选择不同的视图解析策略。这允许开发者根据项目需求选择最适合的视图技术。 7. **控制器和命令对象** 在Spring MVC中...

Global site tag (gtag.js) - Google Analytics