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


    项目情景:

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

    实现:

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

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

    server端:

    
    
    @SlingServlet(resourceTypes = "/apps/xxx/components/pages/xxx-contentpage",
    extensions = "html",
    selectors = "selected",
    methods = {"GET", "POST"},
    metatype = true
    )
    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 {
        doPost(request, response);
     } 

      /** 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"; }
    }

    }

    如果直接forward POST request,会报错:

    500 error - operations.ModifyOperation Exception during response processing 

    所以必须把request转换成GET request.

    前端:

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

     提示: form里面的action URL 与 servlet 的newUrl 必须不一样 ,否则会发生无限循环错误.

    参考: http://suryakand-shinde.blogspot.com/2016/07/aem-form-submission-handling-post.html

    forward()和sendRedirect()的区别: https://examples.javacodegeeks.com/enterprise-java/servlet/java-servlet-sendredirect-example/

  • 相关阅读:
    zabbix迁移思路
    top命令
    random随机数
    判断传入元素是否可见
    title_contains
    1.Selenium的工作原理以及网页上查找元素
    APP升级测试
    英语词汇
    作文 |素材笔记
    408计组 |二、数据的表示和运算
  • 原文地址:https://www.cnblogs.com/blogkevin/p/10749050.html
Copyright © 2020-2023  润新知