我在这个问题上花了很多时间,但无法解决。请帮忙!当我从JSP提交表单时,出现此错误。
我的代码:

<form  action="/albums/add" method="post">
<%--<div><input type="file"></div>--%>
<div>Name: <input type="text" name="name" placeholder="Name"></div>
<div>Year: <input type="text" name="year" placeholder="Release year"></div>
<div>
    <select name="singer" id="singer">
    <c:forEach items="${singers}" var="s">
        <option value="${s.id}">${s.name}</option>
    </c:forEach>
</select>
</div>
<input type="submit" >




而我的控制器代码:

@RequestMapping(value = "", method = RequestMethod.GET)
public String albumsGET(ModelMap map) {
    List<Album> albums = albumDAO.getAllAlbums();
    map.addAttribute("albums", albums);
    return "forward:/Albums.jsp";
}

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String albumsAddGET(ModelMap map) {
    map.put("singers", singerDAO.getAllSingers());
    return "forward:/WEB-INF/Pages/Album/AddAlbum.jsp";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String albumsPOST(@ModelAttribute Album album, HttpServletRequest req) {

        Singer s = singerDAO.getSingerById(Long.valueOf(req.getParameter("singer")));
        s.getAlbumList().add(album);
        album.setSinger(s);
        singerDAO.updateSinger(s);
    return "redirect:/albums";
}


}

最佳答案

您可以引发spring提供的自定义异常,使用@ControllerAdvice批注创建类,并使用不同的HTTP状态代码引发自定义异常

10-04 18:08