David Maes
David Maes

Reputation: 573

Converters in Spring 3.0

Ok so I have build a converter and added it to the dispatcher 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. Each checkbox holds an id of a developer. Spring should make a set of developers from these id's. 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. 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 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;
    }


 }

Upvotes: 3

Views: 765

Answers (1)

nobeh
nobeh

Reputation: 10039

I suppose you have not configured the conversion service for Spring MVC in your XML configuration:

<mvc:annotation-driven conversion-service="conversionService" />

Upvotes: 1

Related Questions