1. 程式人生 > >Step by Step 搭建SpringMVC專案[3]-配置SpringMVC環境

Step by Step 搭建SpringMVC專案[3]-配置SpringMVC環境

裝飾基礎專案

 Web專案轉Maven專案

       想要使用Maven管理我們的專案,就需要將我們的專案轉換一下。當然也可以直接下載jar包放到專案內。不過為了多學習一些就不要省事兒了。

    右鍵專案--》Configurae--》Convert to Maven Project

      

    這裡做一些解釋:

        groupid和artifactId被統稱為“座標”是為了保證專案唯一性而提出的,如果你要把你專案弄到maven本地倉庫去,你想要找到你的專案就必須根據這兩個id去查詢。

        groupId一般分為多個段,這裡我只說兩段,第一段為域,第二段為公司名稱。域又分為org、com、cn等等許多,其中org為非營利組織,com為商業組織。舉個apache公司的tomcat專案例子:這個專案的groupId是org.apache,它的域是org(因為tomcat是非營利專案),公司名稱是apache,artigactId是tomcat。


       artifactId設定為dream,表示你這個專案的名稱是dream

       建議例如包名為org.apache.springmvc .

            那麼groupId改寫成類似 org.apache 專案包名,artifactId 為專案名springmvc

        類似這樣的寫法。不過都是根據個人習慣。

    

    專案轉換完成後,會多出如上圖檔案。暫時不用管,進行下一步,在我們的src/main下建立webapp資料夾。並將WebContent資料夾內的META-INF與WEB-INF檔案複製到該目錄。

    

    刪除WebContent資料夾。右鍵專案--》Properties--》Deployment Assembly

    

      選中/WebContent點選Remove將其移除

      點選Add… 選擇Folder

    

    Next,展開資料夾列表,找到webapp

    

    選中webapp資料夾,點選Finish。

    

    新增完成,Apply and Close。

    Run as--》Run on Server

    釋出專案到服務。

    

    發現沒毛病,完美執行。

    下面使用Maven下載我們的SpringMVC相關類庫。

    開啟pom.xml內容如下。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>pers.wtt</groupId>
  <artifactId>dream</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

    我們所有Maven都在這裡新增。首先我們到maven倉庫找到。    SpringMVC庫

    這裡我下載的是 4.3.14版本。

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.14.RELEASE</version>
</dependency>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>pers.wtt</groupId>
  <artifactId>dream</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
	 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>4.3.14.RELEASE</version>
	</dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

    新增<dependencies></dependencies>標籤,將程式碼複製到這個標籤之間。

    滑鼠右鍵--》Run as --》 Maven install 等待安裝完成即可。

    配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>dream</display-name>
  
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>/WEB-INF/index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

    右鍵專案 Run as --》Run on Server

     配置完成,事跑兩步試試。

    

    哎呀拋了異常了

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/springmvc-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/springmvc-servlet.xml]

    這一看是沒有找到 /WEB-INF/springmvc-servlet.xml配置檔案

    那就建立一個

    

    既然建立了配置檔案,那就順便配置一下。

    到spring官網檢視文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- ... -->

</beans>

先補全 springmvc-servlet.xml檔案,然後重新跑一下看看效果。

很好這次沒有異常出現了。接下來開始配置我們的springmvc-servlet.xml。