Java 默认情况下,Thymeleaf是否按逗号拆分字符串? Spring+Thymeleaf字符串拆分问题
大家好,我在Thymeleaf中使用multiselect时遇到了这个问题。Multiselect接受字符串数组作为选项,而某些字符串包含逗号。例如Java 默认情况下,Thymeleaf是否按逗号拆分字符串? Spring+Thymeleaf字符串拆分问题,java,spring,thymeleaf,Java,Spring,Thymeleaf,大家好,我在Thymeleaf中使用multiselect时遇到了这个问题。Multiselect接受字符串数组作为选项,而某些字符串包含逗号。例如 ["test,first,frist", "test,second,second", "test,third,third"] 等等。如果我选择多个选项,例如第一个和第三个,则不会出现问题: selected = ["test,first,frist", "t
["test,first,frist", "test,second,second", "test,third,third"]
等等。如果我选择多个选项,例如第一个和第三个,则不会出现问题:
selected = ["test,first,frist", "test,third,third"]
但当我仅选择一个时,字符串将自动拆分,结果为:
selected = ["test", "first", "first"]
有人遇到过这个问题吗?非常感谢
编辑:
问题在于默认的Spring转换。这应该可以解决问题
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.removeConvertible(String.class, Collection.class);
registry.addConverter(String.class, Collection.class, noCommaSplitStringToCollectionConverter());
}
public Converter<String, Collection> noCommaSplitStringToCollectionConverter() {
return Collections::singletonList;
}
}
@配置
公共类WebConfig扩展了WebMVCConfigureAdapter{
@凌驾
公共void addFormatters(FormatterRegistry注册表){
registry.removeConvertible(String.class、Collection.class);
addConverter(String.class、Collection.class、noCommaSplitStringToCollectionConverter());
}
公共转换器noCommaSplitStringToCollectionConverter(){
返回集合::singletonList;
}
}