Pass the request parameter in Spring MVC 3

I just want to send the value of my dropdown with the requestparameter parameter. In my case

Kidscalcula_web/start.htm?klasid=myValueHere 

I know a way to do this, but it sounds so irrational to use it for that. If I was bored, I would probably write jQuery to make a message and send a parameter to instance.Now really sounds like a really bad idea to manually make my requeststring, since Spring will take care of that. So, how can I make a simple form that just sends my dropdownvalue to my controller?

I just can’t find something so trivial anywhere, and one of you will probably help me quickly. I believe the controller will be as trivial as:

  @RequestMapping(value = "post") public String postIndex(@RequestParam("klasid") String klasid, HttpServletResponse response, HttpServletRequest request) { } 

But I really can't find examples of how to get the JSP to send me this value. Is this possible with <form> taglib?

+4
source share
2 answers

The tag <form> typically used with command-line objects with form support, and is not associated with controllers using individual @RequestParam arguments. That is why you will not see any examples of documentation using this combination.

For example, instead of @RequestParam("klasid") , you will have a command class with the klasid field, and Spring will bind the entire batch together:

 @RequestMapping(value = "post") public String postIndex(@ModelAttribute MyCommandClass command) { /../ } 

This makes sense when you consider that forms usually have several parameters, and it would be burdensome to declare them all with @RequestParam .

Having said that, you can still do it - any form controls will generate request parameters to which @RequestParam can bind, but if you decide to deviate from the Spring MVC form support command template, this is rather inconvenient.

+11
source

You don't even need taglib to send this request. You can create a simple HTML form with method = "GET" (which is the default value of method ):

 <form action = "..."> <select name = "klasid"> <option value = "value1">Option 1</option> <option value = "value2">Option 2</option> <option value = "value3">Option 3</option> </select> <input type = "submit" /> </form> 
+3
source

Source: https://habr.com/ru/post/905647/


All Articles