6

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 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

D'oh! Thanks, that's simple enough!
Deprecated in Spring 3 and removed in Spring 4. Spring frameworks obviously didn't want to support two different approaches, so ditched the FormControllers in favour of annotation-driven configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.