2012-12-17 33 views
1

在Spring將其刷新到頁面之前,我可以如何修改輸出內容?當內容準備就緒時,我需要運行一個正則表達式來修復所有鏈接。JAVA SPRING MVC在頁面上顯示之前修改輸出流

可以說我使用攔截器,我如何獲取渲染內容,修改它並將其設置回來?

public class SpringControllerInterceptor extends HandlerInterceptorAdapter { 

    @Override 
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { 
     System.out.println("postHandle"); 

    } 
} 

回答

1

您可以添加Filter層來處理你的迴應,你可以抓住整個身體的反應和處理你想

+2

讓我猜...如果計算器允許一個答案短,你會回答 「是」。 – Yevgeniy

0

Spring MVC的攔截器將是這樣做的方式之一的鏈接。覆蓋postHandle方法來更新http響應對象。

void postHandle(HttpServletRequest request, 
       HttpServletResponse response, 
       Object handler, 
       ModelAndView modelAndView) 
       throws Exception 

攔截處理程序的執行。在HandlerAdapter實際調用處理程序之後調用,但在DispatcherServlet呈現該視圖之前調用。

瞭解更多:

  1. Spring MVC Interceptor example
  2. Spring Interceptor Documentation
+0

正確,但內部攔截器如何獲得實際渲染的內容,修改它並將其設置回來? – Dima

+0

爲此,您可能必須擴展類'HttpServletResponseWrapper'並使用裝飾器模式在更新併發送到客戶端之前獲取內容。請參閱示例代碼:http://lexis.livejournal.com/3388.html –

+0

@ViralPatel不幸的是,這種裝飾模式方法不適用於我的情況。我已經在[link](http://stackoverflow.com/q/14092997/349667)的這個stackoverflow問題中傾注了我的悲哀。你能提出一些建議嗎? –

相關問題