spring-boot 配置jsp

sring-boot 集成  jspjava

  spring-boot默認使用的頁面展現並非jsp,若想要在項目中使用jsp還須要配置一番。web

  雖然spring-boot中也默認配置了InternalResourceViewResolver,可是這個視圖解析器並無解析jsp的功能,它只是把解析工做交給容器。而容器中又是調用JspServlet進行jsp解析的,全部這裏咱們須要引入JspServlet所在的jar包( tomcat-embed-jasper-xxx.jar)。一般和jsp配合使用的還有jstl.jar,和standar.jarspring

  

        <!-- 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配置。apache

          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-mvc

 

  然而當咱們運行程序的時候,spring-boot並不會把咱們建立的webapp下的文件打包進去,咱們還須要再maven中配置一下項目的路徑,只有這樣spring-boot纔會把咱們建立的文件夾打包進去,這樣咱們能夠順利的訪問到jsp了。tomcat

        <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>        
相關文章
相關標籤/搜索