springboot-jsp jar problem

[Previous Summary] A recent project was built with the springboot+jsp structure, but when publishing a production environment, it was necessary to use maven as a jar package, but the default configuration at the beginning was not successful.The following article is the specific solution process.

1. Project structure

Base, abnormal phenomenon

The JDK used is 1.8 and the springboot version is:

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

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
</properties>

The resulting jar contains only class files, not resource files.

3. Solutions

1. Add mapping of resource paths

<resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
                <!-- Turn on filtering and replace the parameters in the file under directory with the specified parameters-->
                <filtering>false</filtering>
            </resource>

            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>

            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>
                        **/*.java
                    </exclude>
                </excludes>
            </resource>

        </resources>

2. Modify the maven compiled version to 1.4.2

Only jar packages with this version can resolve jsp

3. Set mainClass

<plugins>
            <plugin>
                <!-- maven Plug-in unit -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.2.RELEASE</version>
                <configuration>
                    <mainClass>com.gt.LaysshApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>

4. Add View Configuration (optional)

spring.mvc.view.prefix=/WEB-INF/jsp/

spring.mvc.view.suffix=.jsp

A more complete maven compilation configuration is given below

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
                <!-- Turn on filtering and replace the parameters in the file under directory with the specified parameters-->
                <filtering>false</filtering>
            </resource>

            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>

            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>
                        **/*.java
                    </exclude>
                </excludes>
            </resource>

        </resources>
        <plugins>
            <plugin>
                <!-- maven Plug-in unit -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.2.RELEASE</version>
                <configuration>
                    <mainClass>com.gt.MyApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

Here are the results of modifying the compilation configuration

[Write later] There are so many modern template parsing engines. Why not try thymeleaf, but in a recent project you encountered a problem with the th:src tag that doesn't parse. It's not clear why, and the same writing works on other pages. That's strange.Given that many people still use springboot+jsp for development, the SpringBoot project created with IDEA tools does not have a webapp directory.If we want to add a webapp directory, we can add it manually.Here is a simple description of the configuration process.

1. Open Project Structure Management and click ProjectStructure in the upper right corner of IDEA

2. Click on the + sign in the picture below before clicking on the Web

3. Modify Configuration

The following figure is the default configuration before modifying the configuration

The following configures the webapp under the traditional main directory


Tags: Programming Maven Java JSP Spring

Posted on Sat, 10 Aug 2019 21:34:54 -0700 by eddjc