jsr303jsp页面怎么显示错误信息,使用Spring MVC提交表单之前显示JSR-303错误

Is there an easy way in Spring MVC 3.x to display form error messages (obtained by JSR303 validation), before submiting the form ?

Consider the example code at the end of this post.

The end-user is supposed to edit forms in which the initial data is already invalid.

Errors are properly displayed when the form is submitted (POST method), but not on initial form display (GET method).

Is there an easy way to display the errors on initial form display (GET method) ? (Is there a way to re-use the form:errors tag for this purpose?)

JSP View form1.jsp:

There are errors:

Field1:

Field2:

Ok

Controller:

@Controller @SessionAttributes("form1")

public class Form1Controller {

@ModelAttribute("form1") public Form1Bean createForm1() { return new Form1Bean(); }

@RequestMapping(value = "/form1/edit", method = RequestMethod.GET)

public String getEdit(Model model) {

// here we pretend to get form1 from database, and store it in session.

// form1 in database may have invalid field values.

// Perform a JSR-303 validation here ?

// MAIN QUESTION: What's the easy way to add errors to model and display them ?

return "/form1";

}

@RequestMapping(value = "/form1/edit", method = RequestMethod.POST)

public String postEdit(@Valid @ModelAttribute("form1") Form1Bean form1, BindingResult result, Model model) {

if (result.hasErrors()) {

return "/form1";

} else {

return "redirect:/done";

}

}

}

Backing Bean:

public class Form1Bean {

@Size(min=4,max=10) private String field1; // getters and setters ommited for brevity

@Size(min=4,max=10) private String field2;

public Form1Bean() {

this.field1 = "bad"; this.field2="good"; // start with an invalid field1

}

//...

}

Edit: After the interpreting the answer from @jb-nizet, here is the complete controller source:

@Controller @SessionAttributes("form1")

public class Form1Controller {

@Autowired org.springframework.validation.Validator validator;

@ModelAttribute("form1") public Form1Bean createForm1() { return new Form1Bean(); }

@RequestMapping(value = "/form1/edit", method = RequestMethod.GET)

public String getEdit(@ModelAttribute("form1") Form1Bean form1, Errors errors, Model model) {

// here we pretend to get form1 from database, and store it in session.

// form1 in database may have invalid field values.

validator.validate(form1, errors);

return "/form1";

}

@RequestMapping(value = "/form1/edit", method = RequestMethod.POST)

public String postEdit(@Valid @ModelAttribute("form1") Form1Bean form1, BindingResult result, Model model) {

if (result.hasErrors()) {

return "/form1";

} else {

return "redirect:/done";

}

}

}

Made a few tests, and it seems to work! Than you @jb-nizet

解决方案

Not tested, but as I understand the documentation, you would just have to inject an object of type org.springframework.validation.Validator in your controller, create and bind an Errors instance as explained in Add Error on GET METHOD Spring annotated controller, and invoke the validator's validate() method, passing the form and the Errors as argument.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值