您的位置:首页 > 编程语言 > Java开发

SpringMVC拦截配置

2015-09-02 15:41 471 查看
1.SpringMVC拦截器的配置

<span style="font-family:Microsoft YaHei;"><!-- Spring MVC配置 -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/bdqn/resources/spring/spring-mvc.xml</param-value>
</init-param>
<!-- 是启动顺序,1表示启动容器时初始化该Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<!-- /是拦截所有请求,*.do表示拦截所有以.do结尾的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping></span>
2.“ / ”和“ *.do "或其它的区别

”/“表示拦截所有请求,包括静态文件,如js文件,css文件,图片文件等,这时,要想加载静态文件,需要在springmvc配置文件(如:srpingMVC-servlet.xml)中进行配置
,配置如下,粗体部分:

<span style="font-family:Microsoft YaHei;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">

<!--注解扫描包,主要扫描的是controller -->
<context:component-scan base-package="com.bdqn.controller"/>

<!-- 添加注解驱动(开启注解) -->
<mvc:annotation-driven/>

<strong><!-- 静态资源的访问 -->
<mvc:resources location="/jsPlugins/" mapping="/jsPlugins/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/scripts/" mapping="/scripts/**"/>
<mvc:resources location="/styles/" mapping="/styles/**"/>
<mvc:resources location="/seip/" mapping="/seip/**"/>
<mvc:resources location="/upload/" mapping="/upload/**"/></strong>

<!-- 定义视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- 上传文件配置 、多了个多请求的处理、目的是为了支持多文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
</bean>

<!-- 用来处理json格式转换 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</beans></span>

2.”*.do “则表示拦截器只会拦截以”*.do“结尾的请求,不会拦截静态文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: