Spring with Thymeleaf、BindingResult 或 bean 名称“user"的普通目标对象可用作请求属性

编程入门 行业动态 更新时间:2024-10-28 13:17:15
本文介绍了Spring with Thymeleaf、BindingResult 或 bean 名称“user"的普通目标对象可用作请求属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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&amp;Z 绘画</name><description>M&amp;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 表单中是这样的 ->

  

更多推荐

[db:关键词]

本文发布于:2023-04-30 14:08:20,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   名称   目标   BindingResult

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!