2011-08-04 111 views
1

這裏的東西,重新注入POSTDATA重定向請求

爲了模仿Grails的方式做多次提交的一種形式,我實現了這個攔截器,這樣,當它遇到一個URL,例如:
/什麼-url/$ {controllerName}/index它重定向到/無論url/$ {controllerName}/$ {submitName}

我有兩個問題,首先它被重定向get方法,第二我不保留jsp中的原始表單的postData,任何人都有一個想法如何在重定向的請求中設置它?
這裏是攔截器類:

public class ControllerIndexInterceptor extends HandlerInterceptorAdapter { 

    private static String[] redirects = {"edit", "delete"}; 

     /** 
     * {@inheritDoc} 
     */ 
     @Override 
     public boolean preHandle(HttpServletRequest request, 
       HttpServletResponse response, Object handler) throws Exception { 
      String param = null; 
      for (String redirect : redirects) { 
       if((param = request.getParameter(redirect)) != null){ 
        response.sendRedirect(request.getContextPath()+request.getServletPath()+ 
transformURI(request.getPathInfo(), redirect, handler)); 
        return false; 
       } 
      } 
      return true; 

     } 

答:

public class ControllerIndexInterceptor extends HandlerInterceptorAdapter { 

    private static String[] redirects = {"edit", "delete"}; 

     /** 
     * {@inheritDoc} 
     */ 
     @Override 
     public boolean preHandle(HttpServletRequest request, 
       HttpServletResponse response, Object handler) throws Exception { 
      String param = null; 
      for (String redirect : redirects) { 
       if((param = request.getParameter(redirect)) != null){ 
        request.getRequestDispatcher(request.getServletPath()+ 
transformURI(request.getPathInfo(), redirect, handler)).forward(request,response); 
        return false; 
       } 
      } 
      return true; 

     } 

回答

2

IMO response.sendRedirect()將發送重定向狀態代碼到瀏覽器,這將最終使一個新的請求,這是你的理由獲取GET請求。這也是沒有獲得POST數據的原因(因爲它是一個新的和不同的請求)。改爲使用request.getRequestDispatcher()

+0

謝謝,我不得不改變一點代碼,但請求調度員是答案。 – Gepsens