spring-boot 配置jsp

sring-boot 集成  jsp

  spring-boot默认使用的页面展示并不是jsp,若想要在项目中使用jsp还需要配置一番。

  虽然spring-boot中也默认配置了InternalResourceViewResolver,但是这个视图解析器并没有解析jsp的功能,它只是把解析工作交给容器。而容器中又是调用JspServlet进行jsp解析的,所有这里我们需要引入JspServlet所在的jar包( tomcat-embed-jasper-xxx.jar)。通常和jsp配合使用的还有jstl.jar,和standar.jar

  

        <!-- jsp 解析 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!-- jstl标签库 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- 标签库引入 -->
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
        </dependency>

 

  

  这时虽然spring-boot可以解析jsp了但是他还需要知道怎么去找到jsp,所以这里还需要配置一下jsp查找路径,即我们熟悉的spring-mvc中的prefix和suffix配置。

          spring.mvc.view.prefix=/WEB-INF/views/
          spring.mvc.view.suffix=.jsp

  而spring-boot中默认是没有/WEB-INF文件夹的所以我们需要自己创建。我们需要在/src/main目录下创建一个和/java,/resources平级的目录/webapp。然后再webapp下面创建WEB-INF/views。我们把jsp放入views中,这样spring-boot就可以顺利的查找到jsp了。

  然而当我们运行程序的时候,spring-boot并不会把我们创建的webapp下的文件打包进去,我们还需要再maven中配置一下项目的路径,只有这样spring-boot才会把我们创建的文件夹打包进去,这样我们可以顺利的访问到jsp了。

        <resources>
            <resource>
                <directory>src/main/webapp</directory>
                <!--注意此次必须要放在此目录下才能被访问到 -->
                    <targetPath>/</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>        

猜你喜欢

转载自www.cnblogs.com/monkeydai/p/10061585.html