java.lang.IllegalStateException:BindingResult和普通目标对象都不可用作请求属性

时间:2014-03-28 23:07:40

标签: java spring spring-mvc

当我尝试提交(Spring)表单查询时:

http://i.imgur.com/6BgiW7Q.png

我没有收到消息。

控制器

@RequestMapping(value = "beerbean", method = RequestMethod.GET)
public String showForm(@ModelAttribute("beerbean") BeerBean beerbean){
    return "addbeerform";
}



@RequestMapping(value = "beerbean", method = RequestMethod.POST)
public String newBeer(Model model, @Valid @ModelAttribute BeerBean beerbean,
        BindingResult bindingResult){
    if(bindingResult.hasErrors()){
        return "addbeerform";
    }
    model.addAttribute("beani", beerbean);
    return "showBeer";
}

addbeerform.jsp

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<form:form commandName="beerbean" action="${pageContext.request.contextPath}/app
     /beerbean" method="POST">

     <form:input path="name" /><form:errors path="name" /><br/>
     <form:input path="id" /><form:errors path="id" /><br/>

     <input type="submit">
</form:form>

当字段中没有错误时,它会工作并转到&#34; showBeer&#34;,但是当它出现故障时就会崩溃。我也有实现Serializable等的BeerBean类,但是不认为解决方案在那里(变量有类似参数的形式,输入路径=&#34;名称&#34;转到私有String名称等)

1 个答案:

答案 0 :(得分:0)

我建议明确指定模型属性的名称:

public String newBeer(
    Model model,
    @Valid @ModelAttribute("beerbean") BeerBean beerbean,
    BindingResult bindingResult
) {