【Java】SpringMVC+JSP部署服務器配置

Spring構建的web項目結構如下:

 

static下放諸如js、img、css、font之類的文件

templates裏放的是404之類的文件(不過我放了貌似沒有效果???)

緊接着創建一個名爲webapp的文件夾,並設置項目structure

 

 然後該文件夾下放jsp文件,如下圖所示。

 

 然後我們要去properties裏面設置一下jsp的前後綴:(前兩項負責找到jsp,後兩項負責解析css、js等文件)

spring.mvc.view.prefix=/page/
spring.mvc.view.suffix=.jsp
# 應該以什麼樣的路徑來訪問靜態資源,這表示只有靜態資源的訪問路徑爲/static/ 時纔會處理(如http://localhost:8080/static/css/base.css)
spring.mvc.static-path-pattern=/**
#用於告訴Spring Boot應該在何處查找靜態資源文件,查找文件時會依賴於配置的先後順序依次進行
spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources

因爲我的webapp下面是page/ 所以設置前綴是/page/

緊接着我們添加jsp支持,在pom文件中加入幾個依賴:(這裏給出的是完整的,畢竟比較常用,注意不要引入thymeleaf,因爲這兩個不能共存)

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.2.5</version>
        </dependency>
        <!--
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- tomcat的支持-->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.2.2.jre8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

這樣直接編譯運行都是沒有問題的,但是打成jar包部署的時候,直接報404

打開jar包發現jsp頁面根本就沒有導入,所以需要配置一下resource目錄。

在pom.xml的<build>中添加<resource>屬性(直接用即可)

<resources>
            <resource>
                <!-- 指定resources插件處理哪個目錄下的資源文件 -->
                <directory>src/main/webapp</directory>
                <!--注意此次必須要放在此目錄下才能被訪問到 -->
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <!-- 將項目中的配置文件,打包至classes下面 -->
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>

然後再次打jar包,然而還是404。。。

我們還需要配置一下打包插件,也是在pom中(即配置主類,並使用1.4.2來打包,只有這個版本支持jsp。。。我也是醉了)

  <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.2.RELEASE</version>
                <configuration>
                    <!--使devtools能夠起作用-->
                    <fork>true</fork>               
  
            <mainClass>com.paul623.javaweb.ex.humanresourcemanagement.HumanresourcemanagementApplication</mainClass> </configuration> </plugin> </plugins>

然後就可以愉快的使用啦~~~

另外提一下,jsp中資源訪問路徑

假如我的font.css文件在resource/static/css/font.css

我的jsp文件在webapp/page/dept/下,則訪問路徑爲../css/font.css

 

可以參考一下這個項目https://github.com/paul623/LibraryManager的配置,花了好久在網上搜尋答案,按照這樣配置絕對不會出問題。

希望能對你有幫助~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章