1. 程式人生 > >spring-boot 配置jsp

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>