1. 程式人生 > >【SpringMvc】從零開始學SpringMvc之初始化(一)

【SpringMvc】從零開始學SpringMvc之初始化(一)

大家好,我們今天開始SpringMvc 這個系列,由於筆者也是安卓出身,對SpringMvc 也是接觸不久,所以,這個系列僅僅只是記錄筆者學習SpringMvc 過程中的心得,如有錯誤,歡迎指正。

在開始之前,我們需要準備一些東西,JDK、Eclipse(MyEclipse)、Tomcat、Mysql、Navicat(或者類似軟體),這些軟體的安裝教程網上很多,這裡就不一一詳述了。

1.建立一個SpringMvc Dynamic Project,如下圖,記得勾選 建立web.xml

image.png
image.png

2.下載SpringMvc 需要的jar 包,下載地址,一般選擇最新版,然後將jar 包複製到 WebContent/WEB-INF/lib 資料夾下,如下圖

image.png

3.在src 下建立springmvc-servlet.xml 檔案,並加入springmvc 的基本配置,這裡包名用的是com.test,你也可以修改為你自己的包名

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"
>
<!-- scan the package and the sub package --> <context:component-scan base-package="com.test" /> <!-- configure the InternalResourceViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 字首 --> <property name="prefix" value="/WEB-INF/html/" /> <!-- 字尾 --> <property name="suffix" value=".html" /> </bean> </beans>

org.springframework.web.servlet.view.InternalResourceViewResolver 作用是在Controller返回的時候進行解析檢視

4.在src 中建立applicationContext.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"
	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">
	<!--6 容器自動掃描IOC元件 -->
	<context:component-scan base-package="com.test"></context:component-scan>


</beans>

5.修改web.xml,加入引入剛才建立的配置檔案,注意applicationContext.xml和springmvc-servlet.xml的引用路徑

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>SpringMvc</display-name>

	<!-- 配置spring核心監聽器,預設會以 /WEB-INF/applicationContext.xml作為配置檔案 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- contextConfigLocation引數用來指定Spring的配置檔案 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>


	<!--configure the setting of springmvcDispatcherServlet and configure the 
		mapping -->
	<servlet>
		<servlet-name>SpringMvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc-servlet.xml</param-value>
		</init-param>
		<!-- <load-on-startup>1</load-on-startup> -->
	</servlet>

	<servlet-mapping>
		<servlet-name>SpringMvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

6.在src 中 建立com.test 包,需要注意,這裡需要和上一步的包名保持一致;然後建立IndexController

@Controller
@RequestMapping("")
public class IndexController {

	@RequestMapping("")
	public String hello() {
		return "index";
	}

}

@Controller 註解 用於標記在一個類上,使用它標記的類就是一個SpringMVC Controller 物件。分發處理器將會掃描使用了該註解的類的方法。通俗來說,被Controller標記的類就是一個控制器,這個類中的方法,就是相應的動作。

@RequestMapping是一個用來處理請求地址對映的註解,可用於類或方法上。用於類上,表示類中的所有響應請求的方法都是以該地址作為父路徑。空字串代表忽略該路徑,所以,hello 方法的完整路徑應該為 http://localhost:8080/SpringMvc/

7.因為 在第3步時,我們配置InternalResourceViewResolver 的字首是WEB-INF/html ,字尾是html,所以我們需要在WebContent/WEB-INF 資料夾下,建立html 資料夾,並建立index.html,只需要在body 中新增一句話即可

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>Hello SpringMvc</h1>

</body>
</html>

8. 到這裡,大功告成了,讓我們執行下專案,然後在瀏覽器中輸入http://localhost:8080/SpringMvc/,就可以看到我們的剛才寫的頁面了。

總結 SpringMvc的配置還是比較多的,能變化的就只有配置檔案的路徑和名字

最後獻上原始碼Github

你的認可,是我堅持更新部落格的動力,如果覺得有用,就請點個贊,謝謝