Springboot integrates jsp packaging and stepping on the pit diary

This article mainly shares how to correctly configure the maven pom for the jar package of the springboot project that integrates jsp.

1. Project structure

2. Packaging configuration

1.pom.xml add spring-boot-maven-plugin configuration

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<executions>
				<execution>
					<goals>
						<goal>repackage</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

If it is just an ordinary project package, the above configuration is enough, but the package with src/main/webapp is not enough, you will find that the content of src/main/webapp is not included in the jar package

2.pom.xml add configuration resources configuration

<build>
    <resources>
        <resource>
            <directory>src/main/webapp</directory>
            <!-- 注意此次必须要放在此目录下才能被访问到 -->
            <targetPath>META-INF/resources</targetPath>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

This configuration can package src/main/webapp into a jar package, but this jar package cannot allow jsp to access normally

Note: If <targetPath>META-INF/resources</targetPath> is not configured, the content of src/main/webapp will be saved in the BOOT-INF/ directory. After testing, the jsp page cannot be accessed normally, and it needs to be specified in META-INF/resources to access normally

3. The version of spring-boot-maven-plugin is specified as 1.4.2.RELEASE

​<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- 一定要是这个版本,其他版本访问不到页面 -->
            <version>1.4.2.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- 1.4.2.RELEASE导致多个main方法的情况下需要指定主类 -->
                <mainClass>com.company.admin.AdminApplication</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

The front-end template engine officially recommended by springboot is thymeleaf, so there may be compatibility for jsp support. 1.4.2.RELEASE is tested. The exact reason is unknown, but it does work.

If there are no multiple main methods in the project, mainClass may not be configured

4. Final version configuration

<build>
	<resources>
		<resource>
			<directory>src/main/webapp</directory>
			<!-- 注意此次必须要放在此目录下才能被访问到 -->
			<targetPath>META-INF/resources</targetPath>
			<filtering>false</filtering>
		</resource>
		<resource>
			<directory>src/main/resources</directory>
			<filtering>true</filtering>
		</resource>
	</resources>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<!-- 一定要是这个版本,其他版本访问不到页面 -->
			<version>1.4.2.RELEASE</version>
			<executions>
				<execution>
					<goals>
						<goal>repackage</goal>
					</goals>
				</execution>
			</executions>
			<configuration>
				<!-- 1.4.2.RELEASE导致多个main方法的情况下需要指定主类 -->
				<mainClass>com.company.admin.AdminApplication</mainClass>
			</configuration>
		</plugin>
	</plugins>
</build>

How about it? If you find it useful, don't hesitate to collect it! ! !

Attachment: the code directory involved

gitee: springcloud-template: A microservice framework based on springcloud netflix, which records some of the best applications for microservice development. Welcome to learn and guide.

Guess you like

Origin blog.csdn.net/w13528476101/article/details/127468115