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> </label> <input type="submit" value="OK" class="btn"></li> </ul> </form>
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... }
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).
You can use @RequestParam
like this:
@RequestMapping(value="/forgotpassword", method=RequestMethod.POST) public String recoverPass(@RequestParam("j_username") String username) { //do smthin }
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.
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