Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将自定义对象从thymeleaf视图传递到控制器_Java_Html_Spring_Model View Controller_Thymeleaf - Fatal编程技术网

Java 将自定义对象从thymeleaf视图传递到控制器

Java 将自定义对象从thymeleaf视图传递到控制器,java,html,spring,model-view-controller,thymeleaf,Java,Html,Spring,Model View Controller,Thymeleaf,试图使用toth:value将自定义对象从视图传递到控制器,但只能通过th:value传递Java的基本数据类型。有没有办法通过th:value 我是春天的新手,尝试一些随机的事情。 它是一个简单的web应用程序 我想做的是: 我有一个创建团队的表格。该表单包括一个用于团队名称的文本字段和一组用于选择团队成员的复选框。对于每个选中的复选框,应将相应的成员添加到团队列表中 我尝试使用thymeleaf的th:value属性将对象从视图传递到控制器。但我只能使用th:value传递字符串值 team

试图使用to
th:value
将自定义对象从视图传递到控制器,但只能通过
th:value
传递Java的基本数据类型。有没有办法通过
th:value

我是春天的新手,尝试一些随机的事情。 它是一个简单的web应用程序

我想做的是: 我有一个创建团队的表格。该表单包括一个用于团队名称的文本字段和一组用于选择团队成员的复选框。对于每个选中的复选框,应将相应的成员添加到团队列表中

我尝试使用thymeleaf的
th:value
属性将对象从视图传递到控制器。但我只能使用th:value传递字符串值

teamCreationForm.html

创建您的团队
创建你的梦想团队
队员姓名
球员姓名
提交你的团队
CreateTeamController.java
package com.example.kkvamshee.Cricket.web;
导入com.example.kkvamshee.Cricket.Player;
导入com.example.kkvamshee.Cricket.Team;
导入com.example.kkvamshee.Cricket.data.JdbcPlayerRepository;
导入lombok.extern.slf4j.slf4j;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.validation.Errors;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.PostMapping;
导入org.springframework.web.bind.annotation.RequestMapping;
导入javax.validation.Valid;
导入java.util.List;
@Slf4j
@控制器
@请求映射(“/创建您的团队”)
公共类CreateTeamController{
....
@GetMapping
公共字符串showSelectTeamPage(模型){
List playersList=playerRepo.findAll();
model.addAttribute(“playersList”,playersList);
model.addAttribute(“团队”,新团队());
返回“teamCreationForm”;
}
.....
}
Team.java
package com.example.kkvamshee.Cricket;
导入龙目数据;
导入javax.validation.constraints.NotNull;
导入javax.validation.constraints.Size;
导入java.util.List;
@资料
公开课小组{
@NotNull私有字符串名称;
@大小(最小值=3,最大值=3,message=“团队中只能有3名成员”)
私人名单玩家;
}
错误日志 在thid错误日志的末尾,没有“找到匹配的编辑器或转换策略”


我尝试将
th:valueType
更改为
Integer
,它成功地将相应的文本转换为整数值。但我在使用团队对象或任何其他定制对象时也会遇到此铸造错误。

Thymeleaf可以知道您正在使用哪个对象,特别是如果您正确映射并正确接受它。看看我对你的代码做的测试

请注意表单中如何介绍该操作


模板引擎必须将
标记转换为HTML(浏览器可以理解),转换后的标记看起来像

因此,视图不知道对象
Player
。该值只是视图的一个简单字符串。Spring需要一些转换器将字符串转换为适当的播放器对象

这可以通过实现
转换器
接口,然后在我们的应用程序中注册转换器来实现

转换器接口的实现:
package com.example.kkvamshee.cricket.config;
导入com.example.kkvamshee.cricket.Player;
导入lombok.extern.slf4j.slf4j;
导入org.springframework.core.convert.converter.converter;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
@Slf4j
公共类StringToPlayerConverter实现转换器{
@凌驾
公共播放器转换(字符串源){
String pattern=“玩家\(姓名=([a-zA-z0-9]+),年龄=([1-9]+),经验=([1-9]+)\”;
Matcher matcherResult=Pattern.compile(Pattern.Matcher)(源代码);
log.info(“regex结果:+matcherResult.find());
字符串名称=matcherResult.group(1);
int age=Integer.parseInt(matcherResult.group(2));
int exp=Integer.parseInt(matcherResult.group(3));
返回新玩家(姓名、年龄、经验);
}
}
注册上述转换器对象:
package com.example.kkvamshee.cricket.config;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.format.FormatterRegistry;
导入org.springframework.web.servlet.config.annotation.WebMVCConfiguer;
@配置
公共类WebConfig实现WebMVCConfiguer{
@凌驾
公共void addFormatters(FormatterRegistry注册表){
addConverter(新的StringToPlayerConverter());
}
}
有多种方法可以实现这种类型转换


有关更多详细信息,请参阅

感谢您的努力,但在我的情况下,错误仍然是一样的。添加th:action属性并没有显著改变。
Field error in object 'team' on field 'players': rejected value [Player(name=vamshee, age=20, exp=1),Player(name=messi, age=32, exp=18),Player(name=ronaldo, age=34, exp=18)]; codes [typeMismatch.team.players,typeMismatch.players,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [team.players,players]; arguments []; default message [players]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 'players'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.example.kkvamshee.Cricket.Player' for property 'players[0]': no matching editors or conversion strategy found]