8 - Spring-Boot 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

 You can develop Spring MVC application with Spring Boot in two ways.

1. Develop as FAT JAR file.


2. Develop as WAR file
 FAT Jar is self executable application which contains Application Code +
Embedded Container
 You can execute the FAT jar as follows.
Java –jar HelloApp.jar
 WAR file is deployable application which can be deployed in any External
Container to run.

Lab16: Spring MVC with Boot (with JSP)


Working Steps:
A) Setup the Project
1) Create the maven Project.
a) Open the Eclipse required Workspace
b) Select New -> Maven Project.
c) Check the checkbox for create Simple Project(skip the Archetype selection) and
Click on Next Button.
d) Provide the following
Group Id: com.jtcindia.springboot Artifact Id :
Lab16
Version : 1.0
packaging : jar
Name : MyLab16and Click On finish button.

2) Remove the following two Source Folders.


a. src/test/java
b. src/test/resources

3) Create application.properties under src/main/resources

4) Update application.properties as follows


server.port=12345

5) Create the Source Folder called src/main/webapp


6) Create the Folder called WEB-INF under src/main/webapp
7) Create the Folder called myjsps under WEB-INF

www.jtcindia.org 91 Spring Boot 2


8) Update pom.xml by adding the following dependencies.

<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>

9) You can see the Error in Maven Project.


Fix the Error by updating the Maven Project.
Select the Project , right click and then select Maven -> Update Project.

10) Create the Package called com.jtcindia.springboot under src/main/java

www.jtcindia.org 92 Spring Boot 2


11) Write Bean Configuration class – JTCWebConfig.java
12) Configure View Resolver in JTCWebConfig.java

@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;
}
}

13) Write Boot Main Class – StartMyBootApp.java

@SpringBootApplication(scanBasePackages = { "com.jtcindia.springboot" })
public class StartMyBootApp extends SpringBootServletInitializer {
public static void main(String[] args)
{ SpringApplication.run(StartMyBootApp.class,
args);
}

14) Run the Boot Application


You will see – Whitelabel Error Page means DS is Ready.

B) Develop the Usecase : show index page


http://localhost:12345/ => You are sending the request for /

15) Write index.jsp under WEB-INF/myjsps folder.

<!DOCTYPE html>
<html>
<body>
<h1 class="text-center"> Welcome to Jtcindia !!!</h1>
</body>
</html>

www.jtcindia.org 93 Spring Boot 2


16) Write IndexController.java

@Controller
public class IndexController { @GetMapping("/")
public String showIndexPage()
{ System.out.println("IndexController - showIndexPage()");
return "index";
}
}

17) Run the Boot Application

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>

20) Observe the following


a) bootstrap-4.3.1.jar
META-INF=> resources => webjars => bootstarp => 4.3.1 => css => bootstrap.min.css
META-INF=> resources => webjars => bootstarp => 4.3.1 => js => bootstrap.min.js

b) jquery-3.4.1.jar =>
META-INF=> resources => webjars => jquery => 3.4.1 => jquery.min.js

www.jtcindia.org 94 Spring Boot 2


21) Add the resource handler bean (to make your resources available in class path)
in JTCWebConfig
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{ registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META
-INF/resources/webjars/");
}

22) Include the css and js files in index.jsp.

<link href="webjars/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">


<script src="webjars/jquery/3.4.1/jquery.min.js"></script>
<script src="webjars/bootstrap/4.3.1/js/bootstrap.min.js"></script>

23) Use Some BootStrap Classes in index.jsp

<h1 class="text-center"> Welcome to Jtcindia !!!</h1>

24) Run the Boot Application and Test

25) Open the browser and hit http://localhost:12345/


If every thing fine you will see Bootstrap Css styles applied in index.jsp

D) Develop the Usecase : show books


http://localhost:12345/showBooks

26) Add hyperlink in index.jsp

<h2 class="text-center"> <a href="showBooks"> Show Books </a></h2>

27) Write Book.java under com.jtcindia.springboot


28) Write BooksController.java under com.jtcindia.springboot
29) Write booksList.jsp under WEB-INF/myjsps
30) Run the Boot Application and Test
Open the browser and hit http://localhost:12345/

www.jtcindia.org 95 Spring Boot 2


Lab16: Files Required

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>

www.jtcindia.org 96 Spring Boot 2


2. booksList.jsp
<%@ 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="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>

www.jtcindia.org 97 Spring Boot 2


3. Book.java
spackage com.jtcindia.springboot;

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";
}
}

www.jtcindia.org 98 Spring Boot 2


5. BooksController.java
package com.jtcindia.springboot;

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()");

List<Book> blist=new ArrayList<>();


Book mybook1=new Book(101,"Master Spring Boot","Som Prakash Rai
",BigDecimal.valueOf(10000.0),"JTC");

Book mybook2=new Book(102,"Master MicroServices","Som Prakash


Rai",BigDecimal.valueOf(10000.0),"JTC");
Book mybook3=new Book(103,"Master Angular","Som Prakash
Rai",BigDecimal.valueOf(10000.0),"JTC");

Book mybook4=new Book(104,"Master React ","Som Prakash


Rai",BigDecimal.valueOf(10000.0),"JTC");

Book mybook5=new Book(105,"Master Java Full Stack","Som Prakash


Rai",BigDecimal.valueOf(10000.0),"JTC");

blist.add(mybook1); blist.add(mybook2); blist.add(mybook3);


blist.add(mybook4); blist.add(mybook5);

session.setAttribute("MyBooks", blist);

return "booksList";
}
}

www.jtcindia.org 99 Spring Boot 2


6. JTCWebConfig.java
package com.jtcindia.springboot;

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
* */

www.jtcindia.org 100 Spring Boot 2


@SpringBootApplication(scanBasePackages = { "com.jtcindia.springboot" }) public
class StartMyBootApp extends SpringBootServletInitializer {
public static void main(String[] args)
{ SpringApplication.run(StartMyBootApp.class,
args);
}
}

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>

www.jtcindia.org 101 Spring Boot 2


<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>
<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>

www.jtcindia.org 102 Spring Boot 2


Lab17: Spring MVC with Boot (using ThymeLeaf)
Working Steps:
1) Copy Lab16 as Lab17
2) Remove the WEB-INF under src/main/webapp ( So that all the JSP’s will be
deleted)
3) Remove ViewResolver from JTCWebConfig.java
4) Create templates folder under src/main/resources
5) Place index.html and booksList.html under templates
a)index.html (new in Lab17)
b)booksList.html (new in Lab17)

6) Add the following property in application.properties


server.port=54321
spring.thymeleaf.cache=false

7) Remove the following dependencies from pom.xml


<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>

8) Add the dependency for ThymeLeaf in pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

www.jtcindia.org 103 Spring Boot 2


9) Make sure that all the Files as given below are updated correctly.

10) Run the Boot Application

11) Open the browser and hit http://localhost:54321/

Lab17: Files Required

1. index.html New in Lab17


2. booksList.html New in Lab17
3. Book.java Same as Lab16
4. IndexController.java Same as Lab16
5. BooksController.java Same as Lab16
6. JTCWebConfig.java Updated in Lab17
7. StartMyBootApp.java Same as Lab16
8. application.properties Updated in Lab17
9. pom.xml Updated in Lab17

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>

www.jtcindia.org 104 Spring Boot 2


<h1 class="text-center"> Welcome to Jtcindia !!!</h1>
<br/>
<h2 class="text-center"> <a th: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>
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>

www.jtcindia.org 105 Spring Boot 2


3. Book.java
4. IndexController.java
5. BooksController.java
6. JTCWebConfig.java
package com.jtcindia.springboot;

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>

www.jtcindia.org 106 Spring Boot 2


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<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>

www.jtcindia.org 107 Spring Boot 2

You might also like