选择带有对象的标签-Thymeleaf和Spring MVC [英] Select Tag with Object - Thymeleaf and Spring MVC

查看:330
本文介绍了选择带有对象的标签-Thymeleaf和Spring MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to change the code of this example thymeleafexamples-stsm, so I changed enum type for class type:

Type.java

Type.java

public class Type { 

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

SeedStarterMngController.java

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; 
    } 

seedstartermng.html

seedstartermng.html

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

因此,我无法添加Seed Starter.

So, I can't Add Seed Starter.

我的输出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>

错误是

无法将类型为java.lang.String的属性值转换为必填项 键入thymeleafexamples.stsm.business.entities.Type作为属性类型; 嵌套的异常是java.lang.IllegalStateException:无法转换 类型[java.lang.String]的值转换为必需的类型 属性类型的[thymeleafexamples.stsm.business.entities.Type]:否 找到匹配的编辑器或转换策略

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

如何进行映射以正确键入?我希望你能帮助我.谢谢你.

How I can do to be mapped to type correctly? I hope you can help me. Thank you.

推荐答案

该错误消息基本上说Spring不知道如何将字符串thymeleafexamples.stsm.business.entities.Type@2c08cec0转换为Type的实例.这是您代码中的错误,因为尝试这样做没有任何意义.

That error message basically says Spring don't know how to convert the string thymeleafexamples.stsm.business.entities.Type@2c08cec0 into an instance of Type. This is a bug on your code because it doesn't make any sense trying to do so.

您不应该将Object的toString()值用作表单下拉标识符.您需要一种(更好)更好的策略来使代码识别用户选择的类型.

You're not supposed to use the toString() value of Object as a form dropdown identifier. You need to have a (much) better strategy for the code to identify the Type selected by user.

常见方法是使用id属性:

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

提交表单后,您需要根据控制器上ID的ID名称撤消Type的实例

When the form is submitted, you then need to retireve an instance of Type based on its id name on your controller

这篇关于选择带有对象的标签-Thymeleaf和Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆