Spring 配置DispatcherServlet

1、Spring MVC 教程,快速入门,深刻分析
前端

2、SpringMVC 基础教程 简单入门实例java

SpringMVC实例代码(maven工程)
web

本文描述了web.xml最基本配置方式。spring

Spring MVC的核心是DispatcherServlet,做为Spring MVC的前端控制器;浏览器

和任何Servlet同样,咱们须要在web.xml文件中配置DispatcherServlet;spring-mvc

下面的描述以这个web.xml为例:mvc

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  
    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>  
            /WEB-INF/iSystem-beans.xml,  
        </param-value>  
    </context-param>  
      
    <!-- Creates the Spring Container shared by all Servlets and Filters -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
  
    <!-- Processes application requests -->  
    <servlet>  
        <servlet-name>iSystem</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/iSystem-servlet.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
          
    <servlet-mapping>  
        <servlet-name>iSystem</servlet-name>  
        <url-pattern>/</url-pattern>  
        <url-pattern>*.htm</url-pattern>  
    </servlet-mapping>  
  
</web-app>

在其中的<servlet>标签下,定义了DispatcherServlet。app

注意:在DispatcherServlet载入后,它将从XML文件中载入Spring的应用上下文,这个XML文件的名字取决于Servlet的名字,或者由init-param来指定。jsp

若是咱们不设置init-param,形如:maven

<servlet>  
    <servlet-name>iSystem</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>

这时,DispatcherServlet载入后将默认去加载  /WEB-INF/iSystem-servlet.xml文件,iSystem就是上面定义的servlet-name。

若是没有这个文件,将会报错:

java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/iSystem-servlet.xml]

所以咱们若是省略init-param的话,须要按下图放置该xml:


附上模板工程自带的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: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-3.0.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
  
   <mvc:resources mapping="/resources/**" location="/resources/" /> 
  	<!-- 此处的name的做用就是:http://localhost:8080/项目名/welcome  而MytestController是个人一个拦截器-->
	<bean name="/welcome" class="com.unis.ucsm.control.MytestController"></bean>
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->  
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <beans:property name="prefix" value="/WEB-INF/views/" />  
        <beans:property name="suffix" value=".jsp" />  
    </beans:bean>  
      
    <context:component-scan base-package="com.codeevoship.app" />  
</beans:beans>

接下来再为DispatcherServlet指定须要处理的URL:

<servlet-mapping>  
    <servlet-name>iSystem</servlet-name>  
    <url-pattern>/</url-pattern>  
    <url-pattern>*.htm</url-pattern>  
</servlet-mapping>

固然这里还能够加上*.png、*.gif等等等等……

拦截地址:

  <url-pattern>/</url-pattern>:这种方式拦截了相似/user/login的地址;会致使静态资源没法访问。

  <url-pattern>/*</url-pattern>:错误的拦截方式,能够走到Action中,但转发到jsp时再次被拦截,不能访问到jsp。

  <url-pattern>/path/*</url-pattern>:拦截包含/path/路径的请求。

  <url-pattern>*.do</url-pattern>:简单,实用的拦截方式,不存在静态资源访问的问题。

到这里DispatcherServlet的配置就完成了,这里再说说其余内容。

虽然咱们已经加载了iSystem-servlet.xml,虽然能够把全项目全部的bean全都定义在这一个xml中……

推荐分层、分模块分别定义,好比iSystem-security.xml/iSystem-data.xml/iSystem-service.xml……

这就用到了上下文载入器,最经常使用的ContextLoaderListener:

<!-- Creates the Spring Container shared by all Servlets and Filters -->  
<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>

这个载入器能够载入除DispatcherServlet载入的配置文件以外的其余上下文配置文件,经过下述方式:

<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>  
        /WEB-INF/iSystem-security.xml,  
        /WEB-INF/iSystem-data.xml,  
    </param-value>  
</context-param>

param-value中就是须要加载的文件,以逗号分隔。

以上就是web.xml的最基本配置。

建立拦截器类MytestController:

package com.unis.ucsm.control;

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 MytestController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("拦截器被调用!");
		return new ModelAndView("test");
	}
}

若是咱们在浏览器里面访问

http://localhost:8080/项目名/welcome;固然我这里服务的是http://localhost:8080/ucsm/welcome

这时候就会访问/WEB-INF/views/文件夹下面的test.jsp文件(固然这个文件是你事先就建立好的)。

为何会反问这个文件而不是别的文件呢,由于你在前面设置下面这个:

     <beans:property name="prefix" value="/WEB-INF/views/" />  
        <beans:property name="suffix" value=".jsp" />

这个的意思是一旦你的拦截器返回的ModelAndView(String str)中的类容(个人ModelAndView("test"))会告诉web容器要访问的页面是

<beans:property name="prefix" value="prefixvlaue" /> +str+<beans:property name="suffix" value="suffixvalue" />而我上面配置prefixvlaue=“/WEB-INF/views/”   str="test" suffixvalue=“.jsp”因此加在一块儿就是访问/WEB-INF/views/test.jsp

相关文章
相关标签/搜索