springMVC跨域访问

@Controller
@RequestMapping("/AjaxProxy")
public class AjaxProxyController {

        @RequestMapping(value = "/proxy")
        @ResponseBody
        public final void proxyAjaxCall(
                        @RequestParam(required = true, value = "url") String url,
                        HttpServletRequest request, HttpServletResponse response)
                        throws IOException {

                // URL needs to be url decoded
                url = URLDecoder.decode(url, "utf-8");

                // 此处主义要设置OutputStreamWriter的编码, 否则使用系统的默认字符编码
                OutputStreamWriter writer = new OutputStreamWriter(
                                response.getOutputStream(), response.getCharacterEncoding());
                HttpClient client = new HttpClient();
                try {

                        HttpMethod method = null;

                        // Split this according to the type of request
                        if (request.getMethod().equals("GET")) {

                                method = new GetMethod(url);

                        } else if (request.getMethod().equals("POST")) {

                                method = new PostMethod(url);

                                // Set any eventual parameters that came with our original
                                // request (POST params, for instance)
                                Enumeration<String> paramNames = request.getParameterNames();
                                while (paramNames.hasMoreElements()) {

                                        String paramName = paramNames.nextElement();
                                        ((PostMethod) method).setParameter(paramName,
                                                        request.getParameter(paramName));
                                }

                        } else {

                                throw new NotImplementedException(
                                                "This proxy only supports GET and POST methods.");
                        }

                        method.getParams().setParameter(
                                        HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

                        // Execute the method
                        client.executeMethod(method);

                        // Set the content type, as it comes from the server
                        Header[] headers = method.getResponseHeaders();
                        for (Header header : headers) {

                                if ("Content-Type".equalsIgnoreCase(header.getName())) {

                                        response.setContentType(header.getValue());
                                }
                        }

                        String responseBody = method.getResponseBodyAsString();

                        // Write the body, flush and close
                        writer.write(responseBody);
                        writer.flush();
                        writer.close();

                } catch (HttpException e) {

                        // log.error("Oops, something went wrong in the HTTP proxy", null,
                        // e);
                        writer.write(e.toString());
                        throw e;

                } catch (IOException e) {

                        e.printStackTrace();
                        writer.write(e.toString());
                        throw e;
                }
        }
}
在 openlayers 中代理的使用:
OpenLayers.ProxyHost = '/XXXXXX/AjaxProxy/proxy.do?url=';
XXXXXX是项目名字

转载 

你可能感兴趣的:(springMVC)