【发布时间】:2018-09-25 23:04:21
【问题描述】:
Java 8 / Spring 4 / Maven / jQuery
所以我制作了一个 Spring MVC webapp,它的 index.jsp 使用了 ajax 调用
--- index.jsp sn-p ----
function popupRuleDeck_update_submit() {
var formJsonStr = $('#form_popupRuleDeck_update').serialize();
$.ajax({
url: '${pageContext.request.contextPath}/ruleDeck_update',
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: formJsonStr,
success: function( response) {
$('#ruleDeckTable').row( this).data( response).draw( false);
return true;
},
error: function( response) {
console.log( response);
alert( "ruleDeck update error: " + response.message)
return false;
}
});
}
到我的控制器中的一个方法
@RequestMapping( value="/ruleDeck_update", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody AjaxResponseBody ruleDeckUpdateHandler( @RequestBody RuleDeck formRuleDeck)
{
AjaxResponseBody result = new AjaxResponseBody( false, "FAILED");
RuleDeck ruleDeck = null;
try {
ruleDeck = tryThisService.findOneRuleDeck( formRuleDeck.getId());
ruleDeck.setRuleDeckId( formRuleDeck.getRuleDeckId());
ruleDeck.setRevision( formRuleDeck.getRevision());
ruleDeck.setName( formRuleDeck.getName());
ruleDeck.setDeckType( formRuleDeck.getDeckType());
ruleDeck.setFileLocation( formRuleDeck.getFileLocation());
ruleDeck = tryThisService.updateOneRuleDeck( ruleDeck);
result.setStatus( true);
result.setMessage("update successful");
} catch (JsonProcessingException e) {
result.setMessage( "JPE:" + e.getMessage());
} catch ( Exception e) {
result.setMessage( "Exception:" + e.getMessage());
}
return result; //should be implicitly converted into json format and send back to the request.
}
在哪里
public class AjaxResponseBody {
@JsonView
private boolean status;
@JsonView
private String message;
// assorted constructors, getters, setters omitted...
}
我的 pom.xml 中确实有 Jackson 数据绑定依赖项
我没有明确配置任何 Http/Json/Xml 消息转换器。
我使用this 页面作为模板。
ajax 调用将 JSON 发送到该方法,该方法确实找到并更新一个现有对象,该对象保存在数据库中,到目前为止一切顺利。 该方法执行没有错误。但是ajax调用总是碰到错误函数,response好像是未定义的,好像方法 实际上并不返回序列化的 AjaxResponseBody 对象。显然,这里的目标是确保在方法没有抛出错误时成功函数被命中,并在方法抛出错误时命中错误函数并提取错误消息。
或者我应该使用一些更好的模式吗?
TIA,
code_warrior
【问题讨论】:
标签: jquery spring-mvc