开发者

Reinject postdata in interceptor redirected request

Here's the thing,

In order to imitate the way grails does multiple submits for one form, I implemented this interceptor, so that when it encounters an url such as :

/whatever-url/${controllerName}/index it redirects to /whatever-url/${controllerName}/${submitName}

I have two problems, first it is redirected with a get method and second I don't keep the postData from the original form that was in the jsp, anyone has an idea how to set this back in the redirected request ?

Here is the interceptor class :

   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;

        }

Answer :

开发者_运维问答

   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;

        }


IMO response.sendRedirect() will send a redirect status code to browser which will eventually make a NEW request which is the reason that you are getting GET requests. This is also the reason behind not getting POST data (as its a new and different request altogether). Use request.getRequestDispatcher() instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜