前台代码
(xxx.html)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form id="iform" th:action="@{/list}" th:method="post" th:object="${user}"><input type="text" th:field="*{name}"/>  
<input type="submit"></form>
</body>
</html>
后台代码
package com.example.demo;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;import com.example.bean.user;@Controller
public class Test { @RequestMapping("/test")
public String login(Model model) {
return "xxx";

}
@RequestMapping("/list")
public String user(@ModelAttribute(value="user")user u, Model model,HttpServletRequest req)  {
String name=u.getName();
 name=req.getAttribute("name").toString();
model.addAttribute("name", name);
return "login";

}
}
数据绑定的bean
package com.example.bean;import javax.validation.constraints.NotEmpty;public class user { @NotEmpty(message="用户名不能为空") 
private String name;
public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @NotEmpty(message="密码不能为空") 
private String pwd;

}
报错信息
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/xxx.html]")
org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "xxx" - line 10, col 20)
 org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "xxx" - line 10, col 20)
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
 Neither BindingResult nor plain target object for bean name 'user' available as request attribute
 ERROR 4672 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/xxx.html]")] with root cause
以上
我是通过
http://localhost:8080/test 进入到xxx.html的xxx里面有个form表单我本来是想通过表单提交数据到后台,但是现在只要进入到xxx.html就报错问题好像是出现在<input type="text" th:field="*{name}"/>  里面的th:field="*{name}" 我在网上找了很多资料但是大多数都不是针对我出现的状况麻烦大佬们指点迷津小弟万分感谢

解决方案 »

  1.   

    取值的时候 应该类似这样 ${user.name}  
      

  2.   

    你把 th:object="${user}" 从form 表单摘出去, 单独设置个div 将 你的input 包起来。 或者按照楼上那么弄。 试试
      

  3.   

    这样就不能和后台bean绑定了啊
      

  4.   

    错误是告诉你,我thymeleaf找不到你要取的值,所以我无法解析这个页面,请检查thymeleaf参数是否接受正确
      

  5.   

    改成@RestController
      

  6.   

    你控制器的 user 方法使用的是 @ModelAttribute(value="user")user u  user对象,而你form表单中传的是 *{name},用 ${user.name}  试试
      

  7.   

    !${name}
      

  8.   

    controller改下:
    @RequestMapping("/test")
    public String login(Model model) {
    model.addAttribute("user",new user());
    return "xxx";}
      

  9.   

    你的页面xxx,但是执行test方法中的model没有赋值,所以${user}是null
      

  10.   


    ///
    (template: "xxx" - line 10, col 20)
    ///
    这个已经指出错误的位置了.
      

  11.   

    你的页面标签中指定标签的name属性了嘛,form格式的请求数据,和jquery的serizabled/serizabledArray异曲同工之妙,都是根据标签的name数据实现传值的效果