springmvc 项目web.xml文件配置

来源:互联网 发布:mac如何播放图片 编辑:程序博客网 时间:2024/06/05 21:50
web.xml文件配置
第一种方式:(spring配置文件分开配置)
<!-- 上下文参数(第一启动),指定其他spring配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext-*.xml
</param-value>
</context-param>
<!-- spring监听器(第二启动),监听springMvc环境 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- springMvc编码拦截器(第三启动),springMvc内置的编码拦截器 -->
<filter>
<filter-name>characterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springMvc前置总控制器(第四启动),在分发其它的控制器前都要经过这个总控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 这里必须在springmvc-servlet.xml文件中加入<context:component-scan base-package="***"/>和
<mvc:annotation-driven /> -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:springmvc-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 拦截所有 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 用来把一个Hibernate Session和一次完整的请求过程对应的线程相绑定。目的是为了实现"Open Session in View"的模式 -->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
applicationContext-common.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启注解扫描,可以同时扫码类,属性,方法注解 推荐使用-->
<context:component-scan base-package="test.dao,test.services,test.pojo"></context:component-scan>
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="admin" />
<property name="maxWait" value="60000" />
<property name="initialSize" value="2" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="10" />
<property name="minIdle" value="2" />
<property name="timeBetweenEvictionRunsMillis" value="300000" />
<property name="testOnBorrow" value="false" />
<property name="testWhileIdle" value="true" />
<property name="validationQuery" value="select 1 from dual" />
</bean>

<!-- 配置sessFactory工厂 配置数据源,这里spring中必须要配置一个id="dataSource"的bean -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingLocations">
<list>
<value>classpath:/test/pojo/*.hbm.xml</value>
</list>
</property>
</bean>

<!-- 配置事务管理器,并注入sessionFactory -->
<bean name="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- **************************************配置事务使用注解申明模式 start *********************************** -->
<!-- 声明使用注解式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- **************************************配置事务使用注解申明模式 end ************************************ -->
</beans>

springmvc-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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 启动时扫描所有的controller -->
<context:component-scan base-package="test.action"></context:component-scan>
<!-- 配置mvc 访问静态资源 -->
<mvc:default-servlet-handler/>
<!--配置MVC 访问动态资源
主要作用于@Controller,激活该模式,下面是一种简写形式,完全可以手动配置替代这种简写形式,它会自动注册DefaultAnnotationHandlerMapping与
AnnotationMethodHandlerAdapter,是spring MVC为@Controllers分发请求所必须的 -->
<mvc:annotation-driven />
<!-- 配置js,css等静态文件直接映射到对应的文件夹,不被DispatcherServlet处理,但是测试没效果-->
<!-- <mvc:resources mapping="/*/script/**" location="/script/"/>
<mvc:resources mapping="/*/css/**" location="/css/"/>
<mvc:resources mapping="/*/images/**" location="/images/"/> -->

<!-- jsp页面解析器 -->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/jsp/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 方式一 session域 根据用户本次会话过程中的语言设定决定语言种类(如:用户登录时选择语言种类,则此次登录周期内统一使用此语言设定)-->
<bean name="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
</bean>
<!-- 方式二 cookie域 根据Cookie判定用户的语言设定(Cookie中保存着用户前一次的语言设定参数)-->
<!-- <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieMaxAge" value="604800"/>
<property name="defaultLocale" value="zh_CN"/>
<property name="cookieName" value="Language"></property>
</bean> -->

<!--********************************************* 装配拦截器 方法一 start*****************************************-->
<!--
<mvc:interceptors>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors> -->
<!--********************************************* 装配拦截器 方法一 end*****************************************-->
<!--******************************************** 装配拦截器 方法二 start******************************************-->
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="locale" />
</bean>

<mvc:interceptors>
<!-- 国际化资源切换(根据请求参数中的locale参数自动切换) -->
<mvc:interceptor>
<mvc:mapping path="/*" />
<ref bean="localeChangeInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<!--********************************************* 装配拦截器 方法二 end*****************************************-->
<!-- 国际化 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 国际化信息所在的文件名 -->
<!-- 方法一 -->
<!-- <property name="basename" value="messages" /> -->
<!-- 方法二 -->
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
<property name ="useCodeAsDefaultMessage" value="true"/>
<property name="defaultEncoding" value="UTF-8" />
</bean>
</beans>



第二种方式:(整合到一个spring配置文件中)
<!-- 上下文参数(第一启动),指定其他spring配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/springmvc-servlet.xml
</param-value>
</context-param>
<!-- spring监听器(第二启动),监听springMvc环境 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- springMvc编码拦截器(第三启动),springMvc内置的编码拦截器 -->
<filter>
<filter-name>characterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springMvc前置总控制器(第四启动),在分发其它的控制器前都要经过这个总控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 这里采用应用级的spring配置:
1:配置文件名必须是 springmvc-servlet.xml
2:配置文件必须放在WEB-INF下面
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 拦截所有 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 用来把一个Hibernate Session和一次完整的请求过程对应的线程相绑定。目的是为了实现"Open Session in View"的模式 -->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

springmvc-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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启注解扫描,可以同时扫码类,属性,方法注解 推荐使用-->
<context:component-scan base-package="test"></context:component-scan>
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="admin" />
<property name="maxWait" value="60000" />
<property name="initialSize" value="2" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="10" />
<property name="minIdle" value="2" />
<property name="timeBetweenEvictionRunsMillis" value="300000" />
<property name="testOnBorrow" value="false" />
<property name="testWhileIdle" value="true" />
<property name="validationQuery" value="select 1 from dual" />
</bean>

<!-- 配置sessFactory工厂 配置数据源,这里spring中必须要配置一个id="dataSource"的bean -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingLocations">
<list>
<value>classpath:/test/pojo/*.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置事务管理器,并注入sessionFactory -->
<bean name="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- **************************************配置事务使用注解申明模式 start *********************************** -->
<!-- 声明使用注解式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- **************************************配置事务使用注解申明模式 end ************************************ -->
<!-- 配置mvc 访问静态资源 -->
<mvc:default-servlet-handler/>
<!--配置MVC 访问动态资源
主要作用于@Controller,激活该模式,下面是一种简写形式,完全可以手动配置替代这种简写形式,它会自动注册DefaultAnnotationHandlerMapping与
AnnotationMethodHandlerAdapter,是spring MVC为@Controllers分发请求所必须的 -->
<mvc:annotation-driven />
<!-- 配置js,css等静态文件直接映射到对应的文件夹,不被DispatcherServlet处理 ,但是测试没效果-->
<!-- <mvc:resources mapping="/*/script/**" location="/script/"/>
<mvc:resources mapping="/*/css/**" location="/css/"/>
<mvc:resources mapping="/*/images/**" location="/images/"/> -->

<!-- jsp页面解析器 -->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/jsp/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 方式一 session域 根据用户本次会话过程中的语言设定决定语言种类(如:用户登录时选择语言种类,则此次登录周期内统一使用此语言设定)-->
<bean name="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
</bean>
<!-- 方式二 cookie域 根据Cookie判定用户的语言设定(Cookie中保存着用户前一次的语言设定参数)-->
<!-- <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieMaxAge" value="604800"/>
<property name="defaultLocale" value="zh_CN"/>
<property name="cookieName" value="Language"></property>
</bean> -->

<!--************************************************** 装配拦截器 方法一 start************************************-->
<!--
<mvc:interceptors>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors> -->
<!--************************************************** 装配拦截器 方法一 end************************************-->
<!--************************************************** 装配拦截器 方法二 start************************************-->
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="locale" />
</bean>

<mvc:interceptors>
<!-- 国际化资源切换(根据请求参数中的locale参数自动切换) -->
<mvc:interceptor>
<mvc:mapping path="/*" />
<ref bean="localeChangeInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<!--************************************************** 装配拦截器 方法二 end************************************-->
<!-- 国际化 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 国际化信息所在的文件名 -->
<!-- 方法一 -->
<!-- <property name="basename" value="messages" /> -->
<!-- 方法二 -->
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
<property name ="useCodeAsDefaultMessage" value="true"/>
<property name="defaultEncoding" value="UTF-8" />
</bean>
</beans>

0 0
原创粉丝点击