SpringBoot+Thymeleaf出现的问题

weixin_42889739 2018-08-05 11:02:29
前台代码
(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}" 我在网上找了很多资料但是大多数都不是针对我出现的状况麻烦大佬们指点迷津小弟万分感谢
...全文
4263 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
di_diCN 2020-05-22
  • 打赏
  • 举报
回复
大哥,你解決沒有啊,大家都説是user对象里面的某个属性没有定义,但是我看着都定义了呀,方法都试了,结果没用。
南宫廿贰 2020-04-14
  • 打赏
  • 举报
回复
之前我也是这个错,后来发现Model写成ModelAndView了
试试这样写
lzh0533 2020-03-19
  • 打赏
  • 举报
回复
正解这里,直接使用name属性传值,或者在进入此页面的Controller把空对象通过model.addAttribute("object", new xxx())传入,这里的对象是你上面th:object中的对象xxx.
专注写bug 2019-06-28
  • 打赏
  • 举报
回复
你的页面标签中指定标签的name属性了嘛,form格式的请求数据,和jquery的serizabled/serizabledArray异曲同工之妙,都是根据标签的name数据实现传值的效果
幺客 2019-06-26
  • 打赏
  • 举报
回复
/// (template: "xxx" - line 10, col 20) /// 这个已经指出错误的位置了.
天禄永随 2019-06-21
  • 打赏
  • 举报
回复
你的页面xxx,但是执行test方法中的model没有赋值,所以${user}是null
misaka10024 2019-06-09
  • 打赏
  • 举报
回复
controller改下: @RequestMapping("/test") public String login(Model model) { model.addAttribute("user",new user()); return "xxx"; }
对梦想的牵挂 2019-03-07
  • 打赏
  • 举报
回复
!${name}
闪耀的瞬间 2019-03-05
  • 打赏
  • 举报
回复
你控制器的 user 方法使用的是 @ModelAttribute(value="user")user u user对象,而你form表单中传的是 *{name},用 ${user.name} 试试
pink22pig 2019-03-03
  • 打赏
  • 举报
回复
改成@RestController
TroubleBoy丶 2018-08-09
  • 打赏
  • 举报
回复
错误是告诉你,我thymeleaf找不到你要取的值,所以我无法解析这个页面,请检查thymeleaf参数是否接受正确
William_Wei007 2018-08-06
  • 打赏
  • 举报
回复
你把 th:object="${user}" 从form 表单摘出去, 单独设置个div 将 你的input 包起来。 或者按照楼上那么弄。 试试
遥远的想念 2018-08-06
  • 打赏
  • 举报
回复
取值的时候 应该类似这样 ${user.name}
weixin_42889739 2018-08-06
  • 打赏
  • 举报
回复
引用 2 楼 William_Wei007的回复:
你把 th:object="${user}" 从form 表单摘出去, 单独设置个div 将 你的input 包起来。 或者按照楼上那么弄。 试试
这样就不能和后台bean绑定了啊
weixin_42889739 2018-08-06
  • 打赏
  • 举报
回复
引用 1 楼 遥远的想念的回复:
取值的时候 应该类似这样 ${user.name}
我不是取值我是和后台绑定

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧