Servern för lagring av formulärdata
Nästa steg är att skapa en serverlet som antingen infogar eller uppdaterar formulärdata. Servern anropar lämpliga metoder i OSGi-tjänsten för att antingen infoga eller uppdatera databasen. Lagrade data för anpassningsbara formulär är kopplade till ett GUID. Samma GUID används sedan för att uppdatera formulärdata. Den här servern anropas när användaren klickar på knappen SaveAndContinueLater.
package com.aemforms.saveandcontinue.core.servlets;
import java.io.IOException;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aemforms.saveandcontinue.core.FetchStoredFormData;
import com.google.gson.JsonObject;
@Component(service = {
Servlet.class
},
property = {
"sling.servlet.methods=post",
"sling.servlet.paths=/bin/storeafdata"
})
public class StoreDataInDB extends SlingAllMethodsServlet {
private static final Logger log = LoggerFactory.getLogger(StoreDataInDB.class);
private static final long serialVersionUID = 1L;
@Reference
FetchStoredFormData fetchStoredFormData;
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
log.debug("Inside my save af data servlet");
if (request.getParameter("operation").equalsIgnoreCase("update")) {
log.debug("The operation is update");
log.debug("The data I got was " + request.getParameter("formdata"));
String guid = fetchStoredFormData.updateData(request.getParameter("guid"), request.getParameter("formdata"));
log.debug("The guid I got was " + guid);
JsonObject jsonResponse = new JsonObject();
try {
jsonResponse.addProperty("guid", guid);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonResponse.toString());
} catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (request.getParameter("operation").equalsIgnoreCase("insert")) {
log.debug("The data I got was " + request.getParameter("formdata"));
String guid = fetchStoredFormData.storeFormData(request.getParameter("formdata"));
log.debug("The guid on inserting data " + guid);
JsonObject jsonResponse = new JsonObject();
try {
jsonResponse.addProperty("guid", guid);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonResponse.toString());
} catch(IOException e) {
// TODO Auto-generated catch block
log.debug("error in writing response " + e.getMessage());
}
}
}
}
recommendation-more-help
8de24117-1378-413c-a581-01e660b7163e