简体   繁体   English

Spring MVC中的WebRequest和HttpServletRequest

[英]WebRequest and HttpServletRequest in Spring MVC

What is the difference between the two? 两者有什么区别? Both have a getParameter method as well as setAttribute method, then where comes the difference between the two? 两者都有getParameter方法以及setAttribute方法,那么两者之间的差异在哪里呢?

1) Which one is better to use in general? 1)哪一个更好用?

2) Please explain specific scenarios where they can be used. 2)请解释可以使用它们的具体方案。

The javadoc of WebRequest is pretty clear on the subject: WebRequestjavadoc在这个主题上非常明确:

Generic interface for a web request. Web请求的通用接口。 Mainly intended for generic web request interceptors, giving them access to general request metadata, not for actual handling of the request. 主要用于通用Web请求拦截器,使其可以访问一般请求元数据,而不是实际处理请求。

(emphasis mine). (强调我的)。

The javadoc links to WebRequestInterceptor, which says: javadoc链接到WebRequestInterceptor,它说:

Interface for general web request interception. 一般Web请求拦截的接口。 Allows for being applied to Servlet request as well as Portlet request environments, by building on the WebRequest abstraction. 通过构建WebRequest抽象,允许应用于Servlet请求以及Portlet请求环境。

So, basically, you should not use WebRequest except in a WebRequestInterceptor. 所以,基本上,除了WebRequestInterceptor之外,你不应该使用WebRequest。 And they introduced this interface in order to be able to write interceptors that apply to servlets and portlets. 他们引入了这个接口,以便能够编写适用于servlet和portlet的拦截器。 Other than that, if you really need access to the request in Spring MVC controllers, you should use the standard HttpServletRequest. 除此之外,如果您真的需要访问Spring MVC控制器中的请求,则应使用标准的HttpServletRequest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何像使用HttpServletRequest一样从Spring MVC中的org.springframework.web.context.request.WebRequest中获取requestUrl? - How can I get the requestUrl from org.springframework.web.context.request.WebRequest in Spring MVC like we do using HttpServletRequest? 从HttpServletRequest投射到WebRequest - Casting from HttpServletRequest to WebRequest 如何使用Spring MVC Jackson拦截HttpServletRequest - How to intercept HttpServletRequest with Spring MVC Jackson Spring MVC HttpServletRequest请求:单击了哪个按钮 - Spring MVC HttpServletRequest request: which button clicked on 如何在 Spring MVC 中获取 HttpServletRequest? - How do I get the HttpServletRequest in Spring MVC? 使用spring mvc从HttpServletRequest获取数据 - Get data from HttpServletRequest with spring mvc Spring MVC如何使HttpServletRequest字段具有线程安全性? - How Spring MVC make HttpServletRequest field threadsafe? 在 spring mvc 中创建自定义注解并获取 httpservletrequest 对象 - creating custom annotation in spring mvc and getting httpservletrequest object 如何在不使用 HttpServletRequest session 的情况下更新 @SessionAttributes,使用 Spring MVC - How to update @SessionAttributes without using the HttpServletRequest session, using Spring MVC 在我的Spring MVC应用程序中使用HttpServletRequest? 有什么好处? - use HttpServletRequest in my Spring MVC application? Any benefits?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM