How to Use JSTL <form:form> and <form:input> tag in Spring MVC

Here We Use :

JDK : Version: 1.8

IDE : Eclipse IDE for Enterprise Java Developers, Version: 2020-06

Build Tool : Maven,Version: 3.2.0

Spring : Version: 5.2.8

Server : apache-tomcat-9.0.34

POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.corejava</groupId>
<artifactId>spring-mvc-java-config-model</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-mvc-java-config-model Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
  <springframework.version>5.2.8.RELEASE</springframework.version>
  <maven.compiler.target>1.8</maven.compiler.target>
  <maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version></version>
  </dependency>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
   </dependency>
  </dependencies>
<build>
<finalName>spring-mvc-java-config-model</finalName>
<plugins>
 <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-war-plugin</artifactId>
        <version>3.2.0</version>          
    </plugin>           
</plugins>
</build>
</project>

Employee.java

package com.corejava;
import java.io.Serializable;
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
		
private int id;
private String fname;
private String lname;
private String email;

public Employee() {}

public String getFname() {
	return fname;
}

public void setFname(String fname) {
	this.fname = fname;
}

public String getLname() {
	return lname;
}

public void setLname(String lname) {
	this.lname = lname;
}

public String getEmail() {
	return email;
}

public void setEmail(String email) {
	this.email = email;
}

@Override
public String toString() {
	return "Employee [fname=" + fname + ", lname=" + lname + ", email=" + email + "]";
}

SpringmvcConfig.java

package com.corejava;
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.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.corejava")
public class SpringmvcConfig {

@Bean
ViewResolver getViewResolver(){
InternalResourceViewResolver resolver=new InternalResourceViewResolver("/WEB-INF/view/", ".jsp");
return resolver;
}
}

SpringMvcDispatcherServletInitializer.java

package com.corejava;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected Class[] getRootConfigClasses() {
		return null;
	}

	@Override
	protected Class[] getServletConfigClasses() {
		return new Class[]{SpringmvcConfig.class};
	}

	@Override
	protected String[] getServletMappings() {
		return new String[]{"/"};
	}

}

Controller.java

package com.corejava;
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 org.springframework.web.bind.annotation.RequestMapping;

@org.springframework.stereotype.Controller
@RequestMapping("home")
public class Controller {

	@GetMapping("/welcome")
	public String getNavbar() {
		return "navbar";
	}
	
	@GetMapping("/reg")
	public String getReg(Model model) {
		Employee employee=new Employee();
		model.addAttribute("employee", employee);
		return "reg";
	}
	
	@PostMapping("/submitreg2")
	public String submitreg2(@ModelAttribute("employee") Employee employee,Model model) {
		System.out.println(employee);
		model.addAttribute("message","Data Save Successfully");
		return "reg-view";
	}
}

navbar.jsp

<html>
<head>
<title>NAVBAR</title>
</head>
<body>
<a href="reg">JSTL Form Tag</a>
</body>
</html>

reg.jsp

 <%@ taglib uri="http://www.springframework.org/tags/form"  prefix="form"%>
<%@ include file="navbar.jsp" %>
<hr>
<form:form action="submitreg2" modelAttribute="employee" method="post">
<table>
	<tr>
		<td>FName</td>
		<td><form:input  path="fname"/></td>
	</tr>
	<tr>
		<td>LName</td>
		<td><form:input  path="lname"/></td>
	</tr>
	<tr>
		<td>Email</td>
		<td><form:input  path="email"/></td>
	</tr>
	<tr>
		<td colspan="1"><input type="submit" value="SUBMIT"></td>
	</tr>
</table>
</form:form>

reg-view.jsp

<html>
<head>
<title>Reg</title>
</head>
<%@ include file="navbar.jsp" %>
<hr>
<body>
<p style="color:green"></p>
<table border="1">
	<tr>
		<td>FName</td>
		<td>${employee.fname}</td>
	</tr>
	<tr>
		<td>LName</td>
		<td>${employee.fname}</td>
	</tr>
	<tr>
		<td>Email</td>
		<td>${employee.fname}</td>
	</tr>
</table>
</body>
</html>