Springboot-java.lang.IllegalStateException:对于bean名称';用户';可用作请求属性
我得到以下错误:Springboot-java.lang.IllegalStateException:对于bean名称';用户';可用作请求属性,spring,spring-mvc,thymeleaf,javabeans,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(M
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;
}
这是thymeleaf页面:
<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>
<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>
和thymeleaf页面:
<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>
<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>
您将获得此异常,因为Spring无法绑定任何
用户
对象。因此,在GET方法中向模型添加一个:
@GetMapping("/invite") //use shorthand
public String showInvitePage(Model model) {
model.addAttribute("user", new User()); //or however you are creating them
return "theFormNameWithoutTheExtension";
}
然后,您的帖子将具有以下处理:
@PostMapping("/register")
public String showRegistrationPage(@ModelAttribute("user") User user) {
//do your processing
return "someConfirmationPage";
}
您获得此异常是因为没有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";
}
然后,您的帖子将具有以下处理:
@PostMapping("/register")
public String showRegistrationPage(@ModelAttribute("user") User user) {
//do your processing
return "someConfirmationPage";
}
谢谢,但是为什么它在register方法中工作而不将对象添加到get方法中,而在invite get方法中却不工作呢?
showInvitePage
在请求URL时(最终)被调用。单击“提交”按钮时,您正在调用showRegistrationPage
。因此,您本质上是在为Spring提供一个bean来“存储”数据。谢谢,但是为什么它在register方法中工作,而不将对象添加到get方法,而在invite get方法中却不工作呢?showInvitePage
在请求URL时(最终)被调用。单击“提交”按钮时,您正在调用showRegistrationPage
。因此,您实际上是在为Spring提供一个bean来“存储”数据。