views:

2286

answers:

1

How do I, without annotations, create and wire a controller that will perform an action based on a query parameter?

So maybe I've got a page with a list of items on it, and each one is a link like "edititem.htm?id=5". When the user clicks a link I want the controller to load up "item #5" and pass it to my edit form.

I'm sorry to ask such a silly question, but for some reason I can't find any example of doing this online.

+1  A: 

You should have a Controller that maps to edititem.htm. (Maybe a SimpleFormController)

Override one of the two showForm methods to populate the your model with the item:

protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors){
//get the id
int id = Integer.parseInt(request.getParameter("id"));

// get the object
Item item = dao.getItemById(id);
return  new ModelAndView(getFormView(), "item", item);
}

Also, see http://stackoverflow.com/questions/762104/different-views-with-springs-simpleformcontroller

Theo
D'oh! Thanks, that's simple enough!
Boden