在SpringBoot项目中使用JSP

在SpringBoot项目中使用JSP标签

##引子

最近接手公司内部一个比较老的项目,使用的很久以前的技术选型,其中就包括大量的JSP页面。

在维护过程中,当习惯了springboot的便捷性之后,很难再去接受老项目的配置--大量的xml文件,大量的properties配置文件。 甚至,项目中的不同环境切换还采用了手动注释的方式,每次发测试环境/生产环境,都要去修改大量的配置文件,所以就萌生了将项目最小化的去优化一些,尽可能的兼容老代码的前提下,引入更便捷的引入方式。

当然,这里只是简单的记录了在重构过程中使用springboot项目兼容JSP页面的过程。

SpringBoot支持JSP,但不建议使用

spring官方文档中对于JSP的支持有这么一段描述:

27.4.5 JSP Limitations
When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.
Undertow does not support JSPs.
Creating a custom error.jsp page does not override the default view for error handling. Custom error pages should be used instead.
There is a JSP sample so that you can see how to set things up.

大致意思就是springboot虽然可以支持JSP页面,但是有一定的限制:

    1. 请将项目打成war包,jar包不支持JSP。
    2. 使用Undertow不支持JSP。
    3. 对于error.jsp需要手动去处理。 同时springboot 提供了一个简单的JSP支持示例.

引入依赖

 <!-- step1: 打包成war包 -->
    <packaging>war</packaging>


    <!-- step2: 添加依赖 -->
    <dependencyManagement>
        <dependencies>
            <!-- spring boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- Compile -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- Provided -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- Test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- step3: 配置插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>

编写Start启动类

对于这个starter类没有特殊要求,标准的springboot启动类即可。

@SpringBootApplication
public class WebStarter extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(WebStarter.class, args);
    }

}

编写application.yml文件

主要实在配置文件中,对spring mvc进行配置

  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp

针对页面编写对应的Controller

结束,部署到tomcat下。

END

其实添加JSP支持相对还是很容易的,记录一下,避免下次踩坑。

转载于:https://my.oschina.net/u/3101282/blog/2245836

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值