Skip to content

Commit f5263af

Browse files
committed
Spring Boot JPA Hibernate Multiple data source Maven MVC
1 parent 2408cb4 commit f5263af

File tree

24 files changed

+292
-48
lines changed

24 files changed

+292
-48
lines changed

Part-4 Spring Boot REST API/SpringMysqlhiber/src/main/java/spring/mysqlhiber/controllers/ChildController.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

Part-4 Spring Boot REST API/SpringMysqlhiber/src/main/java/spring/mysqlhiber/domain/Child.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

Part-4 Spring Boot REST API/SpringMysqlhiber/src/main/java/spring/mysqlhiber/repository/ChildRepository.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

Part-4 Spring Boot REST API/SpringMysqlhiber/src/main/resources/application.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

Part-4 Spring Boot REST API/SpringMysqlhiber/src/main/webapp/WEB-INF/jsp/user.jsp

Whitespace-only changes.

Part-5 Spring Boot Web Application/SpringJSPCalculate/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
</exclusion>
3636
</exclusions>
3737
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
42+
</dependency>
43+
3844
</dependencies>
3945

4046
<build>
Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
package spring.jsp;
22

3-
import javax.servlet.ServletException;
4-
import javax.servlet.annotation.WebServlet;
5-
import javax.servlet.http.HttpServlet;
6-
import javax.servlet.http.HttpServletRequest;
7-
import javax.servlet.http.HttpServletResponse;
8-
import java.io.IOException;
9-
import java.io.PrintWriter;
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.validation.BindingResult;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
9+
10+
11+
import javax.validation.Valid;
12+
1013

1114
/**
1215
* @Created 27 / 03 / 2020 - 5:56 PM
1316
* @project SpringJSP
1417
* @Author Hamdamboy
1518
*/
16-
@WebServlet("/add")
17-
public class AddServlet extends HttpServlet {
19+
@Controller
20+
public class AddServlet implements WebMvcConfigurer {
1821
//
19-
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
22+
@Override
23+
public void addViewControllers(ViewControllerRegistry registry) {
24+
registry.addViewController("/results").setViewName("results");
25+
}
2026

21-
int i = Integer.parseInt(request.getParameter("num1"));
22-
int j = Integer.parseInt(request.getParameter("num2"));
27+
@GetMapping("/")
28+
public String showForm(PersonForm personForm) {
29+
return "form";
30+
}
2331

24-
int k = i + j;
32+
@PostMapping("/")
33+
public String checkPersonInfo(@Valid PersonForm personForm, BindingResult bindingResult) {
2534

26-
PrintWriter out = response.getWriter();
27-
out.println("OutPut:" + k);
35+
if (bindingResult.hasErrors()) {
36+
return "form";
2837
}
38+
39+
return "redirect:/results";
40+
}
2941
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package spring.jsp;
2+
3+
import javax.validation.constraints.Min;
4+
import javax.validation.constraints.NotNull;
5+
import javax.validation.constraints.Size;
6+
7+
/**
8+
* @Created 08 / 04 / 2020 - 11:35 AM
9+
* @project SpringJSP
10+
* @Author Hamdamboy
11+
*/
12+
public class PersonForm {
13+
14+
@NotNull
15+
@Size(min=2, max = 30)
16+
private String name;
17+
18+
@NotNull
19+
@Min(18)
20+
private Integer age;
21+
22+
public String getName(){
23+
return this.name = name;
24+
}
25+
26+
public void setName(String name){
27+
this.name = name;
28+
}
29+
30+
public Integer getAge(){
31+
return age;
32+
}
33+
34+
public void setAge(Integer age){
35+
this.age =age;
36+
}
37+
38+
public String toString(){
39+
return "Person (Name: " + this.name + ", Age: " + this.age + ")";
40+
}
41+
42+
43+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org">
4+
<head>
5+
<meta charset="ISO-8859-1">
6+
<title>Insert title here</title>
7+
</head>
8+
<body>
9+
<form action="#" th:action="@{/}" th:object="${personForm}"
10+
method="post">
11+
<table>
12+
<tr>
13+
<td>Name:</td>
14+
<td><input type="text" th:field="*{name}" /></td>
15+
<td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name
16+
Error</td>
17+
</tr>
18+
<tr>
19+
<td>Age:</td>
20+
<td><input type="text" th:field="*{age}" /></td>
21+
<td th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Age
22+
Error</td>
23+
</tr>
24+
<tr>
25+
<td><button type="submit">Submit</button></td>
26+
</tr>
27+
</table>
28+
</form>
29+
</body>
30+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Results</title>
6+
</head>
7+
<body>
8+
<h1> Conguratulations~! You are nice. </h1>
9+
</body>
10+
</html>

Part-4 Spring Boot REST API/SpringMysqlhiber/pom.xml renamed to Part-5 Spring Boot Web Application/SpringMysqlhiber/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<description>The project for Spring Boot</description>
1616

1717
<properties>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1820
<java.version>11</java.version>
1921
</properties>
2022

@@ -50,13 +52,45 @@
5052
</exclusion>
5153
</exclusions>
5254
</dependency>
55+
56+
<dependency>
57+
<groupId>org.apache.tomcat.embed</groupId>
58+
<artifactId>tomcat-embed-jasper</artifactId>
59+
<scope>provided</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>mysql</groupId>
63+
<artifactId>mysql-connector-java</artifactId>
64+
<scope>runtime</scope>
65+
</dependency>
66+
67+
<!-- JSTL for JSP -->
68+
<dependency>
69+
<groupId>javax.servlet</groupId>
70+
<artifactId>jstl</artifactId>
71+
</dependency>
72+
5373
<!--JSP Support on web -->
5474
<dependency>
5575
<groupId>org.apache.tomcat.embed</groupId>
5676
<artifactId>tomcat-embed-jasper</artifactId>
5777
<scope>provided</scope>
5878
</dependency>
5979

80+
<!-- Need this to compile JSP -->
81+
<dependency>
82+
<groupId>org.apache.tomcat.embed</groupId>
83+
<artifactId>tomcat-embed-jasper</artifactId>
84+
<scope>provided</scope>
85+
</dependency>
86+
87+
<!-- Optional, test for static content, bootstrap CSS -->
88+
<dependency>
89+
<groupId>org.webjars</groupId>
90+
<artifactId>bootstrap</artifactId>
91+
<version>3.3.7</version>
92+
</dependency>
93+
6094
</dependencies>
6195

6296
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package spring.mysqlhiber.controllers;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.ui.Model;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import spring.mysqlhiber.repository.ChildRepository;
8+
9+
/**
10+
* @Created 07 / 04 / 2020 - 5:51 PM
11+
* @project SpringMysqlhiber
12+
* @Author Hamdamboy
13+
*/
14+
15+
@Controller
16+
public class ChildController {
17+
//
18+
@Autowired
19+
ChildRepository childRepository;
20+
21+
@RequestMapping("/child")
22+
public String childeren(Model model){
23+
model.addAttribute("child", childRepository.findAll());
24+
return "index";
25+
}
26+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package spring.mysqlhiber.domain;
2+
3+
4+
import javax.persistence.*;
5+
6+
/**
7+
* @Created 07 / 04 / 2020 - 5:52 PM
8+
* @project SpringMysqlhiber
9+
* @Author Hamdamboy
10+
*/
11+
@Entity
12+
@Table(name = "child")
13+
public class Child {
14+
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.AUTO)
17+
private Integer id;
18+
private String name;
19+
20+
public Integer getId() {
21+
return id;
22+
}
23+
24+
public void setId(Integer id) {
25+
this.id = id;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
public Child(){}
37+
38+
public Child(Integer id, String name){
39+
this.id = id;
40+
this.name = name;
41+
}
42+
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package spring.mysqlhiber.repository;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
import spring.mysqlhiber.domain.Child;
6+
7+
/**
8+
* @Created 07 / 04 / 2020 - 5:52 PM
9+
* @project SpringMysqlhiber
10+
* @Author Hamdamboy
11+
*/
12+
@Repository
13+
public interface ChildRepository extends JpaRepository<Child, Integer> {
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
spring.mvc.view.prefix: /WEB-INF/jsp/
2+
spring.mvc.view.suffix: .jsp
3+
4+
logging.level.org.springframework = INFO
5+
6+
################### DataSource Configuration ##############
7+
8+
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
9+
spring.datasource.url=jdbc:mysql://localhost:3306/management
10+
spring.datasource.username=root
11+
spring.datasource.password=posilka2020
12+
13+
14+
###################### Hibernate Configuration #################
15+
16+
spring.jpa.hibernate.ddl-auto=update
17+
spring.jpa.show-sql=true
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
3+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
4+
<html lang="en">
5+
<head>
6+
7+
<link rel="stylesheet" type="text/css"
8+
href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />
9+
<c:url value="/css/main.css" var="jstlCss" />
10+
<link href="${jstlCss}" rel="stylesheet" />
11+
12+
</head>
13+
<body>
14+
<div class="container">
15+
<header>
16+
<h1>Spring MVC + JSP + JPA + Spring Boot 2</h1>
17+
</header>
18+
<div class="starter-template">
19+
<h3>Child List</h3>
20+
<table
21+
class="table table-striped table-hover table-condensed table-bordered">
22+
<tr>
23+
<th>Id</th>
24+
<th>Name</th>
25+
</tr>
26+
<c:forEach var="child" items="${child}">
27+
<tr>
28+
<td>${child.id}</td>
29+
<td>${child.name}</td>
30+
</tr>
31+
</c:forEach>
32+
</table>
33+
</div>
34+
35+
</div>
36+
37+
<script type="text/javascript"
38+
src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
39+
</body>
40+
41+
</html>

0 commit comments

Comments
 (0)