Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a request parameter in Spring MVC 3

I just want to send the value of my dropdownlist with a requestparameter. In my case being

Kidscalcula_web/start.htm?klasid=myValueHere

I know a way of doing this but it sounds so irrational to use it for this. If I was bored I'd probably write some jQuery to do a post and send the parameter for instance.Now it really sounds like a very bad idea to manually make my requeststring, since Spring takes care of that. So how could I make a simple form that just sends my dropdownvalue to my controller?

It's just that I can't find something so trivial anywhere, and one of you can probably help me out quickly.I suppose the controller would be just as trivial as:

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

But I really can't find any examples on how to make a JSP to send me that value. Is this possible with the <form>taglib ?

like image 789
toomuchcs Avatar asked Nov 09 '10 22:11

toomuchcs


2 Answers

The <form> taglib is generally used with form-backing command objects, rather than being bound to the controllers using individual @RequestParam arguments. This is why you won't see any documentation examples of that combination being used together.

For example, rather than having @RequestParam("klasid"), you'd have a command class with a field called klasid, and Spring would bind the whole lot together:

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

This makes sense when you consider that forms typically have multiple parameters, and it'd get cumbersome to declare them all using @RequestParam.

Having said that, you can still do it - any form controls will generate request parameters that @RequestParam can bind to, but if you choose to deviate from Spring MVC's form-backing command pattern, then it's quite awkward.

like image 133
skaffman Avatar answered Oct 06 '22 11:10

skaffman


You don't even need a taglib to send this request. You can create a simpliest HTML form with method = "GET" (what 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>
like image 37
axtavt Avatar answered Oct 06 '22 11:10

axtavt