Convert a JSP/Servlet project to a Spring Boot project

Posted by Pig on Fri, 31 Dec 2021 09:19:15 +0100

Create Spring Boot application

Before refactoring JSP applications based on Spring, we first introduce Spring Boot to make it a Spring Boot application. You only need to add simple configuration and a small amount of code on the basis of the original code to introduce Spring Boot. The whole process can be completed in 5 minutes.

Here, we use Maven to manage dependencies because we need to add Spring Boot related dependencies. You can also create a new Maven project in Eclipse or IntelliJ.

Add Spring Boot related configurations to the POM file.

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <scope>provided</scope>
    </dependency>
  </dependencies>

Copy original code

If you create a new Spring Boot project, copy all the code in the src/main/java directory of the original project to src/main/java in the Spring Boot project.

Create a New code directory ([New] - [Source Folder]) named src/main/webapp, and copy all the contents in the WebContent directory of the original project to this directory.

Add Spring Boot code

Add a startup class of Spring Boot application, which does not involve any business logic. Here, we add a @ ServletComponentScan annotation to BlogApplication, which will enable the application to scan the Servlet class labeled @ WebServlet and initialize it accordingly.

package com.tianmaying;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class BlogApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(BlogApplication.class, args);
    }

}

Note: the package path of the Servlet class must be a sub path of the package path of the BlogApplication to be scanned. Otherwise, the package of the Servlet class needs to be specified through the basePackages property

Automatic deployment and refresh

Automatic deployment

In order to make the development more convenient, we further optimize the IDE so that the Web application can restart automatically and the Web page can refresh automatically when the page content is modified, which can greatly improve our development experience and development efficiency.

In order to enable Spring Boot to restart automatically, we only need to add the following dependencies to the POM file.

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

After adding this dependency, you can experiment with the following. For example, we modify the following blogapplication Java code, you can see that the embedded Web server is restarted automatically in the console.

Browser auto refresh

After modifying the code, we usually return to the browser and refresh the page through mouse operation or F5. Although this operation takes a short time, countless occurrences will still take up a lot of our time. We can free the F5 key by installing the livereload plug-in for the browser. In the future, as long as there is code modification, you can immediately see the effect by switching back to the browser.

visit http://livereload.com/extensions/ , according to your browser type, you can download the response plug-in extension program.

Topics: Java Web Development Spring Spring Boot