SpringBoot+Thymeleaf出现的问题

weixin_42889739 weixin_42889739 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
前台代码
(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}" 我在网上找了很多资料但是大多数都不是针对我出现的状况麻烦大佬们指点迷津小弟万分感谢
...全文
评论 15
收藏 已收藏
分享
返回顶部
weixin_42889739
Web 开发 81,108 成员 341,721 内容