一、spring的jar包下載html
進入http://repo.springsource.org/libs-release-local/,而後依次點擊org/-->springframework-->spring,便可根據須要下載spring框架的壓縮包。前端
二、common-logging的jar包java
spring核心容器依賴common-logging的jar包,下載地址http://commons.apache.org,依次點擊Releases-->Logging,可選擇commons-logging-1.2-bin.zip下載。web
三、springMVC的DispatcherServletspring
DispatcherServlet是一個Servlet,繼承自HttpServlet。它是springMVC的前端控制器,用於攔截請求,與客戶端交互。apache
四、springMVC入門案例spring-mvc
環境搭建:創建dynamic web project,導入springMVC須要的jar包,在web.xml中配置springMVC前端控制器,編寫springMVC.xml核心配置文件,編寫controller。mvc
web.xml中配置springMVC前端控制器app
1 <!-- 配置springMVC前段控制器 --> 2 <servlet> 3 <servlet-name>springMVC</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <init-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>/WEB-INF/springMVC.xml</param-value> 8 </init-param> 9 <!-- Tomcat啓動時加載該servlet --> 10 <load-on-startup>1</load-on-startup> 11 </servlet> 12 <servlet-mapping> 13 <servlet-name>springMVC</servlet-name> 14 <url-pattern>/</url-pattern> 15 </servlet-mapping>
核心配置文件springMVC.xml框架
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 13 <!-- 配置@Controller註解掃描 --> 14 <context:component-scan base-package="com.alphajuns.controller"></context:component-scan> 15 <!-- 配置處理器映射器 --> 16 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> 17 <!-- 配置處理器適配器 --> 18 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> 19 <!-- 配置視圖解析器 --> 20 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> 21 </beans>
controller類
1 package com.alphajuns.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.servlet.ModelAndView; 6 7 @Controller 8 public class HelloController { 9 10 @RequestMapping(value="/hello") 11 public ModelAndView hello() { 12 // 建立模型和視圖對象 13 ModelAndView mv = new ModelAndView(); 14 // 添加模型 15 mv.addObject("message", "Hello SpringMVC!"); 16 // 設置視圖 17 mv.setViewName("/WEB-INF/content/welcome.jsp"); 18 // 返回模型和視圖 19 return mv; 20 } 21 }
頁面的話主要是取值,可用el表達式,如welcome.jsp頁面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 ${requestScope.message } 11 </body> 12 </html>
訪問成功
若是不指定加載配置文件路徑,默認狀況下,加載springMVC配置文件會去WEB-INF文件夾下查找對應的[servlet-name]-servlet.xml。
解析springMVC配置文件會根據配置文件建立一個WebApplicationContext容器對象,WebApplicationContext繼承自ApplicationContext容器。
springMVC建議把全部視圖頁面存放在WEB-INF文件夾下,這樣能夠保護視圖,避免直接向視圖頁面發送請求。