SpringMVC富功能配置
1、第一件事当然是jar包了,导入如下jar包,当然有用不上的,也可以去熟悉下,毕竟在有些项目上可以用到;如果你用maven的话,也可以参考一下以下的jar名称和版本:







2、配置web.xml 文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="organization" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Daemon</display-name> <!-- 配置contextConfigLocation(名字不能改,spring内部名称) --> <context-param> <param-name>contextConfigLocation</param-name> <!-- contextConfigLocation所引入的xml文件 --> <param-value>classpath*:spring/*/application*.xml</param-value> </context-param> <!-- 配置context加载listener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置内部清理listener --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- 配置在线用户listener 实现方式具体情况具体分析,在这就不贴出来了--> <listener> <listener-class>org.zywx.common.online.OnLineUser</listener-class> </listener> <!-- 配置全局编码过滤器 characterEncodingFilter(名称不能改)--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <!-- 将编码设置为utf-8 --> <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> <!-- springsecurity是在过滤器中配置的 --> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <!-- 过滤所有的请求 /* --> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/*/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- session有效时间30min --> <session-config> <session-timeout>30</session-timeout> </session-config> <!-- 常见的MIME类型: 超文本标记语言文本 .htm,.html text/html 普通文本 .txt text/plain RTF文本 .rtf application/rtf GIF图形 .gif image/gif JPEG图形 .ipeg,.jpg image/jpeg au声音文件 .au audio/basic MIDI音乐文件 mid,.midi audio/midi,audio/x-midi RealAudio音乐文件 .ra, .ram audio/x-pn-realaudio MPEG文件 .mpg,.mpeg video/mpeg AVI文件 .avi video/x-msvideo GZIP文件 .gz application/x-gzip TAR文件 .tar application/x-tar 防止文件在浏览器中直接打开 --> <mime-mapping> <extension>txt</extension> <mime-type>text/plain</mime-type> </mime-mapping> <!-- 登录表单验证方式 Base64 --> <login-config> <auth-method>BASIC</auth-method> </login-config> <!-- 欢迎页配置 --> <welcome-file-list> <welcome-file>/login.jsp</welcome-file> </welcome-file-list> <!-- 404、500页配置 --> <error-page> <error-code>404</error-code> <location>/NotFound.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/NotFound.jsp</location> </error-page> </web-app>
3、数据库配置applicationContext-db.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:util="http://www.springframework.org/schema/util"
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/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<util:properties id="propVal" location="classpath:init.properties"></util:properties>
<bean id="lobHandler" lazy-init="true" autowire="default" class="org.springframework.jdbc.support.lob.DefaultLobHandler">
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- 下边的取值来自init.properties 现将其读取 -->
<value>classpath:init.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" abstract="false">
<property name="driverClassName">
<value>${database.driverClassName}</value>
</property>
<property name="url">
<value>${database.url}</value>
</property>
<property name="username">
<value>${database.username}</value>
</property>
<property name="password">
<value>${database.password}</value>
</property>
<property name="initialSize">
<value>${database.initialSize}</value>
</property>
<property name="maxActive">
<value>${database.maxActive}</value>
</property>
<property name="maxWait">
<value>${database.maxWait}</value>
</property>
<property name="maxIdle">
<value>${database.maxIdle}</value>
</property>
<property name="removeAbandoned">
<value>${database.removeAbandoned}</value>
</property>
<property name="removeAbandonedTimeout">
<value>${database.removeAbandonedTimeout}</value>
</property>
<property name="logAbandoned">
<value>${database.logAbandoned}</value>
</property>
<!-- 下面两个参数后添加,当取得连接对象的时候,进行验证 -->
<property name="testOnBorrow" value="true"/>
<property name="validationQuery" value="${database.sql}"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="lobHandler">
<ref local="lobHandler" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${database.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="hibernate.max_fetch_depth">5000</prop>
<prop key="hibernate.jdbc.batch_size">500</prop>
<prop key="hibernate.query.substitutions"> true = 1 false = 0 </prop>
<prop key="hibernate.bytecode.use_reflection_optimizer"> true </prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider </prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="javax.persistence.validation.mode">none</prop>
<prop key="hibernate.autoReconnect">true</prop><!-- 新加 -->
</props>
</property>
<!-- hibernate 对象数据库映射扫描;实体类所在包,因为是注解,没有hbm.xml文件-->
<property name="packagesToScan" value="org.zywx" />
</bean>
<!-- Hibernate 事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 用注解来实现事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="hibernateGenericDao" class="org.zywx.organization.dao.HibernateGenericDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
4、spring安全配置applicationContext-security.xml;使用的安全机制是有spring security提供的:
..............太多了 不想写了
5、现在配置最熟悉不过的applicationContext.xml 使用注解,配置文件内容就是这么少:
。。。。。
6、还有一个spring-mvc.xml文件,配置一些异常处理的bean和返回数据格式统一:
。。。。。
我出个word吧,这编辑器太水,复制个东西还限制长度。。。。
SpringMVC富功能配置.docx:http://pan.baidu.com/s/1c0k75M4

浙公网安备 33010602011771号