Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a form value in a controller

Tags:

I am using Spring MVC. How can I get text box value of the following snippet in my controller method?

<form name="forgotpassord" action="forgotpassword" method="POST" >     <ul>         <li><label>User:</label> <input type='text' name='j_username' /></li>         <li><label>&nbsp;</label> <input type="submit" value="OK" class="btn"></li>     </ul> </form> 
like image 344
Romi Avatar asked Sep 23 '11 05:09

Romi


People also ask

How do you value a controller?

You can get single value by @RequestParam and total form values by @ModelAttribute. @RequestMapping(value="/forgotpassword", method=RequestMethod. POST) public String getPassword(@RequestParam("j_username") String username) { //your code... }

How do you find the value in spring?

One of the most important annotations in spring is @Value annotation which is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. It also supports Spring Expression Language (SpEL).


2 Answers

You can use @RequestParam like this:

@RequestMapping(value="/forgotpassword", method=RequestMethod.POST) public String recoverPass(@RequestParam("j_username") String username) {     //do smthin } 
like image 65
Jaanus Avatar answered Oct 26 '22 18:10

Jaanus


You can get single value by @RequestParam and total form values by @ModelAttribute.

Here is code for single field-

 @RequestMapping(value="/forgotpassword", method=RequestMethod.POST)  public String getPassword(@RequestParam("j_username") String username) {         //your code...     } 

And if you have more values in form and want to get all as a single object- Use @ModelAttribute with spring form tag.

like image 33
Gaurav Avatar answered Oct 26 '22 18:10

Gaurav