Boden
Boden

Reputation: 4167

Spring MVC controller HTTP GET query parameters

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.

Upvotes: 6

Views: 21170

Answers (1)

Theo
Theo

Reputation: 396

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 Different views with Spring's SimpleFormController

Upvotes: 6

Related Questions