Supported Method Argument Annotations
All these different method argument types and annotations allow us to write very flexible request handling methods.
RequestParam
The RequestParam annotation can be placed on any argument in a request-handling method. When
present, it is used to retrieve a parameter from the request. When put on a Map, there is some special
handling, depending on whether the name attribute is set. If the name is set, the value is retrieved and
converted into a Map. For conversion, if no name is given, all request parameters are added to the map as key/value pairs.
RequestHeader
The RequestHeader annotation can be placed on any method argument. It is used to bind a method
argument to a request header. When placed on a Map, all available request headers are put in the map as key/value pairs. If it is placed on another type of argument, then the value is converted into the type by using a org.springframework.core.convert.converter.Converter or PropertyEditor.
RequestBody
The RequestBody annotation is used to mark a method parameter we want to bind to the body of the web request. The body is converted into the method parameter type by locating and calling an
org.springframework.http.converter.HttpMessageConverter. This converter is selected based on the
requests content-type. If no converter is found, an
org.springframework.web.HttpMediaTypeNotSupportedException is thrown. By default, this leads to a
response with code 415 (SC_UNSUPPORTED_MEDIA_TYPE) being send to the client.
Optionally, method parameters can also be annotated with javax.validation.Valid or
org.springframework.validation.annotation.Validated to enforce validation for the created object.
RequestPart
When the RequestPart annotation is put on a method argument of the type javax.servlet.http.Part,
org.springframework.web.multipart.MultipartFile (or on a collection or array of the latter,) then we
will get the content of that file (or group of files) injected. If it is put on any other argument type, the
content is passed through an org.springframework.http.converter.HttpMessageConverter for the
content type detected on the file. If no suitable converter is found, then an
org.springframework.web.HttpMediaTypeNotSupportedException is thrown.
ModelAttribute
The ModelAttribute annotation can be placed on method arguments, as well as on methods. When
placed on a method argument, it is used to bind this argument to a model object. When placed on a
method, that method is used to construct a model object, and this method will be called before request handling methods are called. These kinds of methods can be used to create an object to be edited in a form or to supply data needed by a form to render itself.
PathVariable
The PathVariable annotation can be used in conjunction with path variables. Path variables can be used in a URL pattern to bind the URL to a variable. Path variables are denoted as {name}in our URL mapping. If we were to use a URL mapping of /book/{isbn}/image, then isbn would be available as a path variable.
CookieValue
This CookieValue annotation can be placed on any argument in the request-handling method. When
present, it is used to retrieve a cookie. When placed on an argument of type javax.servlet.http.Cookie,
we get the complete cookie. Otherwise, the value of the cookie is converted into the argument type.
Supported Method Return Values
When an arbitrary object is returned and there is no ModelAttribute annotation present, the
framework tries to determine a name to use as the name for the object in the model. It basically takes the simple name of the class (the classname without the package) and lowercases the first letter. For
example, the name of our com.apress.prospringmvc.bookstore.domain.Book becomes book. When the
return type is a collection or array, it becomes the simple name of the class, suffixed with List. Thus a
collection of Book objects becomes bookList.
This same logic is applied when we use a Model or ModelMap to add objects without an explicit name.
This also has the advantage of using the specific objects, instead of a plain Map to gain access to the
underlying implicit model.
相关推荐
Pro Spring MVC provides in-depth coverage of Spring MVC and Spring Web Flow, two highly customizable and powerful web frameworks brought to you by the developers and community of the Spring Framework....
Being part of the Spring Framework, it naturally extended and supported it with an amazing set of recognizable annotations. External libraries can be plugged in and plugged out. It also possesses a ...
Being part of the Spring Framework, it naturally extended and supported it with an amazing set of recognizable annotations. External libraries can be plugged in and plugged out. It also possesses a ...
除了这些基础的注解,Spring MVC还提供了许多其他注解,如`@RequestParam`用于从请求参数中获取值,`@CookieValue`用于读取cookie,`@ModelAttribute`用于绑定表单数据到模型对象等。这些注解极大地增强了控制器处理...
然后,你需要在`DispatcherServlet`的配置中添加`<async-supported>`元素并将其值设为`true`,这样Spring MVC就会启用异步处理: ```xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:...
At present, cable supported bridges are enabled for spans in the range from 200 m to 2000 m (and beyond), thus covering approximately 90 per cent of the present span range. For the vast majority of ...
Note Because Visual Studio 2008 and Visual Studio 2010 Beta 2 share a component of ASP.NET MVC 2, installing the ASP.NET MVC 2 Release Candidate release on a computer where Visual Studio 2010 ...
4. **MVC框架配置**:如果你使用了Spring MVC、Struts2等框架,检查控制器(Controller)配置,确保有处理GET请求的方法。 针对给定的压缩包文件名"删除servlet中doGet()中的super.doGet(req, resp);即可.txt",这...
11.3.1. The @RestController and @RequestMapping Annotations 11.3.2. The @EnableAutoConfiguration Annotation 11.3.3. The “main” Method 11.4. Running the Example 11.5. Creating an Executable Jar 12. ...
在IT行业中,Spring框架及其衍生技术,如Spring MVC,是企业级应用开发的基石。Spring以其灵活的依赖注入和强大的事务管理能力深受开发者喜爱。在本文中,我们将深入探讨Spring、Spring MVC以及与之相关的Webservice...
因此,Spring MVC无法自动将请求体解析为JSON格式,并映射到对应的Java对象上。 解决这个问题的方法通常是确保客户端发送的请求Content-Type设置为`application/json`,并且发送的请求体是有效的JSON格式。在服务器...
"inspinia_admin2.7 asp.net MVC5" 是一个基于ASP.NET MVC5框架的响应式后台管理模板,主要用于构建高效、现代化的企业级Web应用。Inspinia Admin是一款流行的前端框架,它提供了丰富的功能和组件,使得开发者可以...
Spring Web MVC支持异步请求处理,通过@Controller和@RequestMapping注解的async-supported属性以及AsyncConfigurer接口来启用。 10. **RESTful支持**: 基于HTTP协议的RESTful风格的API在现代Web开发中广泛使用...
In order to use the thymeleaf-extras-springsecurity3 or thymeleaf-extras-springsecurity4 modules in our Spring MVC application, we will first need to configure our application in the usual way for ...
在使用spring cloud feign时,我们可能会遇到org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported的报错,这是因为feign client中的RequestMethod.GET或@...
在`src/main/resources`目录下创建`springsMVC.xml`和`spring.xml`文件,分别用于配置Spring MVC和Spring容器。 ##### 1. springMVC.xml配置 ```xml <beans xmlns="http://www.springframework.org/schema/beans" ...
The device must have a point-ing ap¬para¬tus or method (such as a stylus, or a finger touching a touch pad), called the cursor, that de¬fines the current position. The cursor must be able to ...
For environments where class instrumentation is required but are not supported by the existing LoadTimeWeaver implementations, a JDK agent can be the only solution. For such cases, Spring provides ...
在Spring Boot应用中实现中英文语言切换是一项常见的需求,它能提供多语言支持,使得应用程序可以适应不同国家和地区的用户。下面将详细讲解如何在Spring Boot项目中搭建一个支持中英文切换的功能,以及如何结合...
Request Method Not Supported(处理方案).md