Hello World XML完整版
2017年自己寫了一些技術文章的總結,我想在2018年給自己繼續寫下去的動力!
這個系列也是對2017年SpringMVC過程的一個總結,希望能堅持下去!
技術和工具說明:
- Spring 5.0.2.RELEASE
- Maven 3
- JDK 1.8
- Eclipse Oxygen Release (4.7.0)
- Tomcat/Jetty
如果大家對Maven不熟悉,請先簡單瞭解一下Maven,方便後面的學習.
1.新建Maven Web專案
- 建立Maven專案

- 點選Next

- 選擇webapp專案

- 新增骨架的內容

2.構建Maven專案,沒有建立src/main/java資料夾
eclipse中建maven專案,建完專案之後結果不能建立src/main/java資料夾,經網上查詢資料得以解決

選擇Java Bulid Path 下的Source資料夾,將紅色X選項移除,在重新建立


3.設定Web專案支援Servlet3.0
因為我們將使用Tomcat7,而新構建的專案的web.xml預設情況下是2.3,我們需要重新設定
-
選中專案右鍵properties->Project Facets
無法修改該項內容,下面會顯示 Cannot Change Version of project Facet Dynamic Web Module to 3.0錯誤,我們需要修改該專案下的配置檔案

將2.3修改為3.0,原因如下我使用的是Tomcat7,只能支援到3.0
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="1.8"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<!-- <installed facet="jst.web" version="2.3"/> -->
<installed facet="jst.web" version="3.0"/>
</faceted-project>
複製程式碼
修改完畢之後,需要將原來的web.xml檔案刪除掉
**如何新建web.xml呢?**操作如下
-
將Dynamic Web Module的核取方塊去掉點選應用
-
再次勾選Dynamic Web Module核取方塊,下面出現了一個超連結點選
-
注意設定web.xml的位置
3.配置pom.xml配置SpringMVC環境
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hanpang.springmvc</groupId>
<artifactId>springmvc-helloword</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvc-helloword Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 設定公共的版本號 -->
<properties>
<springframework.version>5.0.2.RELEASE</springframework.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<build>
<finalName>springmvc-helloword</finalName>
<!-- 設定編譯級別 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 這裡配置埠號和訪問路徑 -->
<port>8001</port>
<path>/mvc</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
複製程式碼
如果過程中下載失敗,請各位找到倉庫刪除重新下載即可
4.配置DispatcherServlet前端控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
複製程式碼
使用預設的核心配置檔名稱,在WEB-INF/[servlet-name]-servlet.xml , 因此,在WEB-INF資料夾下新建mvc-servlet.xml檔案
(1)WEB-INF右鍵新建選擇Other

(2)選擇Spring的核心配置檔案,點選Next

(3)新建預設的springmvc的核心配置檔案 mvc-servlet.xml , 注意點選下一步

(4)勾選內容如下

下面XSD的版本都選擇4.3的版本
(5)mvc-servlet.xml配置核心檔案程式碼如下
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 1.啟動SpringMVC註解 -->
<mvc:annotation-driven/>
<!-- 2.掃描註解 -->
<context:component-scan base-package="com.hanpang.**.web"/>
</beans>
複製程式碼
5.新建一個普通的Java類(控制器)
package com.hanpang.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller//告知其是一個控制器
@RequestMapping(value="/sys")
public class IndexController {
@RequestMapping(path="/index")
public ModelAndView test01() {
System.out.println("Hello World");
return null;
}
}
複製程式碼
6.使用tomcat7-maven-plugin外掛啟動伺服器
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
複製程式碼
請參考pom.xml中的配置
設定啟動Tomcat的命令


使用Maven Build 啟動專案,Goals那一欄填寫:
-
如果是應用在tomcat6外掛上:
tomcat6:run -Dmaven.tomcat.uriEncoding=UTF-8 -Dmaven.tomcat.path=/ -Dmaven.tomcat.port=8080 複製程式碼
-
如果是應用在tomcat7外掛上:
tomcat7:run -Dmaven.tomcat.uriEncoding=UTF-8 -Dmaven.tomcat.path=/ -Dmaven.tomcat.port=8080 複製程式碼
7.瀏覽器訪問工程執行

