Springboot - java.lang.IllegalStateException:既不是BindingResult也不是bean名称的普通目标对象' user'可用作请求属性

时间:2018-01-23 19:04:29

标签: spring spring-mvc thymeleaf javabeans

我收到以下错误:

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

当我尝试调用此方法时:

@RequestMapping(value="/invite", method = RequestMethod.GET)
public ModelAndView showInvitePage(ModelAndView modelAndView,@ModelAttribute("user") User user){
    return modelAndView;
}

这是百里香的页面:

<div class="container">
<div class="wrapper">
    <form class="form-activate" th:action="@{/invite}" method="post" th:object="${user}">
        <h2 class="form-activate-heading">Nodig een student uit</h2>
        <p>Vul hier het e-mailadres in van de student die je wil uitnodigen:</p>
        <div class="form-group">
            <input type="text" th:field="*{username}" class="form-control input-lg"
                   placeholder="Username" tabindex="1"/>
        </div>
        <div class="form-group">
            <input type="text" th:field="*{email}" class="form-control input-lg"
                   placeholder="Username" tabindex="2"/>
        </div>
        <div class="row">
            <div class="col-xs-6 col-sm-6 col-md-6">
                <input type="submit" class="btn btn-secondary" value="Invite"/>
            </div>
        </div>
    </form>
</div>

这很奇怪,因为我有另一种方法几乎就是这个方法的副本,它在这里工作得非常好:

@RequestMapping(value="/register", method = RequestMethod.GET)
public ModelAndView showRegistrationPage(ModelAndView modelAndView, @ModelAttribute User user){
    return modelAndView;
}

和百里香的页面:

<div class="wrapper">
<form class="form-signin" th:action="@{/register}" method="post" th:object="${user}">
    <h2 class="form-signin-heading">Registratie</h2>
            <div class="form-group">
                <input type="text" th:field="*{username}" class="form-control input-lg"
               placeholder="Username" tabindex="1"/>
            </div>
            <div class="form-group">
                <input type="text" th:field="*{email}" class="form-control input-lg"
               placeholder="Email" tabindex="2"/>
            </div>
    <div class="form-group">
        <input type="password" th:field="*{encryptedPassword}" id="password" class="form-control input-lg"
               placeholder="Password" tabindex="3"/>
    </div>
    <div class="form-group">
        <input type="password" name="password_confirmation" id="password_confirmation"
               class="form-control input-lg" placeholder="Confirm Password" tabindex="4"/>
    </div>

我唯一想到的是当你调用invite方法时,用户已经登录并正在进行实际的邀请。注册时,尚未登录任何用户。

编辑:

我从输入字段中删除了thymeleaf th:字段并使用了经典方式,现在它运行正常。

<div class="form-group">
            <input type="text" name="username" id="username" class="form-control input-lg"
                   placeholder="Username" tabindex="2"/>
</div>
<div class="form-group">
            <input type="text" name="email" id="email" class="form-control input-lg"
                   placeholder="Email" tabindex="2"/>
</div>

1 个答案:

答案 0 :(得分:2)

您将收到此异常,因为Spring没有绑定的user对象。因此,在GET方法中为模型添加一个:

@GetMapping("/invite") //use shorthand
public String showInvitePage(Model model) {
    model.addAttribute("user", new User()); //or however you are creating them 
    return "theFormNameWithoutTheExtension";
}

然后你的POST将进行处理:

@PostMapping("/register")
public String showRegistrationPage(@ModelAttribute("user") User user) {
    //do your processing
    return "someConfirmationPage";
}