使用与胸腺多重选择的不可关系的枚举列表

发布于 2025-02-01 03:49:20 字数 3107 浏览 1 评论 0原文

我最近开始使用胸腺,并且通常情况非常好。但是,我正在尝试使用多个选择来填充包裹在不变的枚举中的列表。

我对其进行了重构,并使用POJO进行了工作:

public enum Place {

    FIRST("First"),
    SECOND("Second"),
    THIRD("Third"),
    FOURTH("Fourth"),
    FIFTH("Fifth");

    private final String formatted;

    Place(String s) {
        formatted = s;
    }
    
    public String getFormatted() {
        return formatted;
    }
}
public class Places {

    private List<Place> places;
    // get/set/constructors omitted 


}
    @GetMapping("/complex")
    public ModelAndView complex() {
        Places places =  new Places(Place.THIRD);
        ModelAndView model = new ModelAndView("complex");
        model.addObject("filter", places);
        return model;
    }

    @RequestMapping(value = "/complex",
            method = RequestMethod.POST)
    public ModelAndView multiPost(@ModelAttribute Places places, ModelAndView model) {
        model.addObject("filter", places);
        model.setViewName("complex");
        System.out.println("post " + places.getPlaces());
        return model;
    }
    <form id="main_form" class="mainform"
          method="post" th:action="@{/complex}"
          th:object="${filter}">
        <div class="col-md-3">
            <label id="include">Included</label>
            <select class="form-select" aria-label=".form-select-sm example" multiple="multiple"
                    th:field="*{{places}}">
                <option th:each="state : ${T(uk.co.enums.Place).values()}"
                        th:value="${state}"
                        th:text="${state.getFormatted()}">
                </option>
            </select>
        </div>
        <input type="submit" value="Create">
    </form>

但是,我希望能够使用通过Immutables库创建的对象来替换这些位置类。

@Value.Immutable
public interface Places {

    List<Place> places();
}

但是,这会产生:

Caused by: org.springframework.core.convert.ConversionFailedException: 
Failed to convert from type [java.lang.String] to type [com.google.common.collect.ImmutableList<uk.co.enums.Place>] for value 'FIRST'; 
nested exception is java.lang.IllegalArgumentException: 
Could not instantiate Collection type: com.google.common.collect.ImmutableList

我创建了转换器(StringTolistPlaceConverter和StringArrtopLacesConverter),并将它们添加到有效的格式注册表中,但最终与拥有POJO的长度相似,但周围有额外的类。这些似乎还需要明确使用不可投射的人,这对我来说是错误的:

public class StringToListPlaceConverter implements Converter<String, ImmutableList<Place>> {

    @Override
    public ImmutableList<Place> convert(String from) {
        System.out.println("from = " + from);
        return ImmutableList.of(Place.valueOf(from));
    }

}

我是否错过了对不可公准的注释的东西?还是在网络和Java侧接口时需要保持尽可能平淡的东西?

I've started using Thymeleaf recently, and generally been getting on pretty well. However I'm trying to use a multiple select to populate a list of Enums that are wrapped up in an immutable.

I refactored it and it works using a POJO:

public enum Place {

    FIRST("First"),
    SECOND("Second"),
    THIRD("Third"),
    FOURTH("Fourth"),
    FIFTH("Fifth");

    private final String formatted;

    Place(String s) {
        formatted = s;
    }
    
    public String getFormatted() {
        return formatted;
    }
}
public class Places {

    private List<Place> places;
    // get/set/constructors omitted 


}
    @GetMapping("/complex")
    public ModelAndView complex() {
        Places places =  new Places(Place.THIRD);
        ModelAndView model = new ModelAndView("complex");
        model.addObject("filter", places);
        return model;
    }

    @RequestMapping(value = "/complex",
            method = RequestMethod.POST)
    public ModelAndView multiPost(@ModelAttribute Places places, ModelAndView model) {
        model.addObject("filter", places);
        model.setViewName("complex");
        System.out.println("post " + places.getPlaces());
        return model;
    }
    <form id="main_form" class="mainform"
          method="post" th:action="@{/complex}"
          th:object="${filter}">
        <div class="col-md-3">
            <label id="include">Included</label>
            <select class="form-select" aria-label=".form-select-sm example" multiple="multiple"
                    th:field="*{{places}}">
                <option th:each="state : ${T(uk.co.enums.Place).values()}"
                        th:value="${state}"
                        th:text="${state.getFormatted()}">
                </option>
            </select>
        </div>
        <input type="submit" value="Create">
    </form>

However, I'd like to be able to use an object created through the immutables library to replace the Places class.

@Value.Immutable
public interface Places {

    List<Place> places();
}

However this produces:

Caused by: org.springframework.core.convert.ConversionFailedException: 
Failed to convert from type [java.lang.String] to type [com.google.common.collect.ImmutableList<uk.co.enums.Place>] for value 'FIRST'; 
nested exception is java.lang.IllegalArgumentException: 
Could not instantiate Collection type: com.google.common.collect.ImmutableList

I created the converters (StringToListPlaceConverter and StringArrToPlacesConverter) and added them to the formatter registry which works, but ends up being similar in length to having a POJO, but with extra classes dotted around. These also seemed to require explicit use of the ImmutableList, which feels wrong to me:

public class StringToListPlaceConverter implements Converter<String, ImmutableList<Place>> {

    @Override
    public ImmutableList<Place> convert(String from) {
        System.out.println("from = " + from);
        return ImmutableList.of(Place.valueOf(from));
    }

}

Am I missing something annotation-wise with the immutables? Or is this something that needs to stay as plain as possible while interfacing between the web and java-side?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文