然后是 spring的配置了,请在web-lib/下建立applicationContext.xml文件,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
destroy-method="close">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/mystructs</value>
</property>
<property name="username">
<value>chenzhi</value>
</property>
<property name="password">
<value>1</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com\cz\struct\SysTable.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
net.sf.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="systableDAO" class="com.cz.struct.SysTableDAOImp">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="systableDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="systableDAO" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</beans>
在这个配置文件里面,我们设置了datasource\sessionfactory以及spring的动态代理,还有hibernate所需要的XML映射文件路径
然后是配置xwork.xml
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml" />
<package name="default" extends="webwork-default"
externalReferenceResolver="com.atlassian.xwork.ext.SpringServletContextReferenceResolver">
<interceptors>
<interceptor name="reference-resolver"
class="com.opensymphony.xwork.interceptor.ExternalReferencesInterceptor" />
<interceptor-stack name="vxStack">
<interceptor-ref name="params" />
<interceptor-ref name="model-driven" />
<interceptor-ref name="reference-resolver" />
</interceptor-stack>
</interceptors>
<action name="Test" class="com.cz.struct.TestAction">
<external-ref name="systableDAO">systableDAOProxy</external-ref>
<result name="success" type="dispatcher">
<param name="location">/index.jsp</param>
</result>
<interceptor-ref name="vxStack" />
</action>
</package>
</xwork>
在配置action的external-ref的时候我遇到了一个不大不小的玩笑,一般编辑XML的时候我都是在editplus里面编辑的
,最开始我写成
<external-ref name="systableDAO">
systableDAOProxy
</external-ref>
结果系统提示找不到叫systableDAOProxy这个名字的bean,结果一查,发现旁边不能加上空格和tab,哎。。肯定是返回串的时候没加trim
的后果
好了,最后是我们的TestAction.java
package com.cz.struct;
import com.opensymphony.xwork.*;
import com.opensymphony.webwork.*;
import java.util.*;
/**
* @author tijichen
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestAction extends ActionSupport
{
private SystableDAO systabledao;
String testname=null;
public void setSystableDAO(SystableDAO dao)
{
this.systabledao=dao;
}
public SystableDAO getSystableDAO()
{return this.systabledao;}
public String execute() throws Exception {
testname="executeing";
SystablePK pk=new SystablePK();
Systable newdata=new Systable();
pk.setFieldName("test");
pk.setTableName("test3");
newdata.setComp_id(pk);
newdata.setChineseName("tets");
systabledao.insert(newdata);
return Action.SUCCESS;
}
}
ok,编译打包,然后在浏览器地址栏中输入http://localhost:8080/你的发布目录/Test.action
看看,是不是数据库里面多出了一条数据了?
至于其他,比如从页面提交的值插入数据库等等,这里都不多说了,webwork自带的sample就是最好的参考资料
本文地址:http://com.8s8s.com/it/it11768.htm