Skip to content

Commit 42631ca

Browse files
author
Ramesh Fadatare
committed
adding spring boot freemarker tutorial
1 parent 0e55c79 commit 42631ca

File tree

11 files changed

+308
-0
lines changed

11 files changed

+308
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
46.5 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>net.guides.springboot2</groupId>
8+
<artifactId>springboot2-freemarker-example</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>springboot2-freemarker-example</name>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.5.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-freemarker</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>com.h2database</groupId>
44+
<artifactId>h2</artifactId>
45+
<scope>runtime</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-maven-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
59+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package net.guides.springboot2.freemarker;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.CommandLineRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
10+
import net.guides.springboot2.freemarker.model.Employee;
11+
import net.guides.springboot2.freemarker.repository.EmployeeRepository;
12+
13+
@SpringBootApplication
14+
public class Application implements CommandLineRunner {
15+
16+
@Autowired
17+
private EmployeeRepository employeeRepository;
18+
19+
public static void main(String[] args) {
20+
SpringApplication.run(Application.class, args);
21+
}
22+
23+
@Override
24+
public void run(String... args) throws Exception {
25+
26+
employeeRepository.save(new Employee("Ramesh", "Fadatare", "[email protected]"));
27+
employeeRepository.save(new Employee("Tom", "Cruise", "[email protected]"));
28+
employeeRepository.save(new Employee("John", "Cena", "[email protected]"));
29+
employeeRepository.save(new Employee("tony", "stark", "[email protected]"));
30+
// get list of employees
31+
List<Employee> employees = employeeRepository.findAll();
32+
employees.forEach(employee -> System.out.println(employee.toString()));
33+
}
34+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package net.guides.springboot2.freemarker.controller;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.ui.Model;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.servlet.ModelAndView;
12+
13+
import net.guides.springboot2.freemarker.model.Employee;
14+
import net.guides.springboot2.freemarker.repository.EmployeeRepository;
15+
16+
@Controller
17+
public class EmployeeController {
18+
@Autowired
19+
private EmployeeRepository employeeRepository;
20+
21+
@GetMapping("/")
22+
public String index(Model model) {
23+
24+
return "index";
25+
}
26+
27+
@GetMapping("/test")
28+
public String test(Model model) {
29+
30+
return "index";
31+
}
32+
33+
@GetMapping("/showEmployees")
34+
public ModelAndView showCities() {
35+
36+
List<Employee> employees = employeeRepository.findAll();
37+
38+
Map<String, Object> params = new HashMap<String, Object>();
39+
params.put("employees", employees);
40+
41+
return new ModelAndView("showEmployees", params);
42+
}
43+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package net.guides.springboot2.freemarker.model;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
import javax.persistence.Table;
9+
10+
@Entity
11+
@Table(name = "employees")
12+
public class Employee {
13+
14+
private long id;
15+
private String firstName;
16+
private String lastName;
17+
private String emailId;
18+
19+
public Employee() {
20+
21+
}
22+
23+
public Employee(String firstName, String lastName, String emailId) {
24+
this.firstName = firstName;
25+
this.lastName = lastName;
26+
this.emailId = emailId;
27+
}
28+
29+
@Id
30+
@GeneratedValue(strategy = GenerationType.AUTO)
31+
public long getId() {
32+
return id;
33+
}
34+
public void setId(long id) {
35+
this.id = id;
36+
}
37+
38+
@Column(name = "first_name", nullable = false)
39+
public String getFirstName() {
40+
return firstName;
41+
}
42+
public void setFirstName(String firstName) {
43+
this.firstName = firstName;
44+
}
45+
46+
@Column(name = "last_name", nullable = false)
47+
public String getLastName() {
48+
return lastName;
49+
}
50+
public void setLastName(String lastName) {
51+
this.lastName = lastName;
52+
}
53+
54+
@Column(name = "email_address", nullable = false)
55+
public String getEmailId() {
56+
return emailId;
57+
}
58+
public void setEmailId(String emailId) {
59+
this.emailId = emailId;
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", emailId=" + emailId
65+
+ "]";
66+
}
67+
68+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package net.guides.springboot2.freemarker.repository;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
import net.guides.springboot2.freemarker.model.Employee;
7+
8+
@Repository
9+
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
10+
11+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.freemarker.template-loader-path: classpath:/templates
2+
spring.freemarker.suffix: .ftl
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Home page</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
8+
rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
9+
crossorigin="anonymous">
10+
</head>
11+
12+
<body>
13+
<div class="container">
14+
<div class="panel panel-primary">
15+
<div class="panel-heading">
16+
<h2>Home Page</h2>
17+
</div>
18+
<a href="showEmployees">Show Employees (Retrieve employee data from database)</a>
19+
</div>
20+
</div>
21+
</body>
22+
23+
</html>

0 commit comments

Comments
 (0)