AEM 6.1. The original base form. Submit and redirect to the same page.

I am trying to do the following on AEM 6.1:

  • Design a simple form (3 entry fields)
  • Process the submitted values,
  • And redirect to the same page with processed values ​​/ results

I can send the values ​​to the servlet and process them (business logic), and the result is in the requestparamter so that I can get them in the user interface. But I got stuck in them:

  • Redirect to the same page
  • And get query parameters and display them using Sightly.

Code Snippets: Servlet

@SlingServlet(
methods = { "POST","GET" }, 
name="com.tti.tticommons.service.servlets.LeadTimeTrendsServlet",
paths = { "/services/processFormData" }
)
public class TTICommonServlet extends SlingAllMethodsServlet{   
...
@Override
protected void doPost(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,IOException {
  String result;
  try {
        Enumeration<String> parameterNames = request.getParameterNames();
        Map<String, String> formParametersMap = new HashMap<String, String>();
        while (parameterNames.hasMoreElements()) {
            paramName = parameterNames.nextElement();
            paramValue = request.getParameter(paramName);
            .......
            .......
       }

       request.setAttribute("result",result);

       response.sendRedirect("/content/ttii/en/**posttest.html**");
    }
}

Can someone help in ho to delay the above “result” in posttest.html using visible.

+4
source share
2 answers

. stackoverflow. . .

webservice:

enter image description here

  • POST .
  • Servlet ,
  • webservice. (json)
  • -json
  • Wrapper,
  • WCMUse Sightly.
  • "" Use
  • Use-the-user ,

- HTML

  <form name="userRegistrationForm" method="post" action="/services/processFormData">

<input type="hidden" name=":redirect" value="posttest.html" />
<input type="submit" title="Submit" class="btn submit btn-success" value="Submit" tabindex="25" name="bttnAction">

<div data-sly-use.model="${'com.abccommons.service.helpers.PostServiceHelper' @ slingreq=request }">
**${model.getRawJson}**
</div>

-

@SlingServlet(
label = "ABC - Common Servlet", 
metatype = true, 
methods = { "POST" }, 
name="com.abccommons.service.servlets.ABCPostServlet",
paths = { "/services/processFormData" }
)
public class ABCPostServlet extends SlingAllMethodsServlet{ 

@Override
protected void doPost(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,IOException {  
    log.info("\n\n----- ABCPostServlet POST: ");        

    String paramName;
    String paramValue;
    String osgiService="";

    try {
        Enumeration<String> parameterNames = request.getParameterNames();
        Map<String, String> formParametersMap = new HashMap<String, String>();
        while (parameterNames.hasMoreElements()) {
            paramName = parameterNames.nextElement();
            paramValue = request.getParameter(paramName);

            if (paramName.equals("osgiService")) {
                osgiService = paramValue;
            } else if (paramName.equals(":cq_csrf_token")) {
                //TODO: don't add to the map
            } else if (paramName.equals("bttnAction")) {
                //TODO: dont' add to the map
            } else {
                //log.info("\n---ParamName="+paramName+", value="+paramValue);
                formParametersMap.put(paramName, paramValue);                            
            }
        }           

        String parametersInJSON = JSONHelper.toJson(formParametersMap);
        log.info("\n\n----------- POST paramters in json="+parametersInJSON);

        String json = webServiceHelper.getJSON(osgiService, parametersInJSON, request, response);
        log.info("\n\n----------- POST json from web service="+json);

        request.setAttribute("jsonResponse",json);

        //String redirectPage =  request.getParameter(":redirect");
        //RequestDispatcher dispatcher = request.getRequestDispatcher("/content/en/"+redirectPage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/content/en/postformtest.html");
        GetRequest getRequest = new GetRequest(request);
        dispatcher.forward(getRequest, response);            
    } catch (Exception e) {
        log.error("SlingServlet Failed while retrieving resources");
    } finally {
       //TODO
    }         
}

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

- PostServiceHelper - WCMUSe

public class PostServiceHelper extends WCMUse {
protected final Logger log = LoggerFactory.getLogger(PostServiceHelper.class);

private SlingHttpServletRequest httpRequest;

private String rawJson;

@Override
public void activate() throws Exception {
    log.info("\n\n========= PostServiceHelper.activate():"+get("slingreq", SlingHttpServletRequest.class));
    this.httpRequest = get("slingreq", SlingHttpServletRequest.class);
    //this.resourceResolver = getResourceResolver();        
    //log.info("\n\n========= getRequest()="+getRequest()); 

    SlingHttpServletRequest tRequest;

    Set<String> keys = new HashSet<String>();
    Enumeration<?> attrNames = this.httpRequest.getAttributeNames();
    while (attrNames.hasMoreElements()) {
        String attr = (String) attrNames.nextElement();            
        //log.info("\n--- Key="+attr);

        if (attr.equals("jsonResponse")) {
            this.setRawJson((String)this.httpRequest.getAttribute(attr));
            //log.info("\n---rawJson is SET with : "+this.rawJson);
        }
    }
}

 public void setRawJson(String json) {
    this.rawJson = json;
}   

public String getRawJson() {
    return this.rawJson;
}
}
+4

Sling. , HTML JavaScript.

, HTML. , , , . JSP- Servlet AEM , . , , .

+1

Source: https://habr.com/ru/post/1598213/


All Articles