[英]Converters in Spring 3.0
Ok so I have build a converter and added it to the dispatcher xml. 好的,我已经构建了一个转换器并将其添加到调度程序xml中。 But it won't work.
但它不会起作用。 I don't understand how the controller should know when it should use the converter.
我不明白控制器应该如何知道应该何时使用转换器。 In my jsp page I check multiply checkboxes.
在我的jsp页面中,我检查乘法复选框。 Each checkbox holds an id of a developer.
每个复选框都包含开发人员的ID。 Spring should make a set of developers from these id's.
Spring应该从这些id中创建一组开发人员。 I have the feeling I'm missing something in the controller.
我感觉我在控制器中遗漏了一些东西。 I used to do it with editors and then you would override the initbinder method.
我曾经用编辑器做过,然后你会覆盖initbinder方法。 I don't know how to do it with converters.
我不知道如何使用转换器。
Thank you in advance, David 大卫先生,谢谢你
so first I made a class implementing the interface: 所以首先我创建了一个实现接口的类:
public class DeveloperConverter implements Converter<String, Developer> {
private GameOrganizer gameOrganizer;
public void setGameOrganizer(GameOrganizer gameOrganizer) {
this.gameOrganizer = gameOrganizer;
}
public Developer convert(String s) {
long id2 = Long.parseLong(s);
Developer type = gameOrganizer.getDeveloper(id2);
return type;
}
}
then I added the bean to the dispatcher xml: 然后我将bean添加到调度程序xml:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="converters.GameConverter" />
<bean class="converters.DeveloperConverter" />
</list>
</property>
</bean>
And the controller: 和控制器:
@Controller
@RequestMapping("/AddGame")
public class GameFormController {
@Autowired
private GameOrganizer gameOrganizer;
private DeveloperConverter developerEditor;
private GameValidator gameValidator;
private ConversionService service;
public GameFormController() {
setGameValidator(new GameValidator());
}
public void setGameOrganizer(GameOrganizer gameOrganizer) {
this.gameOrganizer = gameOrganizer;
}
public void setDeveloperEditor(DeveloperConverter developerEditor) {
this.developerEditor = developerEditor;
developerEditor.setGameOrganizer(gameOrganizer);
}
public void setGameValidator(GameValidator gameValidator) {
this.gameValidator = gameValidator;
}
@RequestMapping(method = RequestMethod.GET)
private String showForm(ModelMap model) {
return "AddGame";
}
@ModelAttribute("editGame")
private Game GameformBackingObject(HttpServletRequest request) throws Exception {
Game game = null;
long id = ServletRequestUtils.getLongParameter(request, "id");
if (id <= 0) {
game = new Game();
} else {
game = new Game();
game.setId(gameOrganizer.getGame(id).getId());
game.setDevelopers(gameOrganizer.getGame(id).getDevelopers());
game.setGameNaam(gameOrganizer.getGame(id).getGameNaam());
game.setImages(gameOrganizer.getGame(id).getImages());
game.setPrijs(gameOrganizer.getGame(id).getPrijs());
}
return game;
}
@RequestMapping(method = RequestMethod.POST)
protected String doSubmitAction(@ModelAttribute("editGame") Game game, BindingResult result) throws Exception {
gameValidator.validate(game, result);
if (result.hasErrors()) {
return "AddGame";
} else {
if (game.getId() <= 0) {
gameOrganizer.addGame(game);
} else {
gameOrganizer.update(game);
}
return "forward:/Gamedatabase.htm";
}
}
@ModelAttribute("allDevelopers")
private Set<Developer> getDevelopers() throws Exception {
Set<Developer> developers = gameOrganizer.getAllDevelopers();
return developers;
}
@ModelAttribute("currentId")
private long getCurrentId(HttpServletRequest request) throws ServletRequestBindingException {
long id = ServletRequestUtils.getLongParameter(request, "id");
return id;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.