使用GuideBridge APIPOST表單資料
表單的「儲存並繼續」涉及允許使用者儲存填寫表單的進度並在稍後繼續。
為了完成此使用案例,我們需要使用GuideBridge API存取表單資料,並將其傳送至REST端點以進行儲存和擷取。
表單資料會使用規則編輯器儲存在按鈕的點選事件上
下列JavaScript函式是用來傳送資料至指定的端點
/**
* Submits data and attachments
* @name submitFormDataAndAttachments Submit form data and attachments to REST end point
* @param {string} endpoint in Stringformat
* @return {string}
*/
function submitFormDataAndAttachments(endpoint) {
guideBridge.getFormDataObject({
success: function(resultObj) {
const afFormData = resultObj.data.data;
const formData = new FormData();
formData.append("dataXml", afFormData);
resultObj.data.attachments.forEach(attachment => {
console.log(attachment.name);
formData.append(attachment.name, attachment.data);
});
fetch(endpoint, {
method: 'POST',
body: formData
})
.then(response => {
if (response.ok) {
console.log("successfully saved");
const fld = guideBridge.resolveNode("$form.confirmation");
return "Form data was saved successfully";
} else {
throw new Error('Failed to save form data');
}
})
.catch(error => {
console.error('Error:', error);
});
}
});
}
伺服器端代碼
已寫入下列伺服器端Java程式碼以處理表單資料。 以下是在AEM中執行的Java servlet,這會透過上述JavaScript中的XHR呼叫進行呼叫。
package com.azuredemo.core.servlets;
import com.adobe.aemfd.docmanager.Document;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
@Component(
service = {
Servlet.class
}
)
@SlingServletResourceTypes(
resourceTypes = "azure/handleFormSubmission",
methods = "POST",
extensions = "json"
)
public class StoreFormSubmission extends SlingAllMethodsServlet implements Serializable {
private static final long serialVersionUID = 1 L;
private final transient Logger log = LoggerFactory.getLogger(this.getClass());
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
List < RequestParameter > listOfRequestParameters = request.getRequestParameterList();
log.debug("The size of list is " + listOfRequestParameters.size());
for (int i = 0; i < listOfRequestParameters.size(); i++) {
RequestParameter requestParameter = listOfRequestParameters.get(i);
log.debug("is this request parameter a form field?" + requestParameter.isFormField());
if (!requestParameter.isFormField()) {
Document attachmentDOC = new Document(requestParameter.getInputStream());
attachmentDOC.copyToFile(new File(requestParameter.getName()));
} else {
log.debug("Not a form field " + requestParameter.getName());
log.debug(requestParameter.getString());
}
}
response.setStatus(HttpServletResponse.SC_OK);
}
}
recommendation-more-help
8de24117-1378-413c-a581-01e660b7163e