作爲Spring MVC入門,現以XML配置的方式爲例。
實例文件結構:
首先配置Web工程的web.xml文件,代碼如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置Spring IoC配置文件路徑-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 配置ContextLoaderListener用以初始化Spring IoC容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置DispatcherServlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Servlet 攔截配置-->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
論述一下這個文件的配置內容:
- 系統變量contextConfigLocation的配置,他會告訴Spring MVC其Spring IoC的配置文件在哪裏,這樣Spring就會找到這些配置文件去加載它們。如果是多個配置文件,可以使用逗號將他們分割開來,並且它還支持正則式匹配,進行模糊匹配,這樣就更加靈活了,其默認值是/WEB-INF/applicationContext.xml。
- ContextLoaderListener實現了接口ServletContextListener,通過Java Web容器的學習,我們知道ServletContextListener的作用是可以在整個Web工程前後加入自定義代碼,所以可以在Web工程初始化之前,他先完成對Spring IoC容器的初始化,也可以在Web工程關閉之時完成Spring IoC容器的資源進行釋放。
- 配置DispatcherServlet,首先式配置了servlet-name爲dispatcher,這就意味着需要一個/WEB-INF/dispatcher-servlet.xml文件(注意,servlet-name和文件名的對應關係)與之對應,並且我們配置了在服務器啓動期間就初始化它。
- 配置DispatcherServlet攔截以 後綴“do"結束的請求,這樣所有以後綴”do“結尾的請求都會被它攔截。
由於這是最簡單的入門實例,applicationContext.xml就不需要配置任何內容了,代碼如下:
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
這樣Spring IoC容器就沒有裝載自己的類,根據之前的論述,他還會加載一個/WEB-INF/dispatch-servlet.xml文件,它是與Spring MVC配置相關的內容,代碼如下:
dispatch-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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 使用註解驅動-->
<mvc:annotation-driven />
<!-- 定義掃描裝載的包-->
<context:component-scan base-package="com.*" />
<!-- 定義視圖解析器-->
<!-- 找到Web工程/WEB-INF/JSP文件夾,且文件結尾爲jsp的文件作爲映射-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>
這裏的配置也比較簡單:
- <mvc:annotation-driven />表示使用註解驅動Spring MVC
- 定義一個掃描的包,用它來掃描對應的包,用以加載對應的控制器和其它的一些組件。
- 定義是視圖解析器,解析器中定義了前綴和後綴,這樣視圖就知道去Web工程的/WEB-INF/jsp文件夾中找到對應的JSP文件作爲視圖響應用戶請求。
控制器配置了註解驅動,並且定義了掃描的包,那我們就可以編寫一個簡單的Controller,代碼如下:
myController.java
package com.ssm.example1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
//註解@Controller表示他是一個控制器
@Controller("myController")
//表明當請求的URL在/my下的時候纔有該控制器響應
@RequestMapping("/my")
public class MyController {
//表明URL是/index的時候該方法才請求
@RequestMapping("/index")
public ModelAndView index(){
//模型和視圖
ModelAndView mv=new ModelAndView();
//試圖邏輯名稱爲index
mv.setViewName("index");
//返回模型和視圖
return mv;
}
}
由於我們已經配置了前綴/WEB-INF/jsp,後綴是jsp,再加上返回的視圖邏輯名稱爲index。所以我們會選擇使用/WEN-INF/jsp/index.jsp作爲最後的響應。
index.jsp
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Welcome to Spring Web MVC project</title>
</head>
<body>
<h1>Hello,Spring MVC</h1>
</body>
</html>
最後啓動服務器Tomcat,輸入對應的URL,這裏是http://localhost:8080/my/index.do,就能看到對應的響應了。