虽然是简单的用户登录,但东西一点不少,基于MVC原理实现,共分DAO层,SERVICE层,ACTION层和WEB层,其中DAO和SERVICE层都有各自的接口。
今天主要讲解配置文件的代码,我学习实例,喜欢从控制层出发,然后用到了哪些类或者JSP,再一一扯“蛋”扯出来。
当然,还是先看web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <!-- Spring ApplicationContext配置文件的路径�,可使用通配符,多个路径用�1,号分隔,此参数用于后面的Spring-Context loader -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:spring/*.xml</param-value>
- </context-param>
- <!-- 著名 Character Encoding filter -->
- <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>
- </filter>
- <!--Hibernate Open Session in View Filter-->
- <filter>
- <filter-name>hibernateFilter</filter-name>
- <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
- </filter>
- <!-- ExtremeTable 导出Excel和Pdf的Filter -->
- <filter>
- <filter-name>eXtremeExport</filter-name>
- <filter-class>org.extremecomponents.table.filter.ExportFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>*.do</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>*.jsp</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>hibernateFilter</filter-name>
- <url-pattern>*.do</url-pattern>
- </filter-mapping>
- <!--Spring ApplicationContext 载入 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- Spring 刷新Introspector防止内存泄露 -->
- <listener>
- <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
- </listener>
- <!-- session超时定义,单位为分钟 -->
- <session-config>
- <session-timeout>10</session-timeout>
- </session-config>
- </web-app>
东西很简单,无非是配置一些过滤器呀,监听器的。主要讲一下openSessionInViewFilter吧,假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter或者OpenSessionInViewInterceptor。session会在transaction结束后关闭,此时会抛出session is close 的异常。关于这方面的知识,值得大家去找一下相关资料仔细阅读。
strut2.xml
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts >
- <include file ="struts-default.xml"/>
- <package name ="default" extends ="struts-default">
- <action name="login" method="login" class="userAction">
- <result>/login_success.jspresult>
- <result name="input">/login.jspresult>
- action>
- package>
- struts>
可能注意到了,这里的Action交给SPRING来管理了。所以我们看一下application.xml的代码吧
- xml version="1.0" encoding="UTF-8"?>
- >
- <beans>
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
- <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
- <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:oracleDB" />
- <property name="user" value="xxx" />
- <property name="password" value="xxx" />
- <property name="minPoolSize" value="3" />
- <property name="maxPoolSize" value="30" />
- <property name="maxIdleTime" value="1800" />
- <property name="acquireIncrement" value="3" />
- <property name="maxStatements" value="0" />
- <property name="initialPoolSize" value="3" />
- <property name="idleConnectionTestPeriod" value="60" />
- <property name="acquireRetryAttempts" value="30" />
- <property name="breakAfterAcquireFailure" value="true" />
- <property name="testConnectionOnCheckout" value="false" />
- bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialectprop>
- <prop key="hibernate.show_sql">trueprop>
- <prop key="hibernate.generate_statistics">trueprop>
- <prop key="hibernate.connection.release_mode">autoprop>
- <prop key="hibernate.autoReconnect">trueprop>
- props>
- property>
- <property name="mappingDirectoryLocations">
- <list>
- <value>
- classpath:com/caitong/pingou/bean
- value>
- list>
- property>
- bean>
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref bean="sessionFactory"/>
- property>
- bean>
- <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <property name="transactionManager" ref="transactionManager"/>
- <property name="transactionAttributes">
- <props>
- <prop key="add*">PROPAGATION_REQUIREDprop>
- <prop key="find*">PROPAGATION_REQUIRED,readOnlyprop>
- props>
- property>
- bean>
- <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames">
- <value>*Servicevalue>
- property>
- <property name="interceptorNames">
- <list>
- <value>transactionInterceptorvalue>
- list>
- property>
- bean>
- <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
- <property name="transactionInterceptor" ref="transactionInterceptor"/>
- bean>
- <bean id="baseDAO" class="com.caitong.pingou.dao.impl.BaseDAO" abstract="true">
- <property name="sessionFactory">
- <ref bean="sessionFactory"/>
- property>
- bean>
- <bean id="userDAO"
- class="com.caitong.pingou.dao.impl.UserDAO" parent="baseDAO">
- bean>
- <bean id="userService" class="com.caitong.pingou.service.impl.UserService"
- autowire="byName">
- bean>
- <bean id="userAction" class="com.caitong.pingou.action.UserAction"
- autowire="byName">
- bean>
- beans>
应该说SPRING太强大了,以至于一个配置文件可以解决任何一件事情。简单介绍一下这个配置文件吧,例子用的是c3p0的数据库链接池,hibernate的配置文件也都集成在这里了,如果细心的读者,可能注意到了事务管理模块。是的,本例的事务管理是由spring来管理,而且集中在service层
- <property name="beanNames">
- <value>*Servicevalue>
- property>
有人可能提出问题,为什么非得要放在service层,而不是dao层,应该说,事务管理有一个不成文的规定,尽量将问题放在上层处理。
然后每个类由SPRING来管理,并且autowire="byName"来寻找依赖注入的bean。
所有的xml文件都已经配置完了,其实最重要也是这个,XML文件将是框架的一个趋势,掌握了它,其实你已经打开了这个框架的门。