springboot整合jsp打包踩坑日记

2 篇文章 0 订阅

本文主要分享了对整合jsp的springboot项目打jar包,如何正确的配置maven pom。

一、项目结构

二、打包配置

1.pom.xml添加spring-boot-maven-plugin配置

<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>

如果只是普通的项目打包,上面的配置足够了,但是带src/main/webapp的打包,还不行,会发现src/main/webapp的内容都没有打进jar包

2.pom.xml添加配置resources配置

<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>

这个配置可将src/main/webapp打包到jar包中,但是这个jar包还不能让jsp正常访问

注:如果没有配置<targetPath>META-INF/resources</targetPath>,src/main/webapp的内容会保存到BOOT-INF/ 目录下。经测试,jsp页面无法正常访问,需要指定到META-INF/resources才能正常访问

3.spring-boot-maven-plugin的版本指定为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>

springboot官方推荐的前端模板引擎是thymeleaf,所以对于jsp的支持可能存在兼容性。1.4.2.RELEASE是经过测试出来。具体原因未知,不过确实可用。

如果项目中没有多个main方法,mainClass可以不配置

4.最终版配置

<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>

怎么样?如果你觉得有用的话,还不快快收藏起来!!!

附:涉及的代码目录

gitee:springcloud-template: 一个基于springcloud netflix微服务框架,记录了关于微服务开发的一些最佳应用,欢迎大家学习指导。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值