Spring MVC Form Tags
Spring provides a form tag library to implement view pages using the JSP. It provides tags for creating HTML components, error messages, themes, and internationalized messages. It is useful when you are working with Spring MVC.
It is a built-in library so we can use it on the JSP page by using this tag:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
After adding this tag, we can create an HTML form by using the form prefix. For example, to create an input text, we can use it like:
<form:input type="text" path="name" />
It is equivalent to the below HTML code.
<input id="name" name="name" type="text" value=""/>
List of Spring MVC Form Tags
Spring provides the following tags that can be used to create a form in the application.
Form Tag |
Description |
Example |
form:form |
It is a form tag and used to create an HTML form. |
<form:form action="save" method="post" modelAttribute="user">
|
form:input |
It is an input tag that is used to create a text field. |
<form:input type="text" path="name" />
|
form:radiobutton |
It is a radio tag that is used to create a radio button. |
<form:radiobutton path="choice" />
|
form:checkbox |
It is a checkbox tag that is used to create a checkbox. |
<form:checkbox path="india" value="91"/>
|
form:password |
It is an input tag that is used to create a password input field. |
<form:input type="password" path="password" />
|
form:select |
It is a select tag that is used to create a drop-down list. |
<form:select path="country_names" />
|
form:textarea |
This tag is used to generate the multi-line text field. |
<form:textarea path="name" col="5" row="2" />
|
form:hidden |
It is an input tag that is used to create a hidden input field. |
<form:hidden path="name" />
|
form:option |
It is an option tag that is used to create a single HTML option. |
<form:select path="name" value="1">
<form:option value="1">1</form:option>
<form:option value="2">2</form:option>
</form:select>
|
form:label |
It is an label tag that is used to create a form field label tag. |
<form:label path="contact">Contact</form:label>
|
Time for an Example:
Let's create a maven based Spring example to generate form using Spring form tag library. Here we have given some essential source files that our project has. You can refer them to get idea of Spring form tag library. The given class file is our configuration file that configure MVC application and it's views.
package com.studytonight;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan("com.studytonight")
public class AppConfig implements WebMvcConfigurer{
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver irvr = new InternalResourceViewResolver();
irvr.setPrefix("WEB-INF/views/");
irvr.setSuffix(".jsp");
irvr.setOrder(0);
return irvr;
}
}
// MainApp.java
It is our ServlertDispatcher class that create application context and register our configuration class.
package com.studytonight;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class MainApp implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(AppConfig.class);
context.setServletContext(servletContext);
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
context.close();
}
}
// UserController.java
This is a controller class that contains two methods index() and save(), the index() is used to render JSP page containing user form created by using Tag library and the second method returns another JSP page that shows the data entered by the user.
package com.studytonight.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import com.studytonight.model.User;
@Controller
public class UserController {
@GetMapping("/")
public String index(Model model) {
model.addAttribute("user", new User());
return "index";
}
@PostMapping("save")
public String save(@ModelAttribute("user") User user, Model model) {
model.addAttribute("user", user);
return "response";
}
}
// User.java
It is a bean class to hold the user data in the application.
package com.studytonight.model;
public class User {
private int id;
private String name;
private String email;
private String password;
private String confirm_password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConfirm_password() {
return confirm_password;
}
public void setConfirm_password(String confirm_password) {
this.confirm_password = confirm_password;
}
}
View Files
Here, you can see that we used Spring tag library in the JSP page to create a user form. We used several tags like text box, email, password box, etc.
// index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Form</title>
</head>
<body>
<form:form action="save" method="post" modelAttribute="user">
User Name: <form:input type="text" path="name" />
<br>
<br>
Email: <form:input type="email" path="email" style="margin-left:34px;" />
<br>
<br>
Password: <form:input type="password" path="password" style="margin-left:10px;" />
<br>
<br>
Confirm Password: <form:input type="password" path="confirm_password" />
<br>
<br>
<input type="submit" value="Register">
</form:form>
</body>
</html>
// response.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Response User Data</title>
</head>
<body>
<h2>${user.name}</h2>
<p>Email: ${user.email}</p>
<p>Password: ${user.password}</p>
</body>
</html>
Run the Application
After successfully completing the project and adding the dependencies run the application using the server and you will get the output as below.
User Form created using Form Tag
Enter User Information
Retrieve Data
// response.jsp file