0% found this document useful (0 votes)
11 views17 pages

Spring Boot with Bootstrap Setup Guide

Uploaded by

Rahul Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views17 pages

Spring Boot with Bootstrap Setup Guide

Uploaded by

Rahul Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

 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 [Link]
 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: [Link] 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 [Link] under src/main/resources

4) Update [Link] as follows


[Link]=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

[Link] 91 Spring Boot 2


8) Update [Link] by adding the following dependencies.

<parent>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
</parent>

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>[Link]</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>[Link]</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 [Link] under src/main/java

[Link] 92 Spring Boot 2


11) Write Bean Configuration class – [Link]
12) Configure View Resolver in [Link]

@SpringBootApplication
public class JTCWebConfig implements WebMvcConfigurer{

@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
[Link]("/WEB-INF/myjsps/");
[Link](".jsp");
[Link]([Link]);
return resolver;
}
}

13) Write Boot Main Class – [Link]

@SpringBootApplication(scanBasePackages = { "[Link]" })
public class StartMyBootApp extends SpringBootServletInitializer {
public static void main(String[] args)
{ [Link]([Link],
args);
}

14) Run the Boot Application


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

B) Develop the Usecase : show index page


[Link] => You are sending the request for /

15) Write [Link] under WEB-INF/myjsps folder.

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

[Link] 93 Spring Boot 2


16) Write [Link]

@Controller
public class IndexController { @GetMapping("/")
public String showIndexPage()
{ [Link]("IndexController - showIndexPage()");
return "index";
}
}

17) Run the Boot Application

18) Open the browser and hit [Link] If every thing fine you will see
[Link]

C) Enable BootStarp:
19) Specify the Dependencies for Jquery and Bootstarp

<dependency>
<groupId>[Link]</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>

20) Observe the following


a) [Link]
META-INF=> resources => webjars => bootstarp => 4.3.1 => css => [Link]
META-INF=> resources => webjars => bootstarp => 4.3.1 => js => [Link]

b) [Link] =>
META-INF=> resources => webjars => jquery => 3.4.1 => [Link]

[Link] 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)
{ [Link]("/webjars/**").addResourceLocations("classpath:/META
-INF/resources/webjars/");
}

22) Include the css and js files in [Link].

<link href="webjars/bootstrap/4.3.1/css/[Link]" rel="stylesheet">


<script src="webjars/jquery/3.4.1/[Link]"></script>
<script src="webjars/bootstrap/4.3.1/js/[Link]"></script>

23) Use Some BootStrap Classes in [Link]

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

24) Run the Boot Application and Test

25) Open the browser and hit [Link]


If every thing fine you will see Bootstrap Css styles applied in [Link]

D) Develop the Usecase : show books


[Link]

26) Add hyperlink in [Link]

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

27) Write [Link] under [Link]


28) Write [Link] under [Link]
29) Write [Link] under WEB-INF/myjsps
30) Run the Boot Application and Test
Open the browser and hit [Link]

[Link] 95 Spring Boot 2


Lab16: Files Required

1. [Link] 2. [Link]
3. [Link] 4. [Link]
5. [Link] 6. [Link]
7. [Link] 8. [Link]
9. [Link]

1. [Link]
<!DOCTYPE html>
<html>
<head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/[Link]" 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/[Link]"></script>
<script src="webjars/bootstrap/4.3.1/js/[Link]"></script>

</body>
</html>

[Link] 96 Spring Boot 2


2. [Link]
<%@ taglib uri="[Link] prefix="c" %>

<html> <head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/[Link]" 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="#"> ${[Link]} </a> </td>
<td> ${[Link]} </td>
<td> ${[Link]} </td>
<td> ${[Link]} </td>
<td> ${[Link]}</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/[Link]"></script>
<script src="webjars/bootstrap/4.3.1/js/[Link]"></script>
</body>
</html>

[Link] 97 Spring Boot 2


3. [Link]
spackage [Link];

import [Link];
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : [Link]
* */
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. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : [Link]
* */
@Controller
public class IndexController {

@GetMapping("/")
public String showIndexPage()
{ [Link]("IndexController - showIndexPage()");
return "index";
}
}

[Link] 98 Spring Boot 2


5. [Link]
package [Link];

import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : [Link]
* */
@Controller
public class BooksController {

@GetMapping("/showBooks")
public String getBooks(HttpSession session)
{ [Link](" BooksController - getBooks()");

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


Book mybook1=new Book(101,"Master Spring Boot","Som Prakash Rai
",[Link](10000.0),"JTC");

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


Rai",[Link](10000.0),"JTC");
Book mybook3=new Book(103,"Master Angular","Som Prakash
Rai",[Link](10000.0),"JTC");

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


Rai",[Link](10000.0),"JTC");

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


Rai",[Link](10000.0),"JTC");

[Link](mybook1); [Link](mybook2); [Link](mybook3);


[Link](mybook4); [Link](mybook5);

[Link]("MyBooks", blist);

return "booksList";
}
}

[Link] 99 Spring Boot 2


6. [Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : [Link]
* */
@SpringBootApplication
public class JTCWebConfig implements
WebMvcConfigurer{ @Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
[Link]("/WEB-INF/myjsps/");
[Link](".jsp");
[Link]([Link]);
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{ [Link]("/webjars/**").addResourceLocations("classpath:/META-
INF/resources/webjars/");
}
}

7. [Link]
package [Link];

import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : [Link]
* */

[Link] 100 Spring Boot 2


@SpringBootApplication(scanBasePackages = { "[Link]" }) public
class StartMyBootApp extends SpringBootServletInitializer {
public static void main(String[] args)
{ [Link]([Link],
args);
}
}

8. [Link]
[Link]=12345

9. [Link]
<project…>
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>Lab16</artifactId>
<version>1.0</version>
<name>MyLab16</name>
<url>[Link]

<parent>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
</parent>

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

[Link] 101 Spring Boot 2


<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>

<build>
<finalName>MyLab16</finalName>
<plugins>

<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

[Link] 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 [Link]
4) Create templates folder under src/main/resources
5) Place [Link] and [Link] under templates
a)[Link] (new in Lab17)
b)[Link] (new in Lab17)

6) Add the following property in [Link]


[Link]=54321
[Link]=false

7) Remove the following dependencies from [Link]


<dependency>
<groupId>[Link]</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 [Link]

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

[Link] 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 [Link]

Lab17: Files Required

1. [Link] New in Lab17


2. [Link] New in Lab17
3. [Link] Same as Lab16
4. [Link] Same as Lab16
5. [Link] Same as Lab16
6. [Link] Updated in Lab17
7. [Link] Same as Lab16
8. [Link] Updated in Lab17
9. [Link] Updated in Lab17

1. [Link]
<!DOCTYPE html>
<html xmlns:th="[Link]
<head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/[Link]" rel="stylesheet">
</head>
<body>

[Link] 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/[Link]"></script>
<script src="webjars/bootstrap/4.3.1/js/[Link]"></script>
</body> </html>
2. [Link]
<!DOCTYPE html>
<html xmlns:th="[Link]
<head>
<title>JTC Bookstore</title>
<link href="webjars/bootstrap/4.3.1/css/[Link]" 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 : ${[Link]}">
<td th:text="${[Link]}"> </td>
<td th:text="${[Link]}"> </td>
<td th:text="${[Link]}"></td>
<td th:text="${[Link]}"> </td>
<td th:text="${[Link]}"> </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>

[Link] 105 Spring Boot 2


3. [Link]
4. [Link]
5. [Link]
6. [Link]
package [Link];

import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash Rai
* @Company : JTCINDIA
* @Website : [Link]
* */
@SpringBootApplication
public class JTCWebConfig implements
WebMvcConfigurer{ @Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{ [Link]("/webjars/**").addResourceLocations("classpath:/META-
INF/resources/webjars/");
}
}
7. [Link]
8. [Link]
[Link]=54321
[Link]=false

9. [Link]
<project…>
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>Lab17</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Lab17-jar</name>
<url>[Link]

<parent>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
</parent>

[Link] 106 Spring Boot 2


<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

</dependencies>
<build>
<finalName>MyLab17</finalName>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

[Link] 107 Spring Boot 2

Common questions

Powered by AI

To convert a Spring MVC application using JSPs to Thymeleaf in Spring Boot, follow these steps: Firstly, create a new project for the Thymeleaf-based application. Remove the WEB-INF directory and all JSP files. Update the JTCWebConfig class to remove the ViewResolver configuration for JSPs and adjust the resources handling. Create a 'templates' folder under src/main/resources and place index.html and booksList.html files there. Update application.properties to disable Thymeleaf caching. In pom.xml, remove dependencies for Tomcat Embed Jasper and JSTL, and add the spring-boot-starter-thymeleaf dependency. Finally, update all configuration and resource files as necessary.

When switching from JSP to Thymeleaf in a Spring Boot application, removing Maven dependencies related to JSP (like tomcat-embed-jasper and jstl) is necessary because they are no longer needed for the application's configuration or execution. These dependencies facilitate JSP rendering, which is not required for Thymeleaf templates that instead use their own processing engine. Removing unnecessary dependencies streamlines the application, reduces potential conflicts, and optimizes the build.

To display a list of books in a Spring Boot application using Thymeleaf, start by creating a `Book` class to define the book entity. Implement a controller class (`BooksController`) to handle requests and manage book data. In the controller, define a `getBooks` method that populates a list of books and adds it to the session model. Create a Thymeleaf template (booksList.html) that uses th:each to iterate over the list of books. Bind the book data to the view fields using Thymeleaf expressions such as th:text. Ensure the controller maps the URL endpoint to the template using annotations like `@GetMapping`.

FAT JAR files offer several benefits over WAR files when deploying Spring Boot applications. They contain both the application code and an embedded server, making them self-contained and executable with a simple Java command. This allows for easier deployment and testing across different environments since they don't depend on an external servlet container. FAT JARs simplify DevOps processes by making the application portable and reducing environment-specific configurations. Conversely, WAR files require a servlet container such as Tomcat for deployment, which can complicate provisioning and scripting during deployment.

The `@SpringBootApplication` annotation in Spring Boot combines three crucial annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan. This simplification allows for auto-configuration of a Spring application context, the scanning of components in the application's package, and the setup of default configurations. It decreases boilerplate code and simplifies application setup and execution, making it a powerful convenience when building Spring Boot applications.

To set up basic routing in a Spring Boot MVC application using controllers, first create a controller class with the `@Controller` annotation to denote it as a handler. Use `@GetMapping` or `@RequestMapping` annotations on methods within this class to map incoming requests to methods. Each method should return a view name as a String, which the view resolver uses to return a specific HTML or JSP page. For example, a method annotated with `@GetMapping("/")` could return the string "index" to serve the index.jsp page.

Adding a resource handler in the JTCWebConfig class in a Spring Boot application configures the application to serve static resources such as CSS, JavaScript, and image files stored in specific locations like the 'classpath:/META-INF/resources/webjars/'. The handler maps URL paths to these resource locations using `ResourceHandlerRegistry`. This setup is essential to correctly link front-end resources to the web app, enabling dynamic content updating and responsive design features.

To set up a Spring Boot MVC project with Bootstrap and JQuery, first include their dependencies in the pom.xml using webjars. Add the following dependencies: org.webjars:bootstrap, and org.webjars:jquery with their respective versions. In the JTCWebConfig class, define a resource handler to serve webjars resources. In the HTML or JSP files, include Bootstrap and JQuery files in the head section using link and script tags to point to the webjars paths. This integration allows the application to utilize Bootstrap's CSS and JavaScript for styling and responsiveness, and JQuery for dynamic content handling.

To ensure that changes to Thymeleaf templates are reflected immediately in a Spring Boot application, you should disable Thymeleaf's cache. This is done by adding the property `spring.thymeleaf.cache=false` to the application.properties file. This configuration enables developers to see changes to the templates in real-time without needing to restart the application, which facilitates faster development and testing cycles.

The InternalResourceViewResolver in a Spring Boot application facilitates the resolution of logical view names to URL paths for JSP pages under the Web-INF directory. It is configured to interpret view names as file paths that are resolved to actual JSP pages in the application. In the configuration, the resolver sets a prefix for the directory where JSP files are stored and a suffix for the file extension. This setup helps in managing how views are located and returned in response to requests. It uses JstlView class to support JSP with JSTL tags.

You might also like