Spring,使用表单打开页面时出现IllegalStateException

时间:2014-04-07 20:50:35

标签: java spring jsp spring-mvc

我是Spring新手,在使用浏览器输入campaigns.jsp时,我遇到了这个例外。

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'campaign' available as request attribute

我有这个控制器:

@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

的地址

1 个答案:

答案 0 :(得分:2)

<form:form method="post" action="create" commandName="campaign">

需要模型属性(或命令对象),实际为HttpServletRequest属性,名称为campaign以用作模板对于表单字段。

您尚未添加具有该名称的请求属性。您需要在呈现视图之前执行此操作。例如

@RequestMapping(value = "/secure/campaigns.jsp", method = RequestMethod.GET)
public String campaigns(Model model) {
    model.addAttribute("campaign", new Campaign());
    return "campaigns";
} 

该对象不需要设置任何字段,因为它仅用作模板。