9 - Spring-Boot 2
9 - Spring-Boot 2
9 - Spring-Boot 2
WAR file is deployable application which can be deployed in any External Container to
run.
E:\JDK1.8.0
E:\JDK1.8.0\bin
E:\JDK1.8.0\lib
2) Set JAVA_HOME
set JAVA_HOME=E:\JDK1.8.0
E:\Tomcat8.5
E:\Tomcat8.5\bin\tomcat8
E:\Tomcat8.5\lib\*.jar
E:\Tomcat8.5\conf\server.xml
E:\Tomcat8.5\conf\web.xml
E:\Tomcat8.5\conf\tomcat-users.xml *
E:\apache-maven-3.6.3
E:\apache-maven-3.6.3\bin\mvn
E:\apache-maven-3.6.3\lib\*.jar
E:\apache-maven-3.6.3\confg\settings.xml
<user username="SomPrakash"
password="SomPrakash"
roles="manager-gui,manager-script" />
<server>
<id>jtcTomcatServer</id>
<username>SomPrakash</username>
<password>SomPrakash</password>
</server>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:5050/manager/text</url>
<server>jtcTomcatServer</server>
<path>/MyLab18</path>
</configuration>
</plugin>
2) Update application.properties
No Properies Required
9. pom.xml
<project…>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia.springboot</groupId>
<artifactId>Lab18</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>Lab18-jar</name>
<url>https://www.jtcindia.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<finalName>MyLab18</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:5050/manager/text</url>
<server>jtcTomcatServer</server>
<path>/MyLab18</path>
<update>true</update>
</configuration>
</plugin>
</plugins>
</build>
</project>
viewBook.jsp
1. index.jsp
<html>
<head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="card-header">
<h2 class="text-center">Welcome to JTC Bookstore</h2>
<h4 class="text-center"> Best Books and Best Videos</h4>
</div>
<br/><br/>
<div class="container" align="center">
<h2> <a href="showAllBooks">Display All Books</a> </h2>
</div>
</body>
</html>
<html>
<head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="card-header">
<h2 class="text-center">Welcome to JTC Bookstore</h2>
<h4 class="text-center"> Best Books and Best Videos</h4>
</div>
<br/><br/>
<div class="container" >
<table class="table table-striped table-bordered" style="font-size:20px;font-weight:bold;">
<tr>
<th>Book ID</th>
<th>Book Name</th>
<th>Author</th>
<th>Price</th>
<th>Category</th>
<th>Publications </th>
<th colspan="2" align="center">
<form:form action="addEditBookForm">
<input type="hidden" name="bookId" value="0"/>
<input type="submit" value=" Add New Book " class="btn btn-success btn-lg"/>
</form:form> </th>
</tr>
<c:forEach var="mybook" items="${MyBooksList}">
<tr>
<td> <a href="viewBook?bookId=${mybook.bid }"> ${mybook.bid } </a> </td>
<td>${mybook.bname }</td>
<td>${mybook.author }</td>
<td>${mybook.price }</td>
<td>${mybook.category }</td>
<td>${mybook.pub }</td>
<td>
<form:form action="addEditBookForm">
<input type="hidden" name="bookId" value="${mybook.bid }"/>
<input type="submit" value=" Edit " class="btn btn-primary btn-lg"/>
</form:form>
</td>
3. viewBook.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html> <head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="card-header">
<h2 class="text-center">Welcome to JTC Bookstore</h2>
<h4 class="text-center"> Best Books and Best Videos</h4>
</div>
<br/><br/>
<div class="container" >
<table class="table table-striped table-bordered" style="font-size:20px;font-weight:bold;">
<tr>
<td align="center" colspan="3"> Book Details</td>
</tr>
<tr>
<td> Book Id</td>
<td> ${MyBook.bid} </td>
</tr>
<tr>
<td> Book Name</td>
<td> ${MyBook.bname} </td>
</tr>
<tr>
<td>Author</td>
<td> ${MyBook.author} </td>
</tr>
4. addEditBook.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="card-header">
<h2 class="text-center">Welcome to JTC Bookstore</h2>
<h4 class="text-center"> Best Books and Best Videos</h4>
</div>
<br/><br/>
<div align="center" class="container">
<form:form action="saveUpdateBook" method="post" modelAttribute="mybook">
<table class="table" style="font-size:20px;font-weight:bold;">
<tr>
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
/*
* @Author : Som Prakash Rai
* @Company : Jtcindia
* @Website : www.jtcindia.org
* */
@Controller
public class IndexController {
@GetMapping("/")
public String showIndexPage() {
System.out.println("---------showIndexPage----------");
return "index";
}
6. BookController.java
package com.jtcindia.spring.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.jtcindia.spring.entity.Book;
import com.jtcindia.spring.service.BookService;
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : www.jtcindia.org
* */
@Controller
@SessionAttributes("MyBooksList")
public class BookController {
@GetMapping("/showAllBooks")
public String showBooksList(Model model) {
System.out.println("-------BookController--showBooksList()---------- ");
List<Book> blist=bookService.getAllBooks();
model.addAttribute("MyBooksList", blist);
return "booksList";
}
@PostMapping("/addEditBookForm")
public String addBookForm(@RequestParam("bookId") Integer bookId,Model model)
{ System.out.println("-------BookController--addEditBookForm()-------- ");
System.out.println(bookId);
Book book=new Book();
String opType="ADD";
if(bookId!=0) {
book=bookService.getBookById(bookId);
opType="UPDATE";
}
model.addAttribute("mybook", book);
model.addAttribute("OpType", opType);
return "addEditBook";
}
@PostMapping("/saveUpdateBook")
public String saveUpdateBook(@Valid @ModelAttribute("mybook") Book
book,BindingResult result,Model model,HttpServletRequest req) {
System.out.println("-------BookController--saveUpdateBook()---------- ");
String opType=req.getParameter("OpType");
System.out.println(opType); if(opType.equals("ADD"))
{
bookService.addBook(book);
}
if(opType.equals("UPDATE"))
{ bookService.updateBook(book);
}
List<Book> blist=bookService.getAllBooks();
model.addAttribute("MyBooksList", blist);
return "booksList";
}
return "booksList";
}
@GetMapping("/viewBook")
public String viewBook(@RequestParam("bookId") String bookId,Model model)
{ System.out.println("-------BookController--viewBook()-------- ");
System.out.println(bookId);
Book book=bookService.getBookById(Integer.parseInt(bookId));
model.addAttribute("MyBook", book);
return "viewBook";
}
7. BookService.java
package com.jtcindia.spring.service;
import java.util.List;
import com.jtcindia.spring.entity.Book;
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : www.jtcindia.org
* */
public interface BookService {
public List<Book> getAllBooks();
public Book getBookById(Integer bid);
public void addBook(Book book);
public void updateBook(Book book);
public void deleteBook(Integer bid);
}
8. BookServiceImpl.java
package com.jtcindia.spring.serviceimpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jtcindia.spring.dao.BookDAO;
@Autowired
BookDAO bookDAO;
@Override
public List<Book> getAllBooks() {
System.out.println("-----BookServiceImpl--getAllBooks()---------- ");
return bookDAO.getAllBooks();
}
@Override
public Book getBookById(Integer bid) {
System.out.println("-----BookServiceImpl--getBookById()---------- ");
return bookDAO.getBookById(bid);
}
@Override
public void addBook(Book book) {
System.out.println("-----BookServiceImpl--addBook()---------- ");
bookDAO.addBook(book);
}
@Override
public void updateBook(Book book) {
System.out.println("-----BookServiceImpl--updateBook()---------- ");
bookDAO.updateBook(book);
}
@Override
public void deleteBook(Integer bid) {
System.out.println("-----BookServiceImpl--deleteBook()---------- ");
bookDAO.deleteBook(bid);
}
}
import java.util.List;
import com.jtcindia.spring.entity.Book;
/*
* @Author : Som Prakash Rai
* @Company : Jtcindia
* @Website : www.jtcindia.org
* */
public interface BookDAO{
public List<Book> getAllBooks();
public Book getBookById(Integer bid);
public void addBook(Book book);
public void updateBook(Book book);
public void deleteBook(Integer bid);
}
10. BookDAOImpl.java
package com.jtcindia.spring.daoimpl;
import java.util.List;
import javax.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.jtcindia.spring.dao.BookDAO;
import com.jtcindia.spring.entity.Book;
/*
* @Author : Som Prakash Rai
* @Company : Jtcindia
* @Website : www.jtcindia.org
* */
@Repository
@Transactional
public class BookDAOImpl implements BookDAO{
@Autowired
EntityManager entityManager;
11. Book.java
package com.jtcindia.spring.entity;
import java.math.BigDecimal;
import javax.persistence.*;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/*
* @Author : Som Prakash Rai
* @Company : Jtcindia
* @Website : www.jtcindia.org
* */
@Entity
@Table(name="mybooks")
public class Book {
@Id
@Column(name="bid")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer bid;
@Column(name="bname")
@NotEmpty(message = "Book Name is Required")
@Size(min=5,max=20,message="Name length must be between 5 and 20")
private String bname;
@Column(name="price")
@NotNull(message = "Price is Required")
@Min(value=500, message="Price must be min : 500")
@Max(value=1500, message="Price must be max : 1500")
private BigDecimal price;
@Column(name="category")
@NotEmpty(message = "Category is Required")
private String category;
@Column(name="pub")
@NotEmpty(message = "Pub is Required")
private String pub;
public Book() {
}
//Constructors
//Setters and Getters
@Override
public String
toString() {
return "[" + bid + " , " + bname + " , " + author + " , " + price + " , " + category + " , " + pub+ "]";
}
}
12. JTCWebConfig.java
package com.jtcindia.spring.config;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.*;
/*
* @Author : Som Prakash Rai
* @Company : Jtcindia
* @Website : www.jtcindia.org
* */
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{ registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-
INF/resources/webjars/");
}
}
13. StartMyBootApp.java
package com.jtcindia.spring.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/*
* @Author : Som Prakash Rai
* @Company : Jtcindia
* @Website : www.jtcindia.org
* */
@SpringBootApplication(scanBasePackages = {"com.jtcindia.spring"}) @EnableWebMvc
public class StartMyBootApp extends
SpringBootServletInitializer{ public static void
main(String[] args) {
SpringApplication.run(StartMyBootApp.class, args);
}
}
14. application.properties
server.port=5599
logging.level.root=INFO
logging.pattern.console=%-5level %logger{36} - %msg%n
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
15. pom.xml
<project ….>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia.springboot</groupId>
<artifactId>Lab19</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Lab19-jar</name>
<url>https://www.jtcindia.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<finalName>MyLab19</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>