Creating a request () using spring's servletrequestattributes today gets a null pointer
((servletrequestattributes) requestcontextholder.getrequestattributes ()). Getrequest ();
View Requestcontextholder source code, found in the class mentioned Requestcontextlistener
Then look at the source code for Requestcontextlistener, which has a method
public void requestinitialized (Servletrequestevent requestevent) {
if (!) ( Requestevent.getservletrequest () instanceof HttpServletRequest)) {
throw New IllegalArgumentException (
"The Request is isn't an httpservletrequest:" +
Requestevent.getservletrequest ());
}
HttpServletRequest request = (HttpServletRequest) requestevent.getservletrequest ();
Servletrequestattributes attributes = new Servletrequestattributes (request);
Request.setattribute (Request_attributes_attribute, ATTRIBUTES);
Localecontextholder.setlocale (Request.getlocale ());
Requestcontextholder.setrequestattributes (attributes); Set the Requestattributes property to a good
if (logger.isdebugenabled ()) {
Logger.debug ("Bound request context to Thread:" + request);
} }
Therefore, to be configured under the Web.xml monitoring, so that the server started on the initialization of the change class, you can get request
<listener>
<listener-class>
Org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
This allows normal execution
((servletrequestattributes) requestcontextholder.getrequestattributes ()). Getrequest ();