调试了半天,终于把系统配置了起来,具体环境如下,webwork+spring+hibernate
先看web.xml
xml 代码
- xml version="1.0" encoding="ISO-8859-1"?>
- >
- <web-app>
- <display-name>WebWork 2.0 Example Appdisplay-name>
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value> classpath*:META-INF/applicationContext-*.xml
- **/applicationContext-*.xml
- param-value>
- context-param>
- <context-param>
- <param-name>log4jConfigLocationparam-name>
- <param-value>/WEB-INF/classes/log4j.propertiesparam-value>
- context-param>
- <filter>
- <filter-name>Encodingfilter-name>
- <filter-class>com.koook.util.SetCharacterEncodingFilterfilter-class>
- <init-param>
- <param-name>encodingparam-name>
- <param-value>GBKparam-value>
- init-param>
- filter>
- <filter>
- <filter-name>webworkfilter-name>
- <filter-class>
- com.opensymphony.webwork.dispatcher.FilterDispatcher
- filter-class>
- filter>
- <filter-mapping>
- <filter-name>Encodingfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- <filter-mapping>
- <filter-name>webworkfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>
- listener>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
- <servlet>
- <servlet-name>JspSupportServletservlet-name>
- <servlet-class>com.opensymphony.webwork.views.JspSupportServletservlet-class>
- <load-on-startup>1load-on-startup>
- servlet>
- <welcome-file-list>
- <welcome-file>/WEB-INF/jsp/index.jspwelcome-file>
- welcome-file-list>
- <taglib>
- <taglib-uri>webworktaglib-uri>
- <taglib-location>/WEB-INF/webwork.tldtaglib-location>
- taglib>
- web-app>
这里没什么太多可说的,通过ContextLoaderListener来装载spring的bean,当然在这里我没有用spring缺省的applicationContext.xml,而是通过
applicationContext-hibernate.xml
xml 代码
- xml version="1.0" encoding="UTF-8"?>
- "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="accountSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="mappingResources">
- <list>
- <value>com/koook/account/model/tbl/UKernal.hbm.xmlvalue>
- <value>com/koook/account/model/tbl/UBase.hbm.xmlvalue>
- list>
- property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driverprop>
- <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/usercenterprop>
- <prop key="hibernate.connection.username">rootprop>
- <prop key="hibernate.connection.password">rootprop>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLMyISAMDialectprop>
- <prop key="hibernate.connection.provider">org.hibernate.connection.C3P0ConnectionProviderprop>
- <prop key="hibernate.connection.autocommit">trueprop>
- <prop key="hibernate.show_sql">falseprop>
- <prop key="hibernate.connection.useUnicode">trueprop>
- <prop key="hibernate.connection.characterEncoding">UTF-8prop>
- <prop key="hibernate.c3p0.acquire_increment">1prop>
- <prop key="hibernate.c3p0.idle_test_period">100prop>
- <prop key="hibernate.c3p0.max_size">150prop>
- <prop key="hibernate.c3p0.max_statements">0prop>
- <prop key="hibernate.c3p0.min_size">1prop>
- <prop key="hibernate.c3p0.timeout">100prop>
- <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProviderprop>
- <prop key="hibernate.cache.use_query_cache">trueprop>
- props>
- property>
- bean>
- <bean id="accountTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref bean="accountSessionFactory" />
- property>
- bean>
- <bean id="accountTransactionProxy" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <property name="transactionManager">
- <ref local="accountTransactionManager"/>
- property>
- <property name="transactionAttributes">
- <props>
- <prop key="find*">PROPAGATION_REQUIRED,readOnlyprop>
- <prop key="add*">PROPAGATION_REQUIREDprop>
- <prop key="update*">PROPAGATION_REQUIREDprop>
- <prop key="delete*">PROPAGATION_REQUIREDprop>
- <prop key="*">PROPAGATION_REQUIRED,readOnlyprop>
- props>
- property>
- bean>
- <bean id="userkernalDAO" parent="accountTransactionProxy">
- <property name="target">
- <bean class="com.koook.account.dao.hibernate.UserkernalDAOHibernate">
- <property name="sessionFactory">
- <ref local="accountSessionFactory" />
- property>
- bean>
- property>
- bean>
- beans>
这里面值得注意的是:
- accountTransactionProxy定义这个tranction的时候,用到了lazy-init="true",在调试过程中,如果不加入这个属性,在装载accountTransactionProxy这个bean的时候会出现需要"target"的错误。
- userkernalDAO在定义的时候用到了parent="accountTransactionProxy", 这里主要是为了在配置多个dao或者service用同一个proxy的时候,能尽量少的写配置文件,教程上标准的配置方法是这样的
xml 代码
- xml version="1.0" encoding="UTF-8"?>
- "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="accountTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref bean="accountSessionFactory" />
- property>
- bean>
- <bean id="userkernalDAO" class="com.koook.account.dao.hibernate.UserkernalDAOHibernate">
- <property name="sessionFactory">
- <ref local="accountSessionFactory" />
- property>
- bean>
- <bean id="accountTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <property name="transactionManager">
- <ref local="accountTransactionManager"/>
- property>
- <property name="target">
- <ref local="userkernalDAO" />
- property>
- <property name="transactionAttributes">
- <props>
- <prop key="find*">PROPAGATION_REQUIRED,readOnlyprop>
- <prop key="add*">PROPAGATION_REQUIREDprop>
- <prop key="update*">PROPAGATION_REQUIREDprop>
- <prop key="delete*">PROPAGATION_REQUIREDprop>
- <prop key="*">PROPAGATION_REQUIRED,readOnlyprop>
- props>
- property>
- bean>
- beans>
这样配置一个dao/service就需要一个代理,配置过程将比较繁琐
xwork.xml
xml 代码
- >
- <xwork>
- <include file="webwork-default.xml"/>
- <package name="default" extends="webwork-default">
- <interceptors>
- <interceptor-stack name="WSStack">
- <interceptor-ref name="params">interceptor-ref>
- <interceptor-ref name="model-driven">interceptor-ref>
- interceptor-stack>
- interceptors>
- <action name="login" class="com.koook.photo.action.LoginAction">
- <external-ref name="userkernalDAO">accountTransactionProxyexternal-ref>
- <result name="success" type="dispatcher">
- <param name="location">/main.jspparam>
- result>
- <result name="loginfail" type="dispatcher">
- <param name="location">/index.jspparam>
- result>
- <interceptor-ref name="WSStack" />
- action>
- package>
- xwork>
xml 代码
- <external-ref name="userkernalDAO">accountTransactionProxyexternal-ref>
这里将login这个action中注入userkernalDAO
LoginAction
java 代码
- package com.koook.photo.action;
- import java.util.List;
- import java.util.Map;
- import com.koook.account.dao.UserkernalDAO;
- import com.koook.account.model.Userkernal;
- import com.koook.photo.action.model.LoginInfo;
- import com.opensymphony.xwork.Action;
- import com.opensymphony.xwork.ActionContext;
- import com.opensymphony.xwork.ModelDriven;
- public class LoginAction implements Action,ModelDriven{
- private static String LOGIN_FAIL="loginfail";
- private LoginInfo loginInfo = new LoginInfo();
- private UserkernalDAO ukDao;
- public String execute() throws Exception {
- ... }
- public Object getModel() {
- return loginInfo;
- }
- public UserkernalDAO getUserkernalDAO() {
- return ukDao;
- }
- public void setUserkernalDAO(UserkernalDAO userkernalDao) {
- &nb