Reputation: 3597
When i do a Forward using RequestDispatcher.. the result page loads but the URL does not change.
The URL where we start and Submit data to the PostServlet: http://localhost:4502/content/en/postformtest.html
Final result URL should be: http://localhost:4502/content/en/postformtestresult.html
But is: http://localhost:4502/services/processFormData
What am i missing? Appreciate any thoughts.
Code snippets..
The HTML Form:
<form name="userRegistrationForm" method="post" action="/services/processFormData">
<input type="submit" title="Submit" class="btn submit btn-success" value="Submit" tabindex="25" name="bttnAction">
</form>
The POST Servlet
@SlingServlet(
label = "Common POST Servlet",
metatype = true,
methods = { "POST" },
name="com.commons.service.servlets.TPostServlet",
paths = { "/services/processFormData" }
)
public class TPostServlet extends SlingAllMethodsServlet{
@Override
protected void doPost(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,IOException {
final SlingHttpServletRequest syntheticRequest = new SyntheticSlingHttpServletGetRequest(request);
final RequestDispatcherOptions options = new RequestDispatcherOptions();
options.setReplaceSelectors("");
options.setForceResourceType("cq/Page");
request.getRequestDispatcher("/content/en/postformtestresult.html", options).forward(syntheticRequest, response);
}
}
The Wrapper Servlet:
public class SyntheticSlingHttpServletGetRequest extends
SlingHttpServletRequestWrapper {
private static final String METHOD_GET = "GET";
public SyntheticSlingHttpServletGetRequest(final SlingHttpServletRequest request) {
super(request);
}
@Override
public String getMethod() {
return METHOD_GET;
}
}
Upvotes: 2
Views: 2755
Reputation: 76
Have you looked at ACS Commons Forms? They do support PRG as a standard form handling process. Check it out at http://adobe-consulting-services.github.io/acs-aem-commons/features/forms.html
You may end up using the feature as is or will get some hint for implementation.
The git link for the same is https://github.com/Adobe-Consulting-Services/acs-aem-commons
Upvotes: 0
Reputation: 326
As the javadocs for RequestDispatcher
indicate, the RequestDispatcher
and by association the forward
method act as wrappers around the resource essentially allowing the delegation of further processing to the resource. This is done behind the scenes so to speak and as such the requested URL will not change - it is not a redirect.
Based on the content of your question I presume what you are trying to accomplish is a traditional form POST to a page. This is actually a rather cumbersome pattern to achieve in AEM and you would most likely be better served by submitting the form asynchronously and then redirecting based on the response.
If all you need is a simple redirect after form processing this can be achieved by calling the sendRedirect
method of the response.
If, however, you do need to POST to a page which is then going to handle both the form processing and the page rendering, you could employ a method similar to the OOB form components. The OOB com.day.cq.wcm.foundation.forms.impl.FormsHandlingServlet
is implemented as both a Servlet and a request level Filter. As a filter it catches to POST request to the page prior to processing, forwards it using the RequestDispatcher
to its Servlet nature, and the Servlet in turn is able to process the request and then forward it, again using the RequestDispatcher
, to the page after wrapping the request as a GET request similar to what you are doing above. A bit circuitous but, as noted, this is a cumbersome pattern to realize.
Upvotes: 2