IDEA 环境下Springboot 集成 Jsp 问题

今天就总结下一直以来遇到的IDEA 环境下springboot 集成jsp (内嵌Tomcat )运行时遇到的问题:

第一个问题:     404 找不到jsp页面

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon May 21 12:21:41 CST 2018
There was an unexpected error (type=Not Found, status=404).

No message available

解决方法:

1. 加入解析jsp 的相关依赖:

<!-- 引入springboot内嵌的tomcat 对jsp的解析-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<!-- 引入servlet  依赖的jar包-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
</dependency>

<!-- 引入jsp  依赖的jar包-->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
</dependency>


<!-- 引入jstl  依赖的jar包-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
加入相关的资源
 
 
<resources>
    <resource> <!-- 将src/main/java下的xml 文件编译到classes下-->
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
    </resource>

    <resource><!-- 将src/main/resources下的所有文件文件编译到classes下-->
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.*</include>
        </includes>
    </resource>

    <resource><!-- 将src/main/webapp下的所有文件文件编译到classes/META-INF/resources下-->
        <directory>src/main/webapp</directory>
        <targetPath>META-INF/resources</targetPath>
        <includes>
            <include>**/*.*</include>
        </includes>
    </resource>
</resources>

2.   点击运行的Main类,选择“Edit Configurations” , 点击左边您的主程序入口类, 在右边的“working directory” 选择$MODULE_DIR$ .




第二个问题: 将项目使用内嵌tomcat 打成JAR包使用java -jar 名称.jar 运行时,运动如下问题



Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon May 21 15:09:33 CST 2018
There was an unexpected error (type=Internal Server Error, status=500).
javax.el.ExpressionFactory.newInstance()Ljavax/el/ExpressionFactory;

   检查了一下,发现javax.el.ExpressionFactory.newInstance()这个方法是EL2.2版本之后才有的方法,而在EL2.1之中是没有这个方法的,问题很明显:org.apache.jasper中试图调用2.2版本的EL,当时提供的EL的版本是2.1版本,所以解决的方式无非就是两个,要不降低org.apache.jasper的版本,要不提升el的版本。

修改pom.xml文件,将

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
     修改为2.2版本,上述问题顺利解决,之前的el异常不再出现。


在运行测试过程中还发现,在IDEA环境下,可以正常访问到JSP页面, 但是使用命令 部署JAR 包之后发现JSP无法正常访问404.

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon May 21 17:26:22 CST 2018
There was an unexpected error (type=Not Found, status=404).
/springboot/index.jsp


原因是编译打包插件的版本问题,经过测试发现,只有1.4.2的版本是可行的。

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <!-- 为打jar包指定版本号-->
        <version>1.4.2.RELEASE</version>
    </plugin>
</plugins>

重新maven  install  再次打包,命令运行 部署JAR 包之后发现JSP仍然无法正常访问404.

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon May 21 18:05:14 CST 2018
There was an unexpected error (type=Not Found, status=404).
No message available


在application.properties 配置文件中添加项目的根路径:

server.servlet.context-path=/springboot

再次打包部署,这次是可以访问到jsp页面了。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值