@Entity
public class SeedStarter {
...
private List<SeedStarterFeature> features;
...
在支持表单的bean中,我希望功能将通过用户选择的ID自动映射
在模板中
<!-- seed starter type -->
<div>
<label>Type</label>
<select th:field="*{seedStarterType}">
<option th:each="ftype: ${allTypes}" th:text="${ftype.name}" th:value="${ftype.typeId}"></option>
</select>
</div>
也是支票
<h3 th:text="${#conversions.convert('5', 'io.dreamitpossible.seedstartermanager.model.SeedStarterFeature')}"></h3>
和转换服务
@Component
public class SeedStarterFeatureFormatter implements Formatter<SeedStarterFeature> {
@Autowired
private SeedStarterFeatureService seedStarterFeatureService;
@Override
public SeedStarterFeature parse(String text, Locale locale) throws ParseException {
SeedStarterFeature match = null;
try{
Integer featureId = Integer.valueOf(text);
match = seedStarterFeatureService.selectAllById(featureId);
} catch (NumberFormatException exp){
}
return match;
}
@Override
public String print(SeedStarterFeature object, Locale locale) {
return object.toString();
}
}
@EnableWebMvc
@Configuration
@EnableTransactionManagement
@ComponentScan("io.dreamitpossible.seedstartermanager")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
...
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatter(seedStarterFeatureFormatter());
}
@Bean
public SeedStarterFeatureFormatter seedStarterFeatureFormatter(){
return new SeedStarterFeatureFormatter();
}
...
}
我也尝试了XML配置,但是没有用 正如我对POST方法的功能ID所看到的那样,它通过其构造函数而不使用转换器或格式化程序来构建SeedStarterFeature ...
作为示例: ID 5和6 =))
features = [SeedStarterFeature {featureId = 0,name ='5'},SeedStarterFeature {featureId = 0,name ='6'}
我怎么了?
我使用spring4