8 - Spring-Boot 2
8 - Spring-Boot 2
8 - Spring-Boot 2
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.16.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>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-devtools</artifactId>
</dependency>
</dependencies>
@SpringBootApplication
public class JTCWebConfig implements WebMvcConfigurer{
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/myjsps/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
@SpringBootApplication(scanBasePackages = { "com.jtcindia.springboot" })
public class StartMyBootApp extends SpringBootServletInitializer {
public static void main(String[] args)
{ SpringApplication.run(StartMyBootApp.class,
args);
}
<!DOCTYPE html>
<html>
<body>
<h1 class="text-center"> Welcome to Jtcindia !!!</h1>
</body>
</html>
@Controller
public class IndexController { @GetMapping("/")
public String showIndexPage()
{ System.out.println("IndexController - showIndexPage()");
return "index";
}
}
18) Open the browser and hit http://localhost:12345/ If every thing fine you will see
index.jsp
C) Enable BootStarp:
19) Specify the Dependencies for Jquery and Bootstarp
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
b) jquery-3.4.1.jar =>
META-INF=> resources => webjars => jquery => 3.4.1 => jquery.min.js
1. index.jsp 2. booksList.jsp
3. Book.java 4. IndexController.java
5. BooksController.java 6. JTCWebConfig.java
7. StartMyBootApp.java 8. application.properties
9. pom.xml
1. index.jsp
<!DOCTYPE html>
<html>
<head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h1 class="text-center"> Welcome to Jtcindia !!!</h1>
<br/>
<h2 class="text-center"> <a href="showBooks"> Show Books </a></h2>
<script src="webjars/jquery/3.4.1/jquery.min.js"></script>
<script src="webjars/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</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="container">
<h1 class="text-center"> Welcome to Jtcindia !!!</h1>
<div class="alert alert-primary" role="alert">
<h2 class="text-center"> Latest Books from Som Prakash Rai </h2>
</div>
<br/>
<table class="table table-striped table-bordered table-hover" style="font-size:20px;">
<thead class="thead-light ">
<tr> <th> Book Id </th>
<th> Book Name </th>
<th> Author</th>
<th> Price </th>
<th> Publications</th>
<th> </th>
<th> </th> </tr>
</thead>
<tbody>
<c:forEach var="mybook" items="${MyBooks}">
<tr>
<td> <a href="#"> ${mybook.bid} </a> </td>
<td> ${mybook.bname} </td>
<td> ${mybook.author} </td>
<td> ${mybook.price} </td>
<td> ${mybook.pub}</td>
<td> <button type="button" class="btn btn-success btn-lg">View Book</button> </td>
<td> <button type="button" class="btn btn-primary btn-lg">Edit Book</button> </td>
</tr>
</c:forEach>
<tbody>
</table>
</div>
<script src="webjars/jquery/3.4.1/jquery.min.js"></script>
<script src="webjars/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
</html>
import java.math.BigDecimal;
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : www.jtcindia.org
* */
public class Book
{ private Integer bid;
private String bname;
private String author;
private BigDecimal price;
private String pub;
public Book() {}
//Constructors
//Setters and Getters
}
4. IndexController.java
package com.jtcindia.springboot;
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("IndexController - showIndexPage()");
return "index";
}
}
import java.math.BigDecimal;
import java.util.*;
import javax.servlet.http.HttpSession;
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 BooksController {
@GetMapping("/showBooks")
public String getBooks(HttpSession session)
{ System.out.println(" BooksController - getBooks()");
session.setAttribute("MyBooks", blist);
return "booksList";
}
}
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : www.jtcindia.org
* */
@SpringBootApplication
public class JTCWebConfig implements
WebMvcConfigurer{ @Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/myjsps/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{ registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-
INF/resources/webjars/");
}
}
7. StartMyBootApp.java
package com.jtcindia.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : www.jtcindia.org
* */
8. application.properties
server.port=12345
9. pom.xml
<project…>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia.springboot</groupId>
<artifactId>Lab16</artifactId>
<version>1.0</version>
<name>MyLab16</name>
<url>https://www.jtcindia.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.16.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>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<finalName>MyLab16</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<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-thymeleaf</artifactId>
</dependency>
1. index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<script src="webjars/jquery/3.4.1/jquery.min.js"></script>
<script src="webjars/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body> </html>
2. booksList.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1 class="text-center"> Welcome to Jtcindia !!!</h1>
<div class="alert alert-primary" role="alert">
<h2 class="text-center"> Latest Books from Som Prakash Rai </h2>
</div>
<br/>
<table class="table table-striped table-bordered table-hover" style="font-size:20px;">
<thead class="thead-light ">
<tr> <th> Book Id </th>
<th> Book Name </th>
<th> Author</th>
<th> Price </th>
<th> Publications</th>
<th> </th> <th> </th>
</tr> </thead>
<tbody >
<tr th:each="mybook : ${session.MyBooks}">
<td th:text="${mybook.bid}"> </td>
<td th:text="${mybook.bname}"> </td>
<td th:text="${mybook.author}"></td>
<td th:text="${mybook.price}"> </td>
<td th:text="${mybook.pub}"> </td>
<td> <button type="button" class="btn btn-success btn-lg">View Book</button> </td>
<td> <button type="button" class="btn btn-danger btn-lg">Edit Book</button> </td>
</tr>
<tbody>
</table>
</div>
</body> </html>
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : www.jtcindia.org
* */
@SpringBootApplication
public class JTCWebConfig implements
WebMvcConfigurer{ @Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{ registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-
INF/resources/webjars/");
}
}
7. StartMyBootApp.java
8. application.properties
server.port=54321
spring.thymeleaf.cache=false
9. pom.xml
<project…>
<modelVersion>4.0.0</modelVersion>
<groupId>com.jtcindia.springboot</groupId>
<artifactId>Lab17</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Lab17-jar</name>
<url>https://www.jtcindia.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.16.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<finalName>MyLab17</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>