This article has been authorized by the author Yi Guoqiang netease cloud community.

Welcome to visit netease Cloud Community to learn more about Netease’s technical product operation experience.

To solve the problem

  • With the gradual adoption of the Spring Boot framework, we expect to retrofit some of our existing systems into generic scaffolding that can be used in subsequent new projects.

  • This chapter mainly introduces how to use Spring Boot to transform the traditional JSP project.

The difficulties in

  • First, the Spring Boot + JSP approach is definitely available. However, the official has long said that it is not recommended to use JSP to carry out front-end page development, the official recommendation is the Themeleaf, more in line with the development mode of the separation of the front and back end.

  • Using Spring Boot + JSP, the main is to pay attention to the configuration of the changes, the following together.

Used to introduce

  • Since we choose the Spring Boot framework, it is still recommended to package deployment in the form of JAR, so that developers can more conveniently control the parameter configuration of tomcat container (built-in default container), and application deployment is easier.

  • First, the Packaging type in the project pom.xml file is guaranteed to be of type JAR. The following is an example:

< < the groupId > com.net help ease. Ms/groupId > < artifactId > common - ms < / artifactId > < packaging > jar < / packaging > < version > 1.0.0 < / version >Copy the code
  • The version of Spring Boot can be determined according to the use requirements. Here I used the latest stable version 1.5.9.RELEASE. The following is an example:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> < version > 1.5.9. RELEASE < / version > < / parent >Copy the code
  • Then, introduce the starter-Web component, which contains the built-in Tomcat container. The following is an example:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId></dependency>Copy the code
  • In addition, you need to add built-in Tomcat support for JSP file parsing and, if useful for JSTL tags, dependencies. The following is an example:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId></dependency><dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>${jstl.version}</version></dependency>Copy the code
  • Install the spring-boot-Maven-plugin (1.4.2.RELEASE) to install the spring-boot-Maven-plugin (1.4.2.RELEASE). I have tested that it is not possible to use the default 1.5.9.RELEASE or other 1.5.x versions (IDEA can be run directly and started without any problem, but a 404 error will be reported if the IDEA is put into jar package and run separately, and the relevant JSP page cannot be found). As for whether other versions other than 1.4.2.RELEASE are feasible, I have not tried. Readers with clear reasons are welcome to comment. META_INF/resources is used to copy files from the webApp directory to META_INF/resources. This allows spring Boot’s FAT JAR to recognize the resource path at boot time.

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.2.RELEASE</version> <configuration> <mainClass>test.Application</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> <dependencies> <! -- Spring hot deployment, Springframework </groupId> <artifactId> Springloaded </artifactId> <version>1.2.6.RELEASE</version> </dependency> </dependencies> </dependencies> </plugin> <! -- Ignore no web. XML warning required if packaged as war --> <! --<plugin>--> <! --<groupId>org.apache.maven.plugins</groupId>--> <! --<artifactId>maven-war-plugin</artifactId>--> <! --<configuration>--> <! --<failOnMissingWebXml>false</failOnMissingWebXml>--> <! --</configuration>--> <! --</plugin>--> </plugins> <resources> <! Copy the JSP file to the meta-INF directory --> <resource> <! <directory> SRC /main/webapp</directory> <! <targetPath>META-INF/resources</targetPath> <includes> <include>**/* </include> </includes> </resource> <! <directory> SRC /main/resources</directory> <includes> <include>**/**</include> </includes> <filtering>false</filtering>
            </resource>
        </resources>
    </build>Copy the code
  • After the above configuration is complete, the necessary configuration of the pom.xml file is ok. Next, we move the static resources used in the project from the original webapp directory to the SRC /main/resources directory. The main directory structure is shown below.

  • In the application. Properties file, we configure the JSP resolution prefix and suffix, as well as the static resource mapping path, as shown in the following. Make the corresponding changes, such as spring.mvc. View.prefix =/

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
Define the mapping path of static resources
spring.mvc.static-path-pattern=/resources/**Copy the code
  • In addition, in traditional Web projects, there must be a web. XML file, about this file, we mainly configure some servlets, filters and listeners, change the way to code configuration is also very easy, you can refer to the previous series of articles to adapt the corresponding, here will not repeat.

  • Another point that needs to be mentioned is that when we transform our traditional projects into Spring Boot projects, we often feel very troublesome, mainly because of the de-xmlization of Spring Boot. If we change the configuration of existing XML to Java code implementation, it will really crash. Therefore, for the transformation of existing projects, it is easier and more convenient to retain the original XML configuration mode as much as possible. The following is an example:

@ImportResource("classpath:spring/application-context.xml")@SpringBootApplication(scanBasePackages = {"..."})public class CommonMsApplication {
    ......
}Copy the code

The last

  • After the transformation is completed, it is found that the use of Spring Boot + JSP is not complicated, but in the transformation of traditional projects, the corresponding configuration needs to make some changes. Of course, each project uses different components, perhaps in the actual transformation process, will encounter this or that problem, patience to step by step to find the cause of the problem, I believe that can be quickly solved.

  • The inadequacy, welcome to correct, thank ~


Free experience cloud security (EASY Shield) content security, verification code and other services

For more information about netease’s technology, products and operating experience, please click here.




Introduction to Continuation