我可以提交表单,将数据推送到数据库,然后以我的servlet成功/错误消息结束。 但是我仍然需要将重定向回html格式以及相关结果:
1)成功叠加(要触发的现有javascript方法)和所有表单字段为空
2)错误消息(表单字段仍包含输入值)
这些是我目前拥有(提取)的代码和文件:
Form.java
protected void contructAndSendResponse(HttpServletResponse response, String responseStatus, String... responseErrorCodes) throws Exception {
StringBuilder responseMsg = new StringBuilder();
responseMsg.append(Form.RESPONSE_DATA_STATUS);
responseMsg.append(responseStatus);
if (responseErrorCodes == null || responseErrorCodes.length == 0) {
responseMsg.append(Form.JSON_CLOSE_OBJECT);
}
else {
responseMsg.append(Form.RESPONSE_DATA_ERROR_CODE);
responseMsg.append(Form.JSON_BEGIN_ARRAY);
int errorCodeLength = responseErrorCodes.length;
for (int i = 0; i < errorCodeLength; i++) {
responseMsg.append(Form.JSON_DOUBLE_QUOTE);
responseMsg.append(responseErrorCodes[i]);
responseMsg.append(Form.JSON_DOUBLE_QUOTE);
if (i < errorCodeLength - 1) {
responseMsg.append(Form.JSON_COMMA);
}
}
responseMsg.append(Form.JSON_END_ARRAY);
responseMsg.append(Form.JSON_CLOSE_OBJECT);
}
contructAndSendResponse(response, responseMsg.toString());
}
protected void contructAndSendResponse(HttpServletResponse response, String responseMsg) throws Exception {
ServletOutputStream outStream = response.getOutputStream();
response.setContentType(Util.CONTENT_TYPE_JSON);
outStream.print(responseMsg);
outStream.flush();
outStream.close();
}
public void returnResponse(HttpServletResponse response, String responseStatus, String... responseErrorCodes) {
try {
contructAndSendResponse(response, responseStatus, responseErrorCodes);
}
catch (Exception e) {
}
}
Servlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Form onlineForm = null;
List<String> errorCodes = new ArrayList<String>();
try {
onlineForm = FormFactory.getInstance().createObject(formtype);
onlineForm.extractRequest(request);
errorCodes = onlineForm.validateInputParam();
if (errorCodes == null || errorCodes.isEmpty()) {
Connection conn = PersistentFactory.getInstance().openConnection();
responseStatus = Form.RESPONSE_DATA_STATUS_SUCCESS;
PersistentFactory.getInstance().closeConnection(conn);
}
}
catch (Exception e) {
errorCodes.add(EntityUtils.ErrorCode.E1.name());
}
onlineForm.returnResponse(response, responseStatus, errorCodes.toArray(new String[0]));
}
Form.jsp
<form id="form" action="/projectx/form?type=contact" class="custom-ui" method="POST" >
....
</form>
<script type="text/javascript">
function overlay(){
Projectx.modalDialog({
elem: "#form",
title: "Success",
body: "Thank you",
tpl: "modal-tpl"
});
}
</script>
我已尝试过这些但重定向不适用于我的上述代码:
- dispatcher.forward(request,response);
- forward(responsePage, aRequest, aResponse);
- redirect(responsePage, response);
答案 0 :(得分:0)
我找到了一个简单的解决方案。由于我的服务器端返回了json响应,我将客户端(js)脚本更改为:
$("#form").click(function(){
$.ajax({
url: '/projectx/servlet',
type: 'POST',
data: $('#form').serialize(),
success:function(data){
if(data.status=="success"){
success();
$("#form").trigger("reset");
}else{
checkagain();
}
},
error:function(data){
error();
}
});
});