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.
Simply put, we can use @RequestParam to extract query parameters, form parameters, and even files from the request.
@QueryParam is a JAX-RS framework annotation and @RequestParam is from Spring. QueryParam is from another framework and you are mentioning Spring. @Flao wrote that @RequestParam is from Spring and that should be used in Spring MVC.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With