在不使用HttpClient的情况下将已处理JSP的内容放入spring控制器?

时间:2012-02-01 22:27:24

标签: java spring-mvc

通常在Spring控制器中,您只需返回一个ModelAndView对象并将请求转发给JSP。

我需要做的是实际获取已处理JSP的内容,然后我可以通过JSONP响应发送它(例如:callback("processed HTML from JSP");

我知道我可以使用HttpClient来获取内容但是想知道是否有办法通过调用类似的东西来避免这一额外步骤:

String contents = processJSP(modelAndView);




更新了geek以显示我的最终解决方案:

首先,您需要一个假的HttpResponse来保存响应:

@Service
public class SpringUtils {
    private static final Logger LOG = Logger.getLogger(SpringUtils.class);

    @Autowired private ViewResolver viewResolver;
    @Autowired private LocaleResolver localeResolver;

    public String renderView(HttpServletRequest request, ModelAndView mav) {
        try {
            View view = viewResolver.resolveViewName(mav.getViewName(), localeResolver.resolveLocale(request));
            HttpServletResponse localResponse = new MyHttpServletResponseWrapper(new DummyResponse());

            view.render(mav.getModel(), request, localResponse);

            return localResponse.toString();
        } catch (Exception e) {
            return "";
        }
    }

    public boolean doesViewExist(HttpServletRequest request, String viewName) {
        try {
            if (viewResolver.resolveViewName(viewName, localeResolver.resolveLocale(request)) != null) {
                return true;
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }

        return false;
    }

    static class MyHttpServletResponseWrapper extends HttpServletResponseWrapper {
        private StringWriter sw = new StringWriter();

        public MyHttpServletResponseWrapper(HttpServletResponse response) {
            super(response);
        }

        public PrintWriter getWriter() throws IOException {
            return new PrintWriter(sw);
        }

        public ServletOutputStream getOutputStream() throws IOException {
            throw new UnsupportedOperationException();
        }

        public String toString() {
            return sw.toString();
        }
    }
}





DummyResponse

public class DummyResponse implements HttpServletResponse {
    public DummyResponse() {
    }

    public void setAppCommitted(boolean appCommitted) {}
    public boolean isAppCommitted() { return false; }
    public int getContentCount() { return -1; }
    public boolean getIncluded() { return false; }
    public void setIncluded(boolean included) {}
    public String getInfo() { return null; }
    public ServletResponse getResponse() { return null; }
    public OutputStream getStream() { return null; }
    public void setStream(OutputStream stream) {}
    public void setSuspended(boolean suspended) {}
    public boolean isSuspended() { return false; }
    public void setError() {}
    public boolean isError() { return false; }
    public ServletOutputStream createOutputStream() throws IOException {
        return null;
    }
    public void finishResponse() throws IOException {}
    public int getContentLength() { return -1; }
    public String getContentType() { return null; }
    public PrintWriter getReporter() { return null; }
    public void recycle() {}
    public void write(int b) throws IOException {}
    public void write(byte b[]) throws IOException {}
    public void write(byte b[], int off, int len) throws IOException {}
    public void flushBuffer() throws IOException {}
    public int getBufferSize() { return -1; }
    public String getCharacterEncoding() { return null; }
    public void setCharacterEncoding(String charEncoding) {}
    public ServletOutputStream getOutputStream() throws IOException {
        return null;
    }
    public Locale getLocale() { return null; }
    public PrintWriter getWriter() throws IOException { return null; }
    public boolean isCommitted() { return false; }
    public void reset() {}
    public void resetBuffer() {}
    public void setBufferSize(int size) {}
    public void setContentLength(int length) {}
    public void setContentType(String type) {}
    public void setLocale(Locale locale) {}

    public Cookie[] getCookies() { return null; }
    public String getHeader(String name) { return null; }
    public Collection<String> getHeaders(String arg0) { return null; }
    public Collection<String> getHeaderNames() { return null; };
    public String[] getHeaderValues(String name) { return null; }
    public String getMessage() { return null; }
    public int getStatus() { return -1; }
    public void reset(int status, String message) {}
    public void addCookie(Cookie cookie) {}
    public void addDateHeader(String name, long value) {}
    public void addHeader(String name, String value) {}
    public void addIntHeader(String name, int value) {}
    public boolean containsHeader(String name) { return false; }
    public String encodeRedirectURL(String url) { return null; }
    public String encodeRedirectUrl(String url) { return null; }
    public String encodeURL(String url) { return null; }
    public String encodeUrl(String url) { return null; }
    public void sendAcknowledgement() throws IOException {}
    public void sendError(int status) throws IOException {}
    public void sendError(int status, String message) throws IOException {}
    public void sendRedirect(String location) throws IOException {}
    public void setDateHeader(String name, long value) {}
    public void setHeader(String name, String value) {}
    public void setIntHeader(String name, int value) {}
    public void setStatus(int status) {}
    public void setStatus(int status, String message) {}
}

1 个答案:

答案 0 :(得分:3)

可能不太容易。 也许将视图解析器注入控制器并使用特殊响应调用render会有所帮助,但不确定:

ViewResolver viewResoler = // injected
View view = viewReslover.resolveViewName(String viewName, Locale locale);
HttpServletResponse xresponse = // custom response, buffers data
view.render(Map model, HttpServletRequest request, HttpServletResponse xresponse);
String content = // extract conten from data from xresponse