gpt4 book ai didi

spring - 选择带有对象的标签 - Thymeleaf 和 Spring MVC

转载 作者:行者123 更新时间:2023-12-02 11:56:08 25 4
gpt4 key购买 nike

我正在尝试更改此示例的代码 thymeleafexamples-stsm ,所以我将枚举类型更改为类类型:

类型.java

public class Type { 

private Integer id;
private String type;
...getters and setters
}

SeedStarterMngController.java

@ModelAttribute("allTypes") 
public List<Type> populateTypes() {
Type type1 = new Type();
type1.setId(1);
type1.setType("OUTDOOR");

Type type2 = new Type();
type2.setId(2);
type2.setType("INDOOR");

List<Type> tipos = new ArrayList<Type>();
tipos.add(type1);
tipos.add(type2);
return tipos;
}

seedstarterng.html

<select th:field="*{type}">
<option th:each="type : ${allTypes}" th:value="${type}" th:text="${type.type}">Wireframe</option>
</select>

所以,我无法添加种子发酵剂。

我的输出 html 是

<select id="type" name="type">
<option value="thymeleafexamples.stsm.business.entities.Type@2c08cec0">OUTDOOR</option>
<option value="thymeleafexamples.stsm.business.entities.Type@26cf024">INDOOR</option>
</select>

错误是

Failed to convert property value of type java.lang.String to required type thymeleafexamples.stsm.business.entities.Type for property type; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [thymeleafexamples.stsm.business.entities.Type] for property type: no matching editors or conversion strategy found

如何才能正确映射到类型?我希望你可以帮助我。谢谢。

最佳答案

我知道这个问题很旧,但下面的答案可能会对某人有所帮助,因为我无法轻松找到它。

为了解决这个问题,Thymeleaf 使用 Formatters 在对象和字符串之间进行转换。

  • 在显示阶段 (GET),Formatter Service 会将 Object 转换为字符串。
  • 在提交阶段(POST),Formatter Service 会转换 String返回

    对象。

首先为要在标记中使用的类实现 Formatter 服务:

@Service
public class TypeFormatter implements Formatter<Type> {

@Autowired
TypeService typeService;//Service -> DB

@Override
public String print(Type object, Locale locale) {
return (object != null ? object.getId().toString() : "");
}

@Override
public Type parse(String text, Locale locale) throws ParseException {
Integer id = Integer.valueOf(text);
return this.typeService.get(id);//return Type object form DB
}
}

这是一个非常简单的类,有两个方法:

  • print:将对象转换为字符串。
  • 解析:将字符串转换为对象。

现在,我们必须告诉 Spring-Thymeleaf 我们的格式化程序,或者我们可以将其称为转换器。为此,我们必须在 WebConfig(扩展 WebMvcConfigurerAdapter 的配置类)中注册此格式化程序:

@Configuration
@EnableWebMvc
@ComponentScan(value = { "your package" })
public class WebConfig extends WebMvcConfigurerAdapter {

....
//Formatters

@Autowired //Without autowire, this solution may not work
private TypeFormatter typeFormatter;

@Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatter(typeFormatter);
}
}

现在我们的解决方案已准备好在 html 文件中实现,但如何告诉 Thymeleaf 应用转换?答案是使用 th:field="*{type}" 属性并使用双括号语法 th:value="${{type}}":

<select th:field="*{type}">
<option th:value="NULL" th:text="---Select Type---"></option>
<option th:each="type : ${allTypes}" th:value="${{type}}" th:text="${type.type}">Wireframe</option>
</select>
  • th:field="*{type}" 默认应用已注册的 Formatter 服务。它将类型转换为字符串(这里的字符串将是 Type Id)
  • th:value="${{type}}" 也将类型转换为字符串。
  • 在提交时,Spring会使用Formatter服务将Id转换回来反对。

最后要说的是,有时我们想在下拉列表中添加一个标题,例如“-----选择类型-----”,以防止默认选择并向用户解释。在这种情况下,您必须设置th:value="NULL",除非出现转换错误。

关于spring - 选择带有对象的标签 - Thymeleaf 和 Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25234357/

25 4 0