Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 634
Boden Avatar asked Apr 24 '09 19:04

Boden


People also ask

How do I retrieve query parameters in a spring boot controller?

Simply put, we can use @RequestParam to extract query parameters, form parameters, and even files from the request.

What is difference between @RequestParam and @QueryParam?

@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.


1 Answers

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

like image 189
Theo Avatar answered Oct 19 '22 04:10

Theo