我有一個資源感知spring mvc portlet,用於爲PDF提供服務。以前,我們用於從portlet提供PDF的方法一直是鏈接到servlet以實際編寫PDF響應。我們在servlets中的模式基本上是這樣的:當pdf生成失敗時將錯誤頁面發送到portlet資源響應
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
OutputStream out = response.getOutputStream();
FileInputStream certIn = null;
try {
certIn = new FileInputStream(pdfFile);
if (certIn.available() > 0) {
while (certIn.available() > 0) {
out.write(certIn.read());
}
out.flush();
}
} catch (IOException e) {
response.reset();
response.setContentType("text/html");
getServletContext().getRequestDispatcher("/WEB-INF/jsp/error.jsp").include(request, response);
} finally {
if (certIn != null) {
try {
certIn.close();
} catch (IOException e) {
LOG.warn(
"Failed to close FileInputStream", e);
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
LOG.warn("Failed to close ServletOutputStream", e);
}
}
}
}
我現在試圖在資源感知portlet中複製它。我得到的問題是,如果我們得到一個錯誤,我不能讓它重定向到錯誤的jsp。
如果我使用portletContext.getRequestDispatcher()來轉發到一個jsp,我得到一個錯誤,說我不能在getOuputStream()之後調用getWriter()。如果我嘗試將一個Spring ModelAndView返回給error.jsp,我會得到相同的錯誤。
任何人都可以建議如何在ResourceResponse上調用getOutputStream()後將用戶重定向到jsp?
謝謝。
我不確定Portlets,但是我在正常的非Portlet中完成此操作的方式是讓我的請求處理程序返回ModelAndView。當文件生成成功時,我將「視圖」設置爲一個自定義視圖,該視圖擴展了org.springframework.web.servlet.view.AbstractView,它將我們的有效內容寫出到響應對象OutputStream中(類似於上面的操作) 。如果失敗,我們使用我們通常使用的標準ViewResolver來設置視圖,在我們的例子中是JspViewResolver(我認爲這是名字)。 – CodeChimp