Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
spring引导-将数据html传递给控制器_Spring_Spring Mvc_Spring Boot_Thymeleaf - Fatal编程技术网

spring引导-将数据html传递给控制器

spring引导-将数据html传递给控制器,spring,spring-mvc,spring-boot,thymeleaf,Spring,Spring Mvc,Spring Boot,Thymeleaf,我试图将数据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>

我试图将数据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]

我不明白我为什么会出错。

根据您的堆栈跟踪:

java.lang.IllegalStateException:既不是BindingResult也不是plain bean名称“post”的目标对象可用作请求属性

您的问题是:
@有效帖子

在处理程序方法上。似乎出于某种原因,Spring未能将数据从发布的表单绑定到该对象中。

将@modeldattribute添加到控制器中

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";
}
public ModelAndView savePost(@Valid @ModelAttribute("post") Post post,@PathVariable("id")Long id)
@RequestMapping(method = RequestMethod.GET)
public String postPage(Model model) {
     model.addAttribute("post", new Post());
     return "login";
}