我尝试将数据html传递给控制器,但我有这样的错误。我的GET方法工作清楚,但POST方法不起作用
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'post' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:401) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:328) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:294) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.processAttribute(AbstractSpringFieldAttrProcessor.java:98) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.dom.Node.processNode(Node.java:972) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
这是我的html表单
<form autocomplete="off" action="#" th:action="@{/(${titleId})}"
th:object="${post}" method="post" class="form-horizontal"
role="form">
<h2>Formu doldur aramıza katıl..</h2>
<div class="form-group">
<div class="col-sm-9">
<input type="text" th:field="*{postBody}" placeholder="yorumla"
class="form-control" />
</div>
</div>
<div class="form-group">
<div class="col-sm-9">
<button type="submit" class="btn btn-primary btn-block">Kayıt Ol</button>
</div>
</div>
</form>
最后我的控制器方法就在这里
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public ModelAndView savePost(@Valid Post post,@PathVariable("id")Long id){
ModelAndView modelAndView =new ModelAndView();
Title title = titleService.getTitleById(id);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
Date date = new Date();
Post post1 = new Post();
post1.setPostBody(post.getPostBody());
post1.setPostDate(date);
post1.setPostSender(user);
post1.setPostTitle(title);
postService.savePost(post1);
modelAndView.addObject("post",post1);
modelAndView.setViewName("homePage2");
return modelAndView;
}
我无法理解为什么会有错误。
答案 0 :(得分:0)
根据您的堆栈跟踪:
java.lang.IllegalStateException:既不是BindingResult也不是plain bean名称'post'的目标对象可用作请求属性
您的问题在于:@Valid Post post
在你的处理程序方法。看来,由于某种原因,Spring无法将数据绑定到您发布的表单中的该对象。
答案 1 :(得分:0)
将@ModelAttribute添加到您的控制器
public ModelAndView savePost(@Valid @ModelAttribute("post") Post post,@PathVariable("id")Long id)
并检查您是否在GET上的模型中添加了Post实例。
@RequestMapping(method = RequestMethod.GET)
public String postPage(Model model) {
model.addAttribute("post", new Post());
return "login";
}