In this recipe, you will learn how to get the submitted form data using controller method arguments. This is convenient for simple forms that are not related to a domain object.
Add an argument annotated with @RequestParam
to the controller method:
@RequestMapping("processForm")
public void processForm(@RequestParam("name") String userName) {
...
The userName
argument is initialized by Spring with the submitted value of the name
form field.
@RequestParam
can also retrieve URL parameters, for example, http://localhost:8080/springwebapp/processForm?name=Merlin
.
It's also possible to add the standard HttpServletRequest
object as an argument of the controller method and get the submitted value for name
directly from it:
@RequestMapping("processForm")
public void processForm(HttpServletRequest request) {
String name = request.getParameter("name");