Java Spring MVC 中的 WebRequest 和 HttpServletRequest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24342533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:40:34  来源:igfitidea点击:

WebRequest and HttpServletRequest in Spring MVC

javaspringspring-mvcspring-3

提问by user182944

What is the difference between the two? Both have a getParametermethod as well as setAttributemethod, 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)请说明具体可以使用的场景。

采纳答案by JB Nizet

The javadoc of WebRequestis pretty clear on the subject:

WebRequestjavadoc对这个主题非常清楚:

Generic interface for a web request. Mainly intended for generic web request interceptors, giving them access to general request metadata, not for actual handling of the request.

Web 请求的通用接口。主要用于通用 Web 请求拦截器,使它们能够访问通用请求元数据,而不是用于实际处理请求。

(emphasis mine).

(强调我的)。

The javadoc links to WebRequestInterceptor, which says:

javadoc 链接到 WebRequestInterceptor,它说:

Interface for general web request interception. 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. And they introduced this interface in order to be able to write interceptors that apply to servlets and portlets. Other than that, if you really need access to the request in Spring MVC controllers, you should use the standard HttpServletRequest.

所以,基本上,除了在 WebRequestInterceptor 中,你不应该使用 WebRequest。他们引入这个接口是为了能够编写适用于 servlet 和 portlet 的拦截器。除此之外,如果你真的需要访问 Spring MVC 控制器中的请求,你应该使用标准的 HttpServletRequest。