简体   繁体   English

无法通过POST的sling servlet中的请求分派器重定向到AEM中的另一个页面

[英]Unable to redirect to another page in AEM using request dispatcher in sling servlet via POST

I have a scenario where from one page in AEM, I need to call another AEM page in the same application and I need to pass some hidden parameters. 我有一个场景,在AEM的一个页面中,我需要在同一应用程序中调用另一个AEM页面,并且需要传递一些隐藏参数。 I choose to do it via POST and below are the steps which I followed: From page "A", I did a form submission via POST to the sling servlet and passed some parameters. 我选择通过POST进行操作,以下是我遵循的步骤:从页面“ A”,我通过POST向吊索servlet提交了表单提交并传递了一些参数。 2. In the servlet, using request dispatcher I redirected the same request and response to a different page in doPost method using the following code snippet: 2.在servlet中,使用请求分派器,使用以下代码片段将相同的请求和响应重定向到doPost方法中的不同页面:

          request.getRequestDispatcher("/content/company/en/apps/welcomepage.html").forward(request, response);

When I run the code, I am able to call the servlet through form submission but I am not able to redirect to a new page. 运行代码时,我可以通过表单提交来调用servlet,但是不能重定向到新页面。 I see the below error in logs: 我在日志中看到以下错误:

18.10.2017 14:41:00.802 ERROR [127.0.0.1 [1508352060795] POST /bin/rap/welcomepage HTTP/1.1] org.apache.sling.servlets.post.impl.operations.ModifyOperation Exception during response processing. 18.10.2017 14:41:00.802 错误 [127.0.0.1 [1508352060795] POST / bin / rap / welcomepage HTTP / 1.1] org.apache.sling.servlets.post.impl.operations.ModifyOperation响应处理期间的异常。 javax.jcr.nodetype.ConstraintViolationException: No matching property definition: appointmentTypeId = platform001d at org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate.setProperty(NodeDelegate.java:522) at org.apache.jackrabbit.oak.jcr.session.NodeImpl$35.perform(NodeImpl.java:1375) at org.apache.jackrabbit.oak.jcr.session.NodeImpl$35.perform(NodeImpl.java:1363) at org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate.perform(SessionDelegate.java:208) at org.apache.jackrabbit.oak.jcr.session.ItemImpl.perform(ItemImpl.java:112) at org.apache.jackrabbit.oak.jcr.session.NodeImpl.internalSetProperty(NodeImpl.java:1363) javax.jcr.nodetype.ConstraintViolationException:没有匹配的属性定义:org.apache.jackrabbit.oak.jcr处的约会类型=平台001d在org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate.setProperty(NodeDelegate.java:522)处。在org.apache.jackrabbit.oak.jcr.session的session.NodeImpl $ 35.perform(NodeImpl.java:1375)在org.apache.jackrabbit.oak.jcr.delegate的session.NodeImpl $ 35.perform(NodeImpl.java:1363)。 org.apache.jackrabbit.oak.jcr.session.ItemImpl.perform(ItemImpl.java:112)的SessionDelegate.perform(SessionDelegate.java:208)org.apache.jackrabbit.oak.jcr.session.NodeImpl.internalSetProperty( NodeImpl.java:1363)

If I try the same code in doGet method it works fine. 如果我在doGet方法中尝试相同的代码,则效果很好。 Also if I use response.sendRedirect("/content/company/en/apps/welcomepage.html") it works fine too. 另外,如果我使用response.sendRedirect(“ / content / company / en / apps / welcomepage.html”),它也可以正常工作。 But the problem with this is it initiates it as a new request to the page and it looses all the parameters which I get from the form submission. 但是,这样做的问题是它将它作为对页面的新请求启动,并失去了我从表单提交中获得的所有参数。 Could someone please let me know like how can I redirect a request to a page in AEM via POST since I need to pass some hidden parameters whic should not be visible in the url ? 有人可以让我知道,例如如何通过POST将请求重定向到AEM中的页面,因为我需要传递一些隐藏参数,而url中应该不可见?

Here is the way I understand your question 这是我理解您问题的方式

  1. User visits page "A" 用户访问页面“ A”
  2. User fills a form, then submits. 用户填写表格,然后提交。
  3. Your custom servlet handles the submitted POST request and calls: request.getRequestDispatcher("/content/company/en/apps/welcomepage.html").forward(request, response); 您的自定义Servlet处理提交的POST请求并调用: request.getRequestDispatcher("/content/company/en/apps/welcomepage.html").forward(request, response);
  4. You get the ConstraintViolationException 您会收到ConstraintViolationException

Why do you get this exception? 为什么会出现此异常?

Since you are using forward the POST request is forwarded to /content/company/en/apps/welcomepage.html that node is most likely of type cq:Page , which has constraints on which properties can be added. 由于您正在使用forward因此POST请求将转发到/content/company/en/apps/welcomepage.html ,该节点最有可能是cq:Page类型,该类型对可以添加属性有限制。 Think of it as a simple post request trying to store parameters on the cq:Page node. 可以将其视为一个简单的发布请求,尝试将参数存储在cq:Page节点上。

What can you do? 你能做什么?

Since I don't understand your use-case and particularly why you need to preserve the submit params, I cannot recommend a specific solution. 由于我不了解您的用例,尤其是为什么您需要保留提交参数,因此我不建议您使用特定的解决方案。 However, since you don't want the parameters in the URL, here is a potential solution you can try: 但是,由于您不需要URL中的参数,因此可以尝试以下解决方案:

  1. In your servlet handler, see those hidden params to cookies on the response. 在您的servlet处理程序中,查看响应中cookie的那些隐藏参数。
  2. use response.sendRedirect("/content/company/en/apps/welcomepage.html") 使用response.sendRedirect("/content/company/en/apps/welcomepage.html")
  3. On any of the components in /content/company/en/apps/welcomepage.html , you can get the request cookies and process them however you like. /content/company/en/apps/welcomepage.html任何组件上,您都可以获取请求cookie并根据需要进行处理。 same way you wanted to process those hidden params. 您想要处理这些隐藏参数的方式相同。

Now the flow becomes: 现在流程变为:

  1. User visits page "A" 用户访问页面“ A”
  2. User fills a form, then submits. 用户填写表格,然后提交。
  3. Your custom servlet handles the submitted POST request, adds your special params to cookies on the response, then calls response.sendRedirect("/content/company/en/apps/welcomepage.html") 您的自定义Servlet处理提交的POST请求,将特殊参数添加到响应的Cookie中,然后调用response.sendRedirect("/content/company/en/apps/welcomepage.html")
  4. User's browser receives a 301 response with the cookies, sets the cookies in the browser then requests "/content/company/en/apps/welcomepage.html" 用户的浏览器收到包含Cookie的301响应,在浏览器中设置Cookie,然后请求“ /content/company/en/apps/welcomepage.html”
  5. Your components handle the request and get the params from the cookies, then retuns the appropriate response. 您的组件处理该请求并从cookie中获取参数,然后重新调整适当的响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM