自定义一个配置类
@Configuration//这是配置类
public class Myconfig{
@Bean
public WebMvcConfigurer webMvcConfigurer(){
WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {
@Override
//Hobby类是自定义的参数类型,例如传过来值是以逗号分隔的一串数据
/*例如发送的表单递交
<input name="hobby.name">
<input name="hobby.count">
<!-- <input name="hobby" value="xx,xxx">-->
*/
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new Converter<String, Hobby>() {
@Override
public Hobby convert(String source) {
if (!StringUtils.isEmpty(source)) {
Hobby hobby = new Hobby();
String[] split = source.split(",");
//处理后的数组分别依次给对象赋值
hobby.setName(split[0]);
hobby.setCount(Integer.parseInt(split[1]));
return hobby;
}
return null;
}
});
}
};
return webMvcConfigurer;
}
}