使用POST请求实现页面的跳转

项目情景:

当用户选择几个item之后,点击 查看 按钮之后, 页面跳转到展示items详情页面.

实现:

如果可以使用get请求, 直接在前端使用windows.loaction.href = "newUrl="xxx"&item=itemValue1&item=itemValue2"就行了

这样的问题是如果item比较多, 就有可能超出URL长度限制. 所以考虑用POST实现跳转.

server端:

public class MyServlet extends SlingAllMethodsServlet {
    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String newUrl= request.getParameter("newUrl");
        RequestDispatcher dispatcher = request.getRequestDispatcher(newUrl);
        GetRequest getRequest = new GetRequest(request);
        dispatcher.forward(getRequest, response);
    }

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String selectedPagePath = request.getParameter("SelectedPagePath");
        response.sendRedirect(selectedPagePath);
    }

    /** Wrapper class to always return GET for AEM to process the request/response as GET.
     */
    private static class GetRequest extends SlingHttpServletRequestWrapper {
        public GetRequest(SlingHttpServletRequest wrappedRequest) {
            super(wrappedRequest);
        }

        @Override
        public String getMethod() {
            return "GET";
        }
    }
}

前端:

如果使用ajax提交请求,返回的将是新页面的html代码. 必须用form提交请求.

猜你喜欢

转载自www.cnblogs.com/blogkevin/p/10749050.html