lt;lt;Pro Spring MVC: With Web Flowgt;gt; 115—124页
作者:Colin Yates; Seth Ladd; Marten Deinum
Supported Method Argument Types
A request-handling method can have various method arguments and return values. Most arguments
mentioned in Table 5-4 can be used in arbitrary order. However, there is a single exception to that rule:
the org.springframework.validation.BindingResult argument. That argument has to follow a model
object that we use to bind request parameters to.
Table 5-4. The Supported Method Argument Types
Argument Type |
Description |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
javax.servlet.ServletRequest |
The request object that triggered this method. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
javax.servlet.http. HttpServletRequest |
The HTTP request object that triggered this method. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
org.springframework.web.multipart. MultipartRequest |
The request object that triggered this method only works for multipart requests. This wrapper allows for easy access to the uploaded files(s). Only exposes methods for multipart file access. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
org.springframework.web.multipart. MultipartHttpServletRequest |
The MultipartHttpServletRequest exposes both the HttpServletRequest and MultipartRequest methods. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
javax.servlet.ServletResponse |
The response associated with the request. This is useful if we need to write the response ourselves. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
javax.servlet.http. HttpServletResponse |
The response associated with the request. This is useful if we need to write the response ourselves. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
javax.servlet.http.HttpSession |
The underlying HttpSession. If no session exists, one will be initiated. This argument is therefore never null. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
org.springframework.web.context. request.WebRequest |
Allows for more generic access to request and session attributes without ties to an underlying native API (e.g., Servlet, Portlet, or JSF). |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
org.springframework.web.context. request.NativeWebRequest |
WebRequest extension that has accessor methods for the underlying request and response. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
java.util.Locale |
The currently selected locale as determined by the configured org.springframework.web.servlet .LocaleResolver. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
java.io.InputStream |
The stream as exposed by the getInputStream method on the ServletRequest |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
java.io.Reader |
The reader as exposed by the getReader method on the ServletRequest. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
java.io.OutputStream |
The responses stream as exposed by the getOutputStream method on the ServletResponse. This can be used to write a response directly to the user. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
java.io.Writer |
The responses writer as exposed by the getWriter method on the ServletResponse. This can be used to write a response directly to the user. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
javax.security.Principal |
The currently authenticated user (can be null). |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
java.util.Map |
The implicit model belonging to this controller/request. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
org.springframework.ui.Model |
The implicit model belonging to this controller/request. Model implementations have methods to add objects to the model for added convenience. When adding objects allows method chaining as each method returns the Model. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
org.springframework.ui.ModelMap |
The implicit model belonging to this controller/request. The ModelMap is a Map implementation that includes some methods to add objects to the model for added convenience. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
org.springframework.web.multipart. MultipartFile |
Binds the uploaded file(s) to a method parameter (multiple files are only supported by the multipart support of Spring). This works only when the request is a multipart form submission. The name of the request attribute to use is either taken from an optional org.springframework.web .bind.annotation.RequestPart annotation or derived from the name of the argument (the latter works only if that information is available in the class). |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
javax.servlet.http.Part |
Binds the uploaded file(s) to a method parameter (multiple files are only supported by the multipart support of Spring). This works only when the request is a multipart form submission. The name of the request attribute to use is either taken from an optional org.springframework.web .bind.annotation.RequestPart annotation or derived from the name of the argument (the latter works only if that information is available in the class). |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
org.springframework.web.servlet.mvc. support.RedirectAttributes |
Enables specification of the exact list of attributes in case we want to issue a redirect. This can also be used to add flash attributes. This argument is used instead of the implicit model in the case of a redirect. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
org.springframework.validation. Errors |
The binding and validation results for a preceding model object. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
org.springframework.validation. BindingResult |
The binding and validation results for a preceding model object. Has accessor methods for the model and underlying infrastructure for type conversion. (For most usecases this isnrsquo;t needed, use Errors instead). |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<p 剩余内容已隐藏,支付完成后下载完整资料
lt;lt;Pro Spring MVC: With Web Flowgt;gt; 115—124页 作者:Colin Yates; Seth Ladd; Marten Deinum 支持的方法参数类型 一个请求处理方法可以有各种方法参数和返回值。大多数在表5-4中提到的参数可以任意顺序使用。然而,这个规则有一个例外,org.springframework.validation.BindingResult参数。该参数必须伴随一个用于绑定请求参数的model对象。 表5-4。支持的方法参数类型。
RedirectAttributes org.springframework.web.servlet.mvc.support.RedirectAttributes需要多一些的解释比表5-4所示。RedirectAttribute,可以精确声明哪些属性需要重定向。默认情况下,所有模型属性暴露在做重定向时。因为重定向总是引导一个GET请求,所有原始模型属性(或属性集合/数组的基本类型)将编码作为请求参数。然而,与注释控制器,也有不需要暴露的超出我们控制的对象在模型中(像其他路径变量和隐式值)。 RedirectAttributes可以帮助我们。当这是用作方法参数或一个重定向时,只有添加到RedirectAttributes实例的属性将被添加到URL中。 除了在URL中指定属性编码,也可以指定所谓的临时属性。临时属性是在重定向之前存储和重定向后检索作为模型属性的属性。这是通过配置FlashMapManager。为不能被编码或保持URL干净的对象使用临时属性是有用的。 UriComponentsBuilder UriComponentsBuilder提供一种建立和编码URL的机制。它可以把一个URL模式和替换或拓展变量。这可以针对相对和绝对URL。这种机制在创建URL时尤为有用,而不是当我们需要考虑编码参数或做字符串连接的情况。该组件以一致的方式为我们处理所有这些事情。清单5-6中的代码创建了一个“/book/detail/42”这样的URL。 清单5-6。UriComponentsBuilder示例代码。 UriComponentsBuilder.fromPath('/book/detail/{bookId}'); .build(); .expand('42') .encode() 给出的示例非常简单;但是,可以指定更多的变量(例如,bookId)和替换它们(如,指定port或host)。还有ServletUriComponentsBuilder子类,我们可以使用它来运行当前的请求。例如,我们可以用它来取代,不仅是路径变量,而且有请求参数。 支持的方法参数注解 除了明确支持的类型(如前一节中提到的),也有一对注解,我们可以用它来注释我们的方法的参数(见表5-5)注释。其中的一些还可以与表5-4中提到的方法参数类型使用。在这种情况下,它们被用来指定request、cookie、header、或者response中的属性的名称,以及是否需要该参数。 所有的参数值是使用类型转换转换为参数类型。该类型转换系统使用org.springframework.core.convert.converter.Converter或PropertyEditor将字符串类型转换为实际的类型。 表5-5.支持的方法参数注解
注:所有注解都在org.springframework.web.bind.annotation包。 所有这些不同的方法参数类型进而注解使我们可以写非常灵活的请求处理方法。然而,我们可以通过延伸的框架延长这一机制。通过实现HandlerMethodArgumentResolver来解决方法参数类型。清单5-7显示了接口。如果我们需要,我们可以自己创建这个接口的实现并注册到框架中。你可以在第7章找到更多相关信息。 清单5-7。HandlerMethodArgumentResolver的接口。 package org.springframework.web.method.support; import org.springframework.core.MethodParameter; import org.springframework.web. 剩余内容已隐藏,支付完成后下载完整资料 资料编号:[28800],资料为PDF文档或Word文档,PDF文档可免费转换为Word </p |
课题毕业论文、外文翻译、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。