Search⌘ K

HTML Forms

Explore how to set up and manage HTML forms within a Spring MVC application. Learn to create controllers that display forms and process user input, mapping URLs to methods. Understand the use of JSP pages to render form views and display submitted data dynamically. Gain practical skills in linking pages and handling GET requests for form submission.

In this lesson, we will set up a form, which will prompt the user to enter a player’s name. When the user clicks the "Submit" button, our Spring MVC application will show a page containing the name entered by the user. Later, we will add functionality to show details of the player entered by the user.

A controller can have multiple request mappings. We will add methods to the TennisPlayerController to handle different request mappings. We will map the URL showPlayerForm to a method that displays the form and processPlayerForm to a method that directs the user to the player details page.

Controller method to show the form

The TennisPlayerController class is shown below. It is annotated with @Controller which tells Spring that this class will handle web requests.

@Controller
public class TennisPlayerController {
@RequestMapping(value = "/")
public String welcome() {
return "main-menu";
}
}
TennisPlayerController class

First, we need a method that will show the form to the user. We will create a method named showForm(). The /showPlayerForm URL maps to this method by virtue of the @RequestMapping annotation.

@RequestMapping("/showPlayerForm")
public String showForm () {
return "search-player-form";
}
Creating a method for /showPlayerForm URL

Spring will call the view resolver which will resolve search-player-form to /WEB-INF/views/search-player-form.jsp. ...