是否可以通过javax.validation.constraints
任何方式使用验证器来验证来自程序包的请求参数?即如下所示:
@Controller
public class test {
@RequestMapping("/test.htm")
public String test(@RequestParam("name") @NotNull String name)
{
return "index";
}
}
使用这种方式:
public class Comment{
@NotEmpty
@Length(max = 140)
private String text;
//Methods are omitted.
}
现在在控制器中使用@Valid
@Controller
public class CommentController {
@RequestMapping(value = "/api/comment", method = RequestMethod.POST)
@ResponseBody
public Comment add(@Valid @RequestBody Comment comment) {
return comment;
}
}
当您在cotroller中将@Valid用于Comment对象时,它将应用Comment类中提到的验证及其属性,例如
@NotEmpty
@Length(max = 140)
private String text;
您也可以查看此方法,以了解其他替代方法:http : //techblogs4u.blogspot.in/2012/09/method-parameter-validation-in-spring-3.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句