1.配置web.xmlcss
<?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_receive</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--配置前端控制器DispatcherServlet--> <!-- SpringMVC的前端控制器 --> <!-- 當DispatcherServlet載入後,它將從一個XML文件中載入Spring的應用上下文,該XML文件的名字取決於<servlet-name> --> <!-- 這裏DispatcherServlet將試圖從一個叫作springmvc-servlet.xml的文件中載入應用上下文,其默認位於WEB-INF目錄下 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置字符集 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 處理靜態文件 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> </web-app>
1.1 補充:若是配置前端控制器DispatcherServlet使用以下配置,Spring Web MVC框架將加載 classpath:spring-mvc.xml 來進行初始化上下文,而不是「/WEB-INF/[servlet名字]-servlet.xml」。html
<!--配置前端控制器DispatcherServlet--> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- load-on-startup:表示啓動容器時初始化該Servlet; --> <!-- url-pattern:表示哪些請求交給Spring Web MVC處理, 「/」 是用來定義默認servlet映射的。 也能夠如「*.html」表示攔截全部以html爲擴展名的請求。 -->
1.2 補充 :添加集成Web環境的通用配置:前端
<!-- 加載全部的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name><!-- contextConfigLocation:表示用於加載Bean的配置文件; --> <param-value>classpath:spring-mvc.xml,classpath:spring-hibernate.xml,classpath:spring-beans.xml</param-value> </context-param> <!-- 配置Spring監聽 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
代碼明細(摘抄):java
如上配置是Spring集成Web環境的通用配置;通常用於加載除Web層的Bean(如DAO、Service等),以便於與其餘任何Web框架集成。web
contextConfigLocation:表示用於加載Bean的配置文件;spring
contextClass:表示用於加載Bean的ApplicationContext實現類,默認WebApplicationContext。 spring-mvc
建立完畢後會將該上下文放在ServletContext:mvc
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,this.context);app
ContextLoaderListener初始化的上下文和DispatcherServlet初始化的上下文關係,以下圖框架
從上圖中能夠看出(ContextLoaderListener和DispatcherServlet初始化的區別):
ContextLoaderListener初始化的上下文加載的Bean是對於整個應用程序共享的,無論是使用什麼表現層技術,通常如DAO層、Service層Bean;
DispatcherServlet初始化的上下文加載的Bean是只對Spring Web MVC有效的Bean,如Controller、HandlerMapping、HandlerAdapter等等,該初始化上下文應該只加載Web相關組件。
2.配置springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!--經過註解、掃描Controller--> <!-- springmvc 註解驅動 --> <!--<mvc:annotation-driven/>--> <!-- 掃描器 --> <!--<context:component-scan base-package="com"/>--> <!-- 配置HandlerMapping 根據beanname找到對應的controller --> <!-- <bean class=" org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean> --> <!-- 配置controller --> <!-- <bean name="user" class="com.happy.controller.UserController"></bean> --> <!-- 根據簡單URL 來查找Controller --> <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="userInfo">userController</prop> </props> </property> </bean> <bean id="userController" class="com.happy.controller.UserController"></bean>--> <!-- 控制類的類名訪問Controller ,訪問時類名首字母須要小寫--> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> <bean class="com.happy.controller.UserController"></bean> <!-- 配置視圖解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置前綴和後綴 --> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
3.Controller類
package com.happy.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; /* * 控制器 */ public class UserController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("----訪問到了----"); ModelAndView mav=new ModelAndView("user"); return mav; } }
4.user.jsp頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" 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>Insert title here</title> </head> <body> <a href="user">根據BeanName訪問Controller</a><br><br> <a href="userInfo">根據url訪問Controller</a><br><br> <a href="userController">根據Controller類名訪問Controller</a> </body> </html>