我正在研究一个项目,在Spring-MVC项目中添加一些Ajax。
<form:errors path="fieldName"/>
这将插入 JSP 错误, 现在我试图做一些 Ajax 代码来显示页面上的错误。 有人能让我如何更新以下代码来显示错误信息而不是错误吗?
success: function(response)
{
// we have the response
$( #error ).hide( fast );
if(response.status == "SUCCESS")
{
$( #error ).hide( fast );
alert("Thanks for submitting. We will get back to you as soon as possible.
" + response.result);
$( #error ).hide( slow );
时 时else
{
errorInfo = "";
for( i = 0 ; i < response.result.length ; i++){
errorInfo += "<br>" + (i + 1) +". " + response.result[i].code;
时 时
$( #error ).html("Please correct following errors: " + errorInfo);
$( #info ).hide( slow );
$( #error ).show( slow );
$( #input ).show( slow );
时 时
时 时,
error: function(e){
alert( Error: + e);
时 时
时 时);
上述代码用来显示错误, 而不是错误信息
下面是我的 Java 代码 :
@RequestMapping(value = "/ajax.html", method = RequestMethod.POST)
public @ResponseBody
JsonResponse addMessage(@Valid
@ModelAttribute(value = "memberrequest") MemberRequest memberrequest,
BindingResult result) {
JsonResponse res = new JsonResponse();
if (!result.hasErrors()) {
res.setStatus("SUCCESS");
// Setting data over to RT
String Ticket = zzz.sentWebRequest(memberrequest);
/*
* Setting out the ticket number to be displayed to user
*/
Map<String, Object> model = new HashMap<String, Object>();
Ticket t = new Ticket();
t.setTicketDetails(Ticket);
model.put("ticket", t);
res.setResult(Ticket);
时 时 else {
res.setStatus("FAIL");
res.setResult(result.getAllErrors());
时 时
return res;
时 时
时 时
JSON类:
public class JsonResponse { private String status = null; private Object result = null;
公共字符串获取状态 () { 返回状态;时 时
公共无效设置状态( Status (String status)) { { this. status = status;时 时
公共对象获得Result () { 返回结果;时 时
公共无效设置Result( 对象结果) {此处。 结果=结果;时 时
时 时