tags | projects |
---|---|
This guide walks you through the process of using Spring to create and submit a web form.
In this guide, you will build a web form which will be accessible at the following URL:
http://localhost:8080/greeting
Viewing this page in a browser will display the form. You can submit a greeting by populating the id
and content
form fields. A results page will be displayed when the form is submitted.
In Spring’s approach to building web sites, HTTP requests are handled by a controller. These components are easily identified by the @Controller
annotation. The GreetingController below handles GET requests for /greeting by returning the name of a View
, in this case, "greeting". A View
is responsible for rendering the HTML content:
src/main/java/hello/GreetingController.java
link:complete/src/main/java/hello/GreetingController.java[role=include]
This controller is concise and simple, but a lot is going on. Let’s analyze it step by step.
The @RequestMapping
annotation allows you to map HTTP requests to specific controller methods. The two methods in this controller are both mapped to /greeting
. By default @RequestMapping
maps all HTTP operations, such as GET
, POST
, and so forth. But in this case the greetingForm()
method is specifically mapped to GET
using @RequestMapping(method=GET)
, while greetingSubmit()
is mapped to POST
with @RequestMapping(method=POST)
. This mapping allows the controller to differentiate the requests to the /greeting
endpoint.
The greetingForm()
method uses a Model
object to expose a new Greeting
to the view template. The Greeting
object in the following code contains fields such as id
and content
that correspond to the form fields in the greeting
view, and will be used to capture the information from the form.
src/main/java/hello/Greeting.java
link:complete/src/main/java/hello/Greeting.java[role=include]
The implementation of the method body relies on a view technology, in this case Thymeleaf, to perform server-side rendering of the HTML. Thymeleaf parses the greeting.html
template below and evaluates the various template expressions to render the form.
src/main/resources/templates/greeting.html
link:complete/src/main/resources/templates/greeting.html[role=include]
The th:action="@{/greeting}"
expression directs the form to POST to the /greeting
endpoint, while the th:object="${greeting}"
expression declares the model object to use for collecting the form data. The two form fields, expressed with th:field="{id}"
and th:field="
{content}"
, correspond to the fields in the Greeting
object above.
That covers the controller, model, and view for presenting the form. Now let’s review the process of submitting the form. As noted above, the form submits to the /greeting
endpoint using a POST
. The greetingSubmit()
method receives the Greeting
object that was populated by the form. It then adds that populated object to the model so the submitted data can be rendered in the result
view, seen below. The id
is rendered in the <p th:text="'id: ' + ${greeting.id}" />
expression. Likewise the content
is rendered in the <p th:text="'content: ' + ${greeting.content}" />
expression.
src/main/resources/templates/result.html
link:complete/src/main/resources/templates/result.html[role=include]
For clarity, this example utilizes two separate view templates for rendering the form and displaying the submitted data; however, you can also use a single view for both purposes.
Although it is possible to package this service as a traditional WAR file for deployment to an external application server, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java main()
method. Along the way, you use Spring’s support for embedding the Tomcat servlet container as the HTTP runtime, instead of deploying to an external instance.
src/main/java/hello/Application.java
link:complete/src/main/java/hello/Application.java[role=include]
@SpringBootApplication
is a convenience annotation that adds all of the following:
-
@Configuration
tags the class as a source of bean definitions for the application context. -
@EnableAutoConfiguration
tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. -
Normally you would add
@EnableWebMvc
for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up aDispatcherServlet
. -
@ComponentScan
tells Spring to look for other components, configurations, and services in the thehello
package, allowing it to find theHelloController
.
The main()
method uses Spring Boot’s SpringApplication.run()
method to launch an application. Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.
Logging output is displayed. The service should be up and running within a few seconds.
Now that the web site is running, visit http://localhost:8080/greeting, where you see the following form:
Submit an id and message to see the results: