我是 Spring 新手,在使用浏览器输入 Campaigns.jsp 时遇到此异常。
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'campaign' available as request attribute
我有这个 Controller :
@Controller
@RequestMapping(value = "/admin")
public class AdminIndexController {
@RequestMapping(value = "/secure/campaigns.jsp", method = RequestMethod.GET)
public String campaigns() {
return "campaigns";
}
@RequestMapping(value = "/secure/create", method = RequestMethod.POST)
public String addContact(@ModelAttribute("campaign")
Campaign campaign) {
return "campaigns";
}
}
Campaings.jsp(如果我删除这部分,它会正确显示页面):
<form:form method="post" action="create" commandName="campaign">
<table>
<tr>
<td><form:label path="question">Question</form:label></td>
<td><form:input path="question" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Campaign"/>
</td>
</tr>
</table>
</form:form>
我认为映射 action="create"
存在问题,我不确定它指向哪里。我认为它指向与 .jsp 相同的地方。这是我使用的地址 http://localhost:8080/server/rest/admin/secure/campaigns.jsp
最佳答案
这个
<form:form method="post" action="create" commandName="campaign">
需要一个模型属性(或命令对象),实际上是一个HttpServletRequest
属性,名称为campaign
用作表单字段的模板。
您尚未添加具有该名称的请求属性。您需要在渲染 View 之前执行此操作。例如
@RequestMapping(value = "/secure/campaigns.jsp", method = RequestMethod.GET)
public String campaigns(Model model) {
model.addAttribute("campaign", new Campaign());
return "campaigns";
}
该对象不需要设置任何字段,因为它仅用作模板。
关于java - Spring,使用表单打开页面时出现 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22922834/