Spring Boot 2 + Spring Web + Thymeleaf + Spring Data JPA + H2 Database Example

#JavaInspires


Spring Boot 2 + Spring Web + Thymeleaf + Spring Data JPA + H2 Database Example



Project Structure



Springboot2ExamplesH2Application.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package com.javainspires.examples.springboot;

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

@SpringBootApplication
public class Springboot2ExamplesH2Application {

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

}



SignUpController.java
 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
26
27
28
29
30
31
32
33
34
35
36
package com.javainspires.examples.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.javainspires.examples.springboot.entity.User;
import com.javainspires.examples.springboot.model.SignupForm;
import com.javainspires.examples.springboot.repository.UserRepository;

@Controller
public class SignUpController {

 // autowire user repository
 @Autowired
 private UserRepository userRepo;

 // method to get signup page
 @GetMapping(path = "/signup")
 public String getSignupPage() {
  return "signup";
 }
 // method to submit signup

 @PostMapping(path="/signup")
 public String submitSignup(SignupForm signupForm) {
  //check whether the form data is coming or not\
  User  user = null;
  if(null != signupForm) {
   user = new User(signupForm.getFirstName(), signupForm.getLastName(), signupForm.getEmailId(), signupForm.getPassword());
  }
  userRepo.save(user);
  return "signup-success";
 }
}

User.java
 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.javainspires.examples.springboot.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Long id;
 private String firstName;
 private String lastName;
 private String emailId;
 private String password;

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

 public User(String firstName, String lastName, String emailId, String password) {
  super();
  this.firstName = firstName;
  this.lastName = lastName;
  this.emailId = emailId;
  this.password = password;
 }

 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 String getEmailId() {
  return emailId;
 }

 public void setEmailId(String emailId) {
  this.emailId = emailId;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

}

SignupForm.java
 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.javainspires.examples.springboot.model;

//model to class to hold signup form data
public class SignupForm {

 private String firstName;
 private String lastName;
 private String emailId;
 private String password;

 // no arg constructor
 public SignupForm() {
  super();
  // TODO Auto-generated constructor stub
 }

 public SignupForm(String firstName, String lastName, String emailId, String password) {
  super();
  this.firstName = firstName;
  this.lastName = lastName;
  this.emailId = emailId;
  this.password = password;
 }

 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 String getEmailId() {
  return emailId;
 }

 public void setEmailId(String emailId) {
  this.emailId = emailId;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

}

UserRepository.java
1
2
3
4
5
6
7
8
9
package com.javainspires.examples.springboot.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.javainspires.examples.springboot.entity.User;

public interface UserRepository extends JpaRepository<User, Long>{

}



signup.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
26
27
28
29
30
31
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Java Inspires</title>
</head>
<body>

 <h1>Sign Up here...</h1>
 <br>
 <hr>

 <form th:action="@{/signup}" th:object="${signupForm}" method="post">

  <p>
   First Name : <input type="text" name="firstName" />
  </p>
  <p>
   Last Name : <input type="text" name="lastName" />
  </p>
  <p>
   Email Id : <input type="email" name="emailId" />
  </p>
  <p>
   Password : <input type="password" name="password" />
  </p>
  <p>
   <input type="submit" value="Sign Up" />
 </form>
</body>
</html>

signup-success.html
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Java Inspires</title>
</head>
<body>
<center>
  <h1>Success ....!</h1>
 </center>
</body>
</html>

application.properties
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#add datasource details
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
#this is default username in h2
spring.datasource.username=sa
#default password is empty means no password
#added password
spring.datasource.password=password

#configure dialect
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.jpa.show-sql=true

#to eanble h2 console
spring.h2.console.enabled=true

pom.xml
 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?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.1.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.javainspires</groupId>
 <artifactId>springboot2-examples-h2</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>springboot2-examples-h2</name>
 <description>Demo project for Spring Boot + Web + H2</description>

 <properties>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <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>com.h2database</groupId>
   <artifactId>h2</artifactId>
   <scope>runtime</scope>
  </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>




Post a Comment

Previous Post Next Post