使用与胸腺多重选择的不可关系的枚举列表
我最近开始使用胸腺,并且通常情况非常好。但是,我正在尝试使用多个选择来填充包裹在不变的枚举中的列表。
我对其进行了重构,并使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论