Tomcat在验证期间显示HTTP状态400错误页面

时间:2015-09-30 00:09:06

标签: spring thymeleaf bean-validation

我正在学习如何使用Thymeleaf作为默认视图在Spring MVC中使用Bean Validation。可以正确保存每个有效数据。但是当我尝试传递无效数据时,Tomcat只显示了HTTP Status 400 Error页面。在Tomcat控制台中显示了类似验证的内容,但只是在Tomcat控制台中记录了文本。这是保存数据(项目)的控制器。

@Controller
@RequestMapping("/item")
@SessionAttributes("item")
public class ItemController {
    @Autowired
    private ItemService itemService;
    @Autowired
    private ColorService colorService;

    @ModelAttribute("allColors")
    public List<Color> populateColors() {
        return colorService.findAll();
    }

    @ModelAttribute("allItems")
    public List<Item> populateItems() {
        return itemService.findAll();
    }

    @RequestMapping(value = {"/image/{id}", "image/{id}"})
    @ResponseBody
    public byte[] showImage(@PathVariable("id") String id) {

        return itemService.getItem(id).getImage();
    }

    @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
    public String showAllItems() {

        return "itemList";
    }

    @RequestMapping(value = {"add", "/add"}, method = RequestMethod.GET)
    public String showItemAddForm(Model model) {
        model.addAttribute("item", new Item());

        return "itemAddForm";
    }

    @RequestMapping(value = {"add", "/add"}, method = RequestMethod.POST)
    public String processAddItem(
            @ModelAttribute("item") @Valid Item item,
            RedirectAttributes model,
            BindingResult errors,
            SessionStatus session) {
        if (errors.hasErrors()) {
            return "itemAddForm";
        }

        itemService.saveItem(item);
        session.setComplete();

        model.addFlashAttribute("message", "Item has been added");
        return "redirect:/item";
        }
    }

控制器有问题吗?我应该如何使Bean Validation与Spring和Thymeleaf一起使用?

0 个答案:

没有答案