Last time to write a website to the site before and after the separation of the last kneeling in the Ajax cross-domain on the internet I found a way to try to use the record
Write a class inheritance handlerinterceptoradapter
PackageCom.util;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.handler.HandlerInterceptorAdapter; Public classCommoninterceptorextendsHandlerinterceptoradapter { Public BooleanPrehandle (httpservletrequest request,httpservletresponse response, Object handler)throwsException {response.setheader ("Access-control-allow-origin", "*"); Response.setheader ("Access-control-allow-methods", "*"); Response.setheader ("Access-control-max-age", "3600"); Response.setheader ("Access-control-allow-headers","Origin, X-requested-with, Content-type, Accept"); Response.setheader ("Access-control-allow-credentials", "true");//whether the browser is allowed to carry user identity information (cookies)return true;}}
Then configure the path in the XML
<mvc:interceptors><!--filtering all requests, handling cross-domain request issues--><mvc:interceptor><mvc:mapping path= "/**"/>< Bean class= "Com.util.CommonInterceptor"/></mvc:interceptor></mvc:interceptors>
That's fine, but there's a blog post that says it's okay to do a simple cross-domain. However, the request for Post+json failed, prompting cross-domain failure. So let's take a look at the way he solves it.
Establish a class inheritance Onceperrequestfilter
Public classCrossfilterextendsOnceperrequestfilter {@Overrideprotected voidDofilterinternal (HttpServletRequest request, httpservletresponse response, Filterchain Filterchain)throwsservletexception, IOException {if(Request.getheader ("Access-control-request-method")! =NULL&& "OPTIONS". Equals (Request.getmethod ())) { //CORS "pre-flight" RequestResponse.AddHeader ("Access-control-allow-origin", "*"); Response.AddHeader ("Access-control-allow-methods", "GET, POST, PUT, DELETE"); Response.AddHeader ("Access-control-allow-headers", "Content-type"); Response.AddHeader ("Access-control-max-age", "1800");//min} filterchain.dofilter (request, response); }}
Web. XML inside Set
<filter> <filter-name>cors</filter-name> <filter-class>cn.***.filter. crossfilter</filter-class></filter><filter-mapping> <filter-name>cors< /filter-name> <url-pattern>/*</url-pattern></filter-mapping>
Then you can do the above way for the spring3.0 version if the 4.0 version can be used in the following way (no test)
XML inside Configuration
<mvc:cors> <mvc:mapping path= "/**" allowed-origins= "*" allow-credentials= "true" max-age= "1800" allowed-methods= "Get,post,options"/> </mvc:cors>
Part of the blog http://www.cnblogs.com/asfeixue/p/4363372.html of Flying Ang Snow
SPRINGMVC Ajax cross-domain request processing