先說明一下,有關SpringMVC相關的資源太多了,這裏僅記錄一下實現過程。同時實現過程中採用了tomcat的服務版本爲2.5。具體過程記錄如下:
1. 整體Springhello程序結構:
2.開始創建Dynamic web project,選擇tomcat dynamic web module version爲2.5(最新爲4.0),然後點擊下一步創建。
3. 想要實現Spring框架應用,就必須要使用Spring相關jar包,基本包括:spring-core、spring-context、spring-web、spring-webmvc、spring-beans、commons-logging等jar包,這些都是可以直接下載的。將這些jar包複製到web-inf裏的lib文件夾下,同時使用buildpath添加到項目中,這樣在referenced libraries裏就有這些包了。
4.項目建完,所需的jar包也準備好後,開始進入SpringMVC的配置。
(1)修改web-inf目錄下的web.xml配置文件,配置前端控制器dispatchservlet
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Springhello</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--配置前端控制器DispatcherServlet -->
<servlet>
<description></description>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name> <!--上下文配置路徑 -->
<param-value>classpath:springmvc.xml</param-value> <!--指向springmvc.xml -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern> <!-- 攔截一切訪問 ,讓指向springmvc.xml文件-->
</servlet-mapping>
</web-app>
(2)前端控制器DispatcherServlet中,在初始化時param-value中設置了classpath,指向的是springmvc.xml。因此第2步就創建springmvc.xml文件,在src目錄點擊右鍵,新建xml文件,保存爲springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置控制器 -->
<context:component-scan base-package="com.handler"></context:component-scan>
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/views/"></property> <!-- 視圖的url前綴 -->
<property name="suffix" value=".jsp"></property> <!-- 視圖文件的後綴 -->
</bean>
</beans>
這個文件中,上部是xml的命名空間,中部開始配置控制器,使用context:component-scan掃描器定位到base-package,也就是控制器所在的包名,這裏的包名爲com.handler。然後配置視圖解析器,指定了視圖文件的url前綴和後綴名。也就是控制器指向視圖爲/views目錄下的jsp文件。
(3)在src目錄下新建一個包com.handler,然後創建一個java類控制器文件。handler、servlet、controller都是控制器的命名。將該類文件命名爲HelloHandler類,這裏使用註解方式將其變成一個控制器類。註解爲@controller就可以將該類修改爲一個控制器類。控制器如何攔截訪問請求呢?這裏使用註解@RequestMapping(參數),這裏的參數就是從前端接收到的請求。同時如果這個類有註解,如果想使用註解功能,就一定要放在springmvc.xml進行掃描,掃描方式爲:<context:component-scan base-package=“註解所在的包名”></context:component-scan>。
package com.handler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller //控制器
public class SpringmvcHandler {
@RequestMapping("welcome") //攔截到前端的welcome請求名
public String Hellowelcome() {
return "success"; //返回success,這是控制器的返回,加上在springmvc.xml配置文件中定義的格式,實際上控制器將會調用/views/success.jsp文件/.
}
}
(4)上述控制器類中,“welcome”這個welcome是來自於前端的http請求,當接收請求後,將執行Hellowelcome,並返回success,這裏返回的是視圖文件,加上之前在springmvc配置文件裏的設置,實際上將顯示視圖文件爲/views/success.jsp。
(5)此時在webcontent目錄下新建views文件夾,並新建一個success.jsp文件,輸入簡單的提示內容如:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
welcome to this page, caojianhua!
</body>
</html>
(6)最後在webcontent目錄下新建的index.jsp文件中加入超鏈接請求:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
this is a simple springmvc project!
<a href="welcome">test mvc</a>
</body>
</html>
上面這個過程是最簡單的入門示例,初步實現了SpringMVC框架的應用。當點擊index.jsp文件的超鏈接時,就向服務器發送了一個get請求,請求名爲welcome,服務器spring dispatcherServlet接受請求,將其分發給定義的控制器,通過@controller標註將普通的類變成控制器後,在使用@RequestMapping註解接收到請求名welcome,然後調用控制器方法Hellowelcome,這裏比較簡單,控制器方法就直接調用視圖success,此時dispatcherServlet根據配置文件設置,調用views/success.jsp文件。