Catalog
Take Maven Project as an example to illustrate how to integrate JSP into SpringBook Project
1. Add JSP to IDE
Add the following directory for placing jsp files
src/main/webapp/WEB-INF/jsp
maven configures pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.test</groupId> <artifactId>SpringBoot-jsp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>SpringBoot-jsp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring-boot.version>2.1.7.RELEASE</spring-boot.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath /> </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> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
java code:
package cn.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication @EnableAutoConfiguration(exclude = { ErrorMvcAutoConfiguration.class }) public class App { public static void main(String[] args) { ConfigurableApplicationContext ctx = SpringApplication.run(App.class, args); ctx.registerShutdownHook(); } }
package cn.test; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @RequestMapping("/") @Controller public class BaseController { @GetMapping(path = "welcome.do") public String doHeartbeat() { return "welcome"; } }
spring configuration file:
spring.application.name=springboot-jsp
server.port=8080
server.servlet.context-path=/# Setting up jsp storage path
spring.mvc.view.prefix=/WEB-INF/jsp/
# Setting jsp suffix
spring.mvc.view.suffix=.jsp
maven can be used to run:
mvn clean spring-boot:run
You can also run App.class directly.
Adding JSP to JAR
Add the following to pom.xml:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.1.7.RELEASE</version> <configuration> <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments> <mainClass>com.test.App</mainClass> <layout>ZIP</layout> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/webapp</directory> <targetPath>META-INF/resource</targetPath> <includes> <include>**/**</include> </includes> </resource> </resources> </build>
Execute the following command to package jsp files into JAR
mvn package spring-boot:repackage -Dmaven.test.skip=true
The directory structure of the generated JAR files:
XXXX.jar
--org/springframework/boot/loader/...
--META-INF
--resource
--WEB-INF
--jsp/...
--BOOT-INF
--lib/...
--classes/...
At this point, you can run the application as other spring boot applications do:
java -jar XXXX.jar
Access can be made using the following address:
http://localhost:8080/welcome.do
3. Adding JSP to WAR
Modify the pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.ebatech</groupId> <artifactId>SpringBoot-jsp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>SpringBoot-jsp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath /> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
Modify < packaging > jar </packaging > to < packaging > war </packaging >.
Since the generated war is to be run in a web container, the inclusion of spring-boot-starter-tomcat is removed.
Modify app.java to enable it to start and run automatically in the web container
package cn.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication @EnableAutoConfiguration(exclude = { ErrorMvcAutoConfiguration.class }) public class App extends SpringBootServletInitializer { public static void main(String[] args) { ConfigurableApplicationContext ctx = SpringApplication.run(App.class, args); ctx.registerShutdownHook(); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(App.class); } }
Run the following command to hit the war package:
mvn clean package -Dmaven.test.skip=true
Once the war package is ready, it can be put into tomcat's webapp to run.
Reference document
https://spring.io/guides/gs/serving-web-content/
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-static-content
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-jsp-limitations
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file
springboot+jsp jar package upload cloud server