spirng mvc配置文件

来源:互联网 发布:免费网络空间 编辑:程序博客网 时间:2024/06/05 15:12

spring参考手册下载地址:全面、详细,五星推荐,点击下载!

spring mvc的配置文件有两个,一个是web.xml,一个是applicationContext.xml,还有一个是项目名+servlert.xml
下面对这三个文件中的一些常用的配置进行介绍。

一、web.xml
主要配置加载文件、拦截器、过滤器、日志、字符等处理;

<?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>app-ws</display-name>    <!-- 加载spring配置文件,这样可以加载多个符合的条件的xml文件 -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>            classpath*:application/applicationContext*.xml        </param-value>    </context-param>    <!-- log4j日志打印 -->    <context-param>        <param-name>log4jConfigLocation</param-name>        <param-value>classpath:log4j.xml</param-value>    </context-param>    <!--在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,如果不定义,都将会缺省为“webapp.root”,尽量每个项目的参数值不同,以免引起项目冲突-->    <context-param>        <param-name>webAppRootKey</param-name>        <param-value>app-ws</param-value>    </context-param>    <!-- 加载监听 -->    <listener>        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>    </listener>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!-- Spring 请求分发控制器,可以使用默认的,也可以自己定义 extends DispatcherServlet -->    <servlet>        <!--根据serlvet-name名称配置对应的 名称-servlet.xml 文件-->        <servlet-name>cmcf</servlet-name>        <servlet-class>com.cmcf.web.base.servlet.CmcfDispatcherServlet</servlet-class>        <init-param>            <param-name>namespace</param-name>            <param-value>classes/cmcf-servlet</param-value>        </init-param>        <init-param>            <param-name>encoding</param-name>            <param-value>UTF-8</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>cmcf</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <!-- Spring 字符过滤器 -->    <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>    <!-- 用户请求传递过滤器,自己根据是否需要定义  implements Filter -->    <filter>        <filter-name>userTransferForWsFilter</filter-name>        <filter-class>com.cmcf.web.base.filter.UserTransferForWsFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>userTransferForWsFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!-- 以下是静态资源的默认处理,所有的请求进入tomcat,都会流经servlet,如果没有匹配到任何应用指定的servlet,那么就会流到默认的servlet-->    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.html</url-pattern>    </servlet-mapping>    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.js</url-pattern>    </servlet-mapping><!--设置session存活时间,单位是60分钟。如果在java里etMaxInactiveInterval单位为秒-->    <session-config>        <session-timeout>60</session-timeout>    </session-config><!--当用户在浏览器中输入的URL不包含某个servlet名或JSP页面时,welcome-file-list子元素可指定显示的默认文件-->    <welcome-file-list>        <welcome-file>index.html</welcome-file>    </welcome-file-list></web-app>

二、项目名+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:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="        http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-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/aop         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <!-- 自动扫描的包名,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean -->    <context:component-scan base-package="com.cmcf.*" />    <!-- 添加注解驱动   context:component-scan标签是告诉Spring 来扫描指定包下的类,并注册被@Component,@Controller,@Service,@Repository等注解标记的组件。    而mvc:annotation-driven是告知Spring,我们启用注解驱动。然后Spring会自动为我们注册上面说到的几个Bean到工厂中,来处理我们的请求。-->    <mvc:annotation-driven />    <!-- 使用默认的Servlet来响应静态文件。 -->    <!-- DispatcherServlet把找不到的请求映射到默认的servlet,从而实现处理静态资源的请求 -->    <mvc:default-servlet-handler />    <!-- 拦截器 -->    <!-- 配置国际化拦截器,动态改变本地语言 -->    <bean id="localeChangeInterceptor"        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">        <property name="paramName" value="lang" />    </bean>    <bean id="requestMappingHandlerMapping"        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />    <!-- 配置视图解析器 -->    <bean id="jstlViewResolver"        class="org.springframework.web.servlet.view.UrlBasedViewResolver">        <property name="order" value="1" />        <property name="viewClass"            value="org.springframework.web.servlet.view.JstlView" />        <property name="prefix" value="/WEB-INF/views/" />        <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="20480000" />    </bean></beans>

三、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans default-lazy-init="true"    xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:util="http://www.springframework.org/schema/util" 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.1.xsd            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">    <!-- 对properties文件中的值可以使用 $ 符号来引用 -->    <bean id="propertyFileConfigForDubbo"        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="location">            <value>classpath:/properties/dubbo.properties</value>        </property>    </bean>  <!-- 切面aop 配置,一般记录下日志,比如操作时间、操作者id等信息-->  <!--数据库连接池配置,推荐阿里巴巴的druid(可以查看sql性能),传统的还有c3p0和dbcp数据库连接池-->  <!--数据库事务配置--></beans>
1 0