问题描述
限时送ChatGPT账号..我使用 Thymeleaf 和 Spring 创建了一个表单并遇到了这个问题.我还阅读了这个问题,但是它对我不起作用
pom.xml
<?xml version="1.0";编码=UTF-8"?><project xmlns="http://maven.apache/POM/4.0.0";xmlns:xsi=http://www.w3/2001/XMLSchema-instance"xsi:schemaLocation=http://maven.apache/POM/4.0.0 https://maven.apache/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><父母><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.3</version><relativePath/><!-- 从存储库中查找父级 --></父母><groupId>mz.painting</groupId><artifactId>mz</artifactId><version>0.0.1-SNAPSHOT</version><name>M&Z 绘画</name><description>M&Z 的 Web 应用程序 </description><属性><java.version>11</java.version></属性><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>org.thymeleaf</groupId><artifactId>百里香</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></依赖><依赖><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring5</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></依赖><依赖><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>运行时</scope></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><范围>测试</范围></依赖><依赖><groupId>javax.validation</groupId><artifactId>validation-api</artifactId></依赖><依赖><groupId>org.hibernate.validator</groupId><artifactId>hibernate-validator</artifactId></依赖></依赖项><构建><插件><插件><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></插件></plugins></build></项目>
控制器更新
@Controller公共类SavingUser{@自动连线私有 UserRepository userRepository;@GetMapping("/addUser")公共字符串添加用户(模型模型){System.out.println("在注册页面添加用户..");model.addAttribute(user", new Customer());返回服务";}@PostMapping("/registerUser")公共字符串用户注册(@ModelAttribute 客户用户,模型模型){System.out.println("用户在注册页面..");model.addAttribute(用户", 用户);//userRepository.save(user);返回重定向:/服务";}
**Bean 类**吸气剂()二传手()toString()
HTML 表单
<form action="registerUser";th:action="@{/registerUser}";th:object="${user}";方法=POST"><br/><label>您的姓名:</label><br/><输入类型=文本"th:field="*{name}";占位符="/><br><label >你的郊区</label><br/><输入类型=文本"th:field="*{suburb}";占位符="/><br/><label>电子邮件</label><br/><输入类型=电子邮件"th:field="*{email}";占位符="/><br/><label>电话</label><br/><输入类型=电话"th:field="*{电话}";占位符="/><br/><label>请告诉我们我们可以如何提供帮助</label><br/><textarea type="text";th:field="*{text}";行=10"cols=40"></textarea><br/><div><输入类=提交";类型=提交"值=提交"/><br/><br/></表单>
完整的错误信息
Servlet.service() for servlet [dispatcherServlet] in context with path [] 抛出异常 [Request processing failed;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "index" - line 86, col 32)] 与根本原因
索引"- 第 86 行,第 32 列)] 在 HTML 表单中是这样的 ->
我还检查了这个解决方案,但它对我不起作用>
知道如何解决这个问题吗?谢谢
解决方案乍一看,
缺少结束标记,Thymeleaf 处理器可能会抱怨.
应该是.注意
的结尾斜线.
您在指定实际原因的地方准确地删除了异常消息,即在 和根本原因 [...]
之后.:)
I have created a form using Thymeleaf and Spring and run into this issue. I also read This question but it didn't work for me
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache/POM/4.0.0" xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache/POM/4.0.0 https://maven.apache/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>mz.painting</groupId>
<artifactId>mz</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>M&Z Painting</name>
<description>Web Application for M&Z </description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Controller Update
@Controller
public class SavingUser{
@Autowired
private UserRepository userRepository;
@GetMapping("/addUser")
public String addUser(Model model){
System.out.println("Adding User in registration page..");
model.addAttribute("user", new Customer());
return "service";
}
@PostMapping("/registerUser")
public String userRegistration(@ModelAttribute Customer user, Model model){
System.out.println("User in registration page..");
model.addAttribute("user", user);
// userRepository.save(user);
return "redirect:/service";
}
**Bean Class**
Getters()
Setters()
toString()
HTML Form
<div id="form">
<form action="registerUser" th:action="@{/registerUser}"
th:object="${user}" method="POST">
<br />
<label>Your Name:</label><br />
<input type="text" th:field="*{name}" placeholder="" /><br>
<label >Your Suburb</label><br />
<input type="text" th:field="*{suburb}" placeholder="" /><br />
<label>Email</label><br/>
<input type="email" th:field="*{email}" placeholder="" /><br />
<label>Phone</label><br/>
<input type="tel" th:field="*{phone}" placeholder="" /><br />
<label>Please tell us how we can help</label><br />
<textarea type="text" th:field="*{text}" rows="10" cols="40"></textarea><br />
<div>
<input class="submit" type="submit" value="Submit" /><br/><br/>
</div>
</form>
</div>
Full Error Message
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "index" - line 86, col 32)] with root cause
"index" - line 86, col 32)] in HTML Form is this ->
<input type="text" th:field="*{name}" placeholder="" /><br>
I also checked this solution and it didn't work for me
Any idea how to fix this ? thank you
解决方案At first glance, the <br>
is missing the closing tag and Thymeleaf processor may complain about it.
It should be
<input type="text" th:field="*{name}" placeholder="" /><br />
. Note the ending slash of <br />
.
You cut out the exception message exactly where it would specify the actual cause, namely after with root cause [...]
. :)
这篇关于Spring with Thymeleaf、BindingResult 或 bean 名称“user"的普通目标对象可用作请求属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论