Spring Boot + Thymeleaf CRUD Exmaple - Java Inspires

#JavaInspires

Spring Boot + Thymeleaf CRUD Exmaple



 Project Folder Structure

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javainspires</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>11</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>


application.properties

spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=classpath:/pages/


index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Java Inspires</title>
</head>
<body>
	<br>
	<div align="center">
		<br> <a th:href="@{/employee}">Employees</a>
	</div>

</body>
</html>


employee-list.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Java Inspires</title>
<style type="text/css">
table {
	border-collapse: collapse;
}

td {
	text-align: center;
	padding: 2px;
}
</style>
</head>
<body>
	<div align="center">
		<br>
		<h3>Employee List</h3>
		<br />
		<table border="1">
			<tr>
				<td>Id</td>
				<td>First Name</td>
				<td>Last Name</td>
				<td>Age</td>
				<td>...</td>
			</tr>
			<tr th:each="emp : ${employees}">
				<td th:text="${emp.id}">id</td>
				<td th:text="${emp.firstName}">first name</td>
				<td th:text="${emp.lastName}">last name</td>
				<td th:text="${emp.age}">age</td>
				<td><a th:href="@{/employee/edit/{id}(id=${emp.id})}">Edit</a>
					<a th:href="@{/employee/delete/{id}(id=${emp.id})}">Delete</a></td>
			</tr>
		</table>
		<br> <a th:href="@{/employee/add}">Add Employee</a> <br>
	</div>
</body>
</html>


employee-form.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Java Inspires</title>
</head>
<body>
	<div align="center">
		<br>
		<h3>Add Employee</h3>
		<br>
		<form th:action="@{/employee/add}" th:object="${addEmployee}"
			method="post">
			<table>
				<tr>
					<td>Emp Id</td>
					<td><input type="text" name="id" /></td>
				</tr>
				<tr>
					<td>First Name</td>
					<td><input type="text" name="firstName" /></td>
				</tr>
				<tr>
					<td>Last Name</td>
					<td><input type="text" name="lastName" /></td>
				</tr>
				<tr>
					<td>Age</td>
					<td><input type="number" name="age" /></td>
				</tr>
			</table>
			<input type="submit" value="Submit" />
		</form>
	</div>
</body>
</html>



employee-edit.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Java Inspires</title>
</head>
<body>
	<div align="center">
		<br>
		<h3>Edit Employee</h3>
		<br>
		<form th:action="@{/employee/edit/{id}(id=*{id})}"
			th:object="${editEmployee}" method="post">
			<table>
				<tr>
					<td>Emp Id</td>
					<td><input type="text" disabled="disabled" name="id"
						th:field="*{id}" /></td>
				</tr>
				<tr>
					<td>First Name</td>
					<td><input type="text" name="firstName"
						th:field="*{firstName}" /></td>
				</tr>
				<tr>
					<td>Last Name</td>
					<td><input type="text" name="lastName" th:field="*{lastName}" /></td>
				</tr>
				<tr>
					<td>Age</td>
					<td><input type="number" name="age" th:field="*{age}" /></td>
				</tr>
			</table>
			<input type="submit" value="Submit" />
		</form>
	</div>
</body>
</html>


EmployeeController.java

package com.javainspires.demo;

import java.util.HashMap;
import java.util.Map;

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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * 
 * @author #JavaInspires
 *
 */
@Controller
public class EmployeeController {

	public Map<String, Employee> empMap = new HashMap<>();

	public EmployeeController() {
		empMap.put("E101", new Employee("E101", "Brett", "Lee", 23));
		empMap.put("E102", new Employee("E102", "Chris", "Gayle", 23));
		empMap.put("E103", new Employee("E103", "Brian", "Lara", 23));
		empMap.put("E104", new Employee("E104", "MS", "Dhoni", 23));

	}

	// to get all employees
	@GetMapping("/employee")
	public String getAllEmployees(Model model) {

		model.addAttribute("employees", empMap.values());

		return "employee-list";
	}

	// to get employee form
	@GetMapping("/employee/add")
	public String addEmployee() {

		return "employee-form";
	}

	// to add new employee
	@PostMapping("/employee/add")
	public String addEmployee(@ModelAttribute("addEmployee") Employee employee, Model model) {
		empMap.put(employee.getId(), employee);
		model.addAttribute("employees", empMap.values());
		return "employee-list";
	}

	// get employee to edit
	@GetMapping("/employee/edit/{id}")
	public String getAllEmployees(Model model, @PathVariable("id") String empId) {

		model.addAttribute("editEmployee", empMap.get(empId));

		return "employee-edit";
	}

	// to update edited employee
	@PostMapping("/employee/edit/{id}")
	public String updateEmployee(@ModelAttribute("editEmployee") Employee employee, @PathVariable("id") String empId,
			Model model) {

		empMap.put(empId, employee);

		model.addAttribute("employees", empMap.values());
		return "employee-list";
	}

	// to delete particular employee
	@GetMapping("/employee/delete/{id}")
	public String deleteEmployee(Model model, @PathVariable("id") String empId) {
		empMap.remove(empId);
		model.addAttribute("employees", empMap.values());
		return "employee-list";
	}
}


Employee.java

package com.javainspires.demo;
/**
 * 
 * @author #JavaInspires
 *
 */
public class Employee {

	private String id;
	private String firstName;
	private String lastName;
	private int age;

	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Employee(String id, String firstName, String lastName, int age) {
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.age = age;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}



DemoApplication.java
 
package com.javainspires.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * 
 * @author #JavaInspires
 *
 */
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}


Screenshots:









---------------------------------------------------------------------------------
Facebook https://www.facebook.com/javainspires/
Twitter https://twitter.com/javainspires
Pinterest https://in.pinterest.com/javainspires/boards
Linked In https://www.linkedin.com/in/java-inspires-b782621a7
Reddit https://www.reddit.com/user/JavaInspires_
Tumblr https://javainspires.tumblr.com/
Blogger https://javainspires.blogspot.com/
---------------------------------------------------------------------------------




Post a Comment

Previous Post Next Post