Create Spring MVC Project
On the Eclipse, create a Spring MVC project in Spring Boot
Enter Project Information:
- Name: LearnSpringMVCWithRealApps
- Group: com.demo
- Artifact: LearnSpringMVCWithRealApps
- Description: Learn Spring MVC with Real Apps
- Package: com.demo
Select the technologies and libraries to be used:
- Web
Click Next button to show Site Information for project
Click Finish button to finish create Spring MVC project
Configure pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>LearnSpringMVCWithRealApps</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>LearnSpringMVCWithRealApps</name>
<description>Learn Spring MVC with Real Apps</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JSTL tag lib -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- Tomcat for JSP rendering -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Configure application.properties
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp
spring.mvc.static-path-pattern=/resources/**
server.port=9596
Entities Class
Create new package, named com.demo.entities. In this package, create entities class as below:
Role Entity
Create new java class, named Role.java
package com.demo.entities;
public class Role {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Role() {
}
public Role(String id, String name) {
this.id = id;
this.name = name;
}
}
Account Entity
Create new java class, named Account.java
package com.demo.entities;
public class Account {
private String username;
private String fullName;
private Role role;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
Create Conversion
Create new package named com.demo.conversion. In this package, create new conversion named RoleEditor as below:
package com.demo.conversion;
import java.beans.PropertyEditorSupport;
import com.demo.entities.Role;
import com.demo.models.RoleModel;
public class RoleEditor extends PropertyEditorSupport {
@Override
public void setAsText(String id) throws IllegalArgumentException {
try {
RoleModel roleModel = new RoleModel();
this.setValue(roleModel.find(id));
} catch (Exception e) {
this.setValue(null);
}
}
@Override
public String getAsText() {
Role role = (Role) getValue();
return role.getId();
}
}
Models Class
Create new package, named com.demo.models. In this package, create RoleModel class as below:
package com.demo.models;
import java.util.ArrayList;
import java.util.List;
import com.demo.entities.Role;
public class RoleModel {
private List<Role> roles;
public RoleModel() {
this.roles = new ArrayList<Role>();
this.roles.add(new Role("r1", "Role 1"));
this.roles.add(new Role("r2", "Role 2"));
this.roles.add(new Role("r3", "Role 3"));
this.roles.add(new Role("r4", "Role 4"));
}
public List<Role> findAll() {
return this.roles;
}
public Role find(String id) {
for (Role role : this.roles) {
if (role.getId().equalsIgnoreCase(id)) {
return role;
}
}
return null;
}
}
Create Controller
Create new package named com.demo.controllers. In this package, create new controller named AccountController as below:
package com.demo.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.demo.conversion.RoleEditor;
import com.demo.entities.Account;
import com.demo.entities.Role;
import com.demo.models.RoleModel;
@Controller
@RequestMapping(value = "account")
public class AccountController {
@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
webDataBinder.registerCustomEditor(Role.class, new RoleEditor());
}
@RequestMapping(method = RequestMethod.GET)
public String index(ModelMap modelMap) {
RoleModel roleModel = new RoleModel();
Account account = new Account();
account.setRole(roleModel.find("r3"));
modelMap.put("account", account);
modelMap.put("roles", roleModel.findAll());
return "account/index";
}
@RequestMapping(value = "save", method = RequestMethod.POST)
public String save(@ModelAttribute("account") Account account, ModelMap modelMap) {
modelMap.put("account", account);
return "account/success";
}
}
Create Views
Create new folders with path webapp\WEB-INF\views in src\main. In views folder, create new folder named account. In account folder, create new views as below:
Index View
Create new jsp file named index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>Account Register</h3>
<s:form method="post" commandName="account"
action="${pageContext.request.contextPath }/account/save">
<table cellpadding="2" cellspacing="2" border="0">
<tr>
<td>Username</td>
<td><s:input path="username" /></td>
</tr>
<tr>
<td>Full Name</td>
<td><s:input path="fullName" /></td>
</tr>
<tr>
<td>Role</td>
<td>
<s:select path="role" items="${roles }"
itemValue="id" itemLabel="name"></s:select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save" /></td>
</tr>
</table>
</s:form>
</body>
</html>
Success View
Create new jsp file named success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Account Info</title>
</head>
<body>
<h3>Account Info</h3>
<table cellpadding="2" cellspacing="2" border="1">
<tr>
<td>Username</td>
<td>${account.username }</td>
</tr>
<tr>
<td>Full Name</td>
<td>${account.fullName }</td>
</tr>
<tr>
<td>Role Id</td>
<td>${account.role.id }</td>
</tr>
<tr>
<td>Role Name</td>
<td>${account.role.name }</td>
</tr>
</table>
</body>
</html>
Structure of Spring MVC Project
Run Application
Select LearnSpringMVCWithRealAppsApplication.java file in com.demo package, right click and select Run As/Spring Boot App menu
Access index method in account controller with following url: http://localhost:9596/account
Output
Click Save button submit form to save method in account controller with following url: http://localhost:9596/account/save
Output
References
I recommend you refer to the books below to learn more about the knowledge in this article:
- Spring MVC Beginners Guide – Second Edition
- Spring MVC Cookbook
- Spring MVC Blueprints
- Mastering Spring MVC 4
- Spring Boot in Action
- Pro Spring Boot