Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 默认情况下,Thymeleaf是否按逗号拆分字符串? Spring+Thymeleaf字符串拆分问题_Java_Spring_Thymeleaf - Fatal编程技术网

Java 默认情况下,Thymeleaf是否按逗号拆分字符串? Spring+Thymeleaf字符串拆分问题

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

大家好,我在Thymeleaf中使用multiselect时遇到了这个问题。Multiselect接受字符串数组作为选项,而某些字符串包含逗号。例如

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