IDEA中启动SpringBoot + jsp 项目能访问成功,但是打成jar包启动404

版权声明:未经允许不得转载! https://blog.csdn.net/bingxuesiyang/article/details/88398689

官方推荐的SpringBoot+Thyemleaf (如果是新建项目强烈推荐黄金搭档:SpringBoot+Thyemleaf )

背景介绍

但是由于笔者团队中大部分人jsp用的最熟练、还有的是老项目改造用的也是jsp,迫不得已使用的:SpringBoot(版本号 2.1.3.RELEASE) + JSP

如下是笔者的pom配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.school.eution</groupId>
	<artifactId>accommodation</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>accommodation</name>
	<packaging>jar</packaging>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
        <!-- web配置 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

        <!-- 数据库和mybatis配置 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.0</version>
		</dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
			<version>5.1.23</version>
            <scope>runtime</scope>
        </dependency>

        <!-- 热部署配置, 改完直接生效, 不用重启服务 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>

        <!-- bootstrap -->
		<dependency>
			<groupId>org.webjars</groupId>
			<artifactId>bootstrap</artifactId>
			<version>3.3.5</version>
		</dependency>
		<dependency>
			<groupId>org.webjars</groupId>
			<artifactId>jquery</artifactId>
			<version>3.1.1</version>
		</dependency>

        <!-- lombok 自动生成get/set方法和toString()方法 -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>

        <!-- springboot 测试 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

        <!-- jsp支持 -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>1.4.2.RELEASE</version>
			</plugin>
			<!-- 通过maven插件方式, 自动生成dao层相关po类、mapper文件、对应xml文件 -->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.0</version>
				<executions>
					<execution>
						<id>Generate MyBatis Artifacts</id>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

	<!--环境配置-->
	<profiles>
		<!--本地服务器-->
		<profile>
			<id>development</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<properties>
				<profileActive>development</profileActive>
			</properties>
		</profile>

		<!--线上服务器-->
		<profile>
			<id>production</id>
			<activation>
				<activeByDefault>false</activeByDefault>
			</activation>
			<properties>
				<profileActive>production</profileActive>
			</properties>
		</profile>

	</profiles>
</project>

由于支持jsp需要引入以下pom文件,作用就是解析jsp页面的。注意这里没有加<scope>provided</scope> SpringBoot2.0之前版本的看网友说是需要加的,但是2.0之后的版本是不需要的。

   <dependency>
	<groupId>org.apache.tomcat.embed</groupId>
	<artifactId>tomcat-embed-jasper</artifactId>
   </dependency>

添加jstl支持

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

笔者在IDEA中启动时,能正常访问页面,但是打成jar包就不行了

报如下错误:

解决方案(敲黑板划重点):

在往上找了好长时间也没解决问题,笔者突然灵机一动,因为之前pom文件中添加了tomcat-embed-jasper依赖,改写了SpringBoot内嵌的Tomcat,这时就不能使用jar包启动了,而应该打成war包。所以笔者将pom文件的打包方式改成了war包,最终访问成功。

war包的启动方式和jar一样

java -jar XXX.war

再次访问成功!

猜你喜欢

转载自blog.csdn.net/bingxuesiyang/article/details/88398689