大家好,我們今天開始SpringMvc 這個系列,由於筆者也是安卓出身,對SpringMvc 也是接觸不久,所以,這個系列僅僅只是記錄筆者學習SpringMvc 過程中的心得,如有錯誤,歡迎指正。
在開始之前,我們需要準備一些東西,JDK、Eclipse(MyEclipse)、Tomcat、Mysql、Navicat(或者類似軟件),這些軟件的安裝教程網上很多,這裏就不一一詳述了。
1.創建一個SpringMvc Dynamic Project,如下圖,記得勾選 創建web.xml
2.下載SpringMvc 需要的jar 包,下載地址,一般選擇最新版,然後將jar 包複製到 WebContent/WEB-INF/lib 文件夾下,如下圖
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>