Spring Boot + Thymeleaf Data Table | Java Inspires

#JavaInspires



Spring Boot + Thymeleaf Datatable Example



Project Folder Structure















ThymeleafdatableApplication.java

package com.javainspires.thymeleafdatatable;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ThymeleafdatableApplication {

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



EmployeeController.java

package com.javainspires.thymeleafdatatable.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.javainspires.thymeleafdatatable.dao.EmployeeDao;
import com.javainspires.thymeleafdatatable.model.Employee;

@Controller
public class EmployeeController {

 @Autowired
 private EmployeeDao employeeDao;

 @RequestMapping(value = "/employeeList", method = RequestMethod.GET)
 public String getEmployeeList(Model model) {

  List<Employee> empList = employeeDao.findAll();
  model.addAttribute("empList", empList);
  return "employeelist";
 }
}

EmployeeDao.java

package com.javainspires.thymeleafdatatable.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.javainspires.thymeleafdatatable.model.Employee;

@Repository
public class EmployeeDao {

 public List<Employee> findAll(){
  List<Employee> empList = new ArrayList<>();
  empList.add(new Employee("A1", "AAA", "22"));
  empList.add(new Employee("A2", "ABC", "23"));
  empList.add(new Employee("A3", "ACB", "24"));
  empList.add(new Employee("A4", "BCA", "25"));
  empList.add(new Employee("A5", "BAC", "26"));
  empList.add(new Employee("A6", "CAB", "27"));
  empList.add(new Employee("A7", "CBA", "28"));
  return empList;
 }
}  

Employee.java

package com.javainspires.thymeleafdatatable.model;

public class Employee {

 private String id;
 private String name;
 private String age;

 /**
  * 
  */
 public Employee() {
  super();
 }

 /**
  * @param id
  * @param name
  * @param age
  */
 public Employee(String id, String name, String age) {
  super();
  this.id = id;
  this.name = name;
  this.age = age;
 }

 /**
  * @return the id
  */
 public String getId() {
  return id;
 }

 /**
  * @param id
  *            the id to set
  */
 public void setId(String id) {
  this.id = id;
 }

 /**
  * @return the name
  */
 public String getName() {
  return name;
 }

 /**
  * @param name
  *            the name to set
  */
 public void setName(String name) {
  this.name = name;
 }

 /**
  * @return the age
  */
 public String getAge() {
  return age;
 }

 /**
  * @param age
  *            the age to set
  */
 public void setAge(String age) {
  this.age = age;
 }

}



index.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Index</title>
</head>
<body>
 <h1>Welcome</h1>
 <br />
 <p>
  Click here to get <a th:href="@{/employeeList}">Employee List</a>
 </p>
</body>
</html>


employeelist.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Employee List</title>
</head>
<body>
 <h1>Welcome</h1>
 <br>
 <h3>Employee List</h3>
 <br />
 <table border="1">
  <tr>
   <td>Employee Id</td>
   <td>Employee Name</td>
   <td>Employee Age</td>
  </tr>
  <tr th:each="emp : ${empList}">
   <td th:text="${emp?.id}">id</td>
   <td th:text="${emp?.name}">name</td>
   <td th:text="${emp?.age}">age</td>
  </tr>
 </table>
</body>
</html>




Post a Comment

Previous Post Next Post