我是Spring框架的新手。目前,我正在使用注释进行spring验证。
所以看看我的DAO课程:
gulp.task('js', function () {
var modularizedLibraries =
[
{ file: 'react', expose: 'react' },
{ file: 'react-router', expose: 'react-router' },
{ file: 'jquery', expose: 'jquery' },
{ file: 'bootstrap', expose: 'bootstrap' }
];
modularizedLibraries.map(function (file) {
return browserfy(file)
.require(file.file, { expose: file.expose })
.bundle()
.pipe(source(file.file))
.pipe(rename({ dirname: '', extname: '.js' }))
.pipe(gulp.dest(config.paths.dest.js + "/libs"));
});
这是我的控制者:
public class Spitter {
private Long id;
@NotNull(message = "Username cannot be null")
@Size(min = 10, max = 14, message = "Username must be between 10 and 14 characters long")
private String username;
SETTERS AND GETTERS }
和JSP文件:
@Controller
@RequestMapping("/spitters")
public class SpitterController {
@RequestMapping(value = "/edit", method=RequestMethod.GET)
public String createSpitterProfile(Model model) {
model.addAttribute("spitter", new Spitter());
return "spitters/edit";
}
@RequestMapping(value = "/edit/createAccount", method = RequestMethod.POST)
public String addSpitterFromForm(Model model, @Valid @ModelAttribute("spitter")Spitter spitter, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "spitters/edit";
} else {
// spitterService.addSpitter(spitter);
return "redirect:/home";
}
}
}
但是,Spitter Controller可以从jsp表单接收数据。但DAO类中添加的约束(@NotNull和@Size)不起作用,我不知道为什么。
答案 0 :(得分:1)
请更加了解非工作约束。一个例子 会有所帮助
也许您的bean数据有效且username只是空字符串。我认为您使用Hibernate Validator,如果是这样,请尝试将@NotEmpty约束添加到用户名字段
答案 1 :(得分:0)
我相信你需要注册bean MethodValidationPostProcessor
。见Spring Docs