美文网首页
spring boot 打包jsp

spring boot 打包jsp

作者: 咪雅先森 | 来源:发表于2020-03-04 11:02 被阅读0次

    很久没有写过代界面的东西,突然有需求需要做一个简单的 JSP,然后发现打包后运行访问的时候访问总是404,查了一下解决了这个问题,顺序记录一下。
    先说原因,打包一定要使用 1.4.2.RELEASE 这个打包插件,2.x 的都不行。

    application 配置

    # jsp 配置
    spring.mvc.view.prefix=/WEB-INF/page/
    spring.mvc.view.suffix=.jsp
    

    在 src/main 下建立一个 webapp 目录 跟 resources 平级, 下面建立 webapp/WEB-INF 目录。 测试, 不管是 IDEA 起, 还是java -jar 起, 都可以启动, 只需要 webapp/WEB-INF/page 这三个目录即可,
    不需要 webapp/MATE-INF/resources/WEB-inf/page

    POM 配置 , 注意 反复验证 <resources> 标签是必须的, 否则启动后就是 404 访问不到, 打包插件必须是 1.4.2.RELEASE 这个是个大坑, 搞了好久才搞定, 2.x 以上的版本都不行。

    pom 配置

    <!--jsp-->
    <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>
    <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>
            </plugin>
    
        </plugins>
        <resources>
            <!-- 打包时将jsp文件拷贝到META-INF目录下-->
            <resource>
                <!-- 指定resources插件处理哪个目录下的资源文件 -->
                <directory>src/main/webapp</directory>
                <!--注意此次必须要放在此目录下才能被访问到-->
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
    

    相关文章

      网友评论

          本文标题:spring boot 打包jsp

          本文链接:https://www.haomeiwen.com/subject/qeqmlhtx.html