我将一个列表发送到我的jsp页面,该页面为用户提供选择。这部分做得很好。但是当请求被发送回控制器时为空。我将在控制器中通过@ModelAttribute获取请求,但该对象为null。 Jsp和控制器如下:
<form action="deleteAuthorExecution" method="post">
Select Author: <select name="author">
<option value="all" label="All Authors">All Authors</option>
<c:forEach var="author" items="${authorList}">
<option value="${author}">${author}</option>
</c:forEach>
</select>
<input type="submit" value="submit"/>
</form>
@RequestMapping(value="/deleteAuthorExecution", method = RequestMethod.POST)
protected ModelAndView deleteAuthorExecution(@ModelAttribute Author author) throws Exception {
authorService.delAuthor(author.getAuthorId());
ModelAndView model = new ModelAndView("adminFunctionsPage");
model.addObject("successMsg", "Your request has been processed Successfully.");
return model;
}
作者对象变为空。任何人都可以给我一个提示我代码中的问题是什么?
答案 0 :(得分:0)
我建议您不要发送整个作者对象,也可以传递authorId
<form action="deleteAuthorExecution" method="post">
Select Author: <select name="author">
<option value="0" label="All Authors">All Authors</option>
<c:forEach var="author" items="${authorList}">
<option value="${author.authorId}">${author.authorName}</option>
</c:forEach>
</select>
<input type="submit" value="submit"/>
</form>
@RequestMapping(value="/deleteAuthorExecution", method = RequestMethod.POST)
protected ModelAndView deleteAuthorExecution(@RequestParam Integer authorId) throws Exception {
if(authorId == 0){
//user selected all, so delete all the authors
}
authorService.delAuthor(authorId);
ModelAndView model = new ModelAndView("adminFunctionsPage");
model.addObject("successMsg", "Your request has been processed Successfully.");
return model;
}
希望这有帮助