Spring 配置DispatcherServlet

一、Spring MVC 教程,快速入门,深入分析

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

SpringMVC实例代码(maven工程)

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

Spring MVC的核心是DispatcherServlet,作为Spring MVC的前端控制器;

和任何Servlet一样,我们需要在web.xml文件中配置DispatcherServlet;

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

<?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。

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

如果我们不设置init-param,形如:

<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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值