<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="mysql"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="1"></property>
</bean> <!-- 声明一个处理句柄 //-->
<bean id="lobHandler"
class="org.springframework.jdbc.support.lob.DefaultLobHandler"
lazy-init="true" />
<bean id="sf"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="mysql" />
</property>
<!-- 为处理Blob类型字段的句柄声明 //-->
<property name="lobHandler" ref="lobHandler" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>dao/Up.hbm.xml</value>
</list>
</property>
</bean>
<!-- DAO 的IoC配置//-->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sf" />
</bean> <bean id="UpDAO" class="dao.UpDAO">
<property name="hibernateTemplate" ref="hibernateTemplate" />
<property name="sessionFactory">
<ref bean="sf" />
</property>
</bean>
<bean name="/up" class="com.yourcompany.struts.action.UpAction">
<property name="dao" ref="UpDAO"></property>
</bean>
<!-- 定义事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="mysql"></property>
</bean> <!-- 定义事务管理策略 -->
<bean id="transactionAttributeSource"
class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义事务代理,声明事务管理 -->
<bean name="dao1"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target">
<ref bean="UpDAO" />
</property>
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource" />
</property>
</bean></beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="mysql"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="1"></property>
</bean> <!-- 声明一个处理句柄 //-->
<bean id="lobHandler"
class="org.springframework.jdbc.support.lob.DefaultLobHandler"
lazy-init="true" />
<bean id="sf"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="mysql" />
</property>
<!-- 为处理Blob类型字段的句柄声明 //-->
<property name="lobHandler" ref="lobHandler" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>dao/Up.hbm.xml</value>
</list>
</property>
</bean>
<!-- DAO 的IoC配置//-->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sf" />
</bean> <bean id="UpDAO" class="dao.UpDAO">
<property name="hibernateTemplate" ref="hibernateTemplate" />
<property name="sessionFactory">
<ref bean="sf" />
</property>
</bean>
<bean name="/up" class="com.yourcompany.struts.action.UpAction">
<property name="dao" ref="UpDAO"></property>
</bean>
<!-- 定义事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="mysql"></property>
</bean> <!-- 定义事务管理策略 -->
<bean id="transactionAttributeSource"
class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义事务代理,声明事务管理 -->
<bean name="dao1"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target">
<ref bean="UpDAO" />
</property>
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource" />
</property>
</bean></beans>
解决方案 »
- 散分,超级Easy的问题
- Hibernate中用Annotations代替的VO的xml文件,这样有啥好处吼~~
- 帮我翻译Servlet接口的init()方法
- tomcat 找不到java.library.path
- 各们大人帮个忙
- 基于STRUTS的亮点何在
- java.lang.NoClassDefFoundError: Could not initialize class com.aw.dao.HibernateU
- 你觉得使用Struts有必要考虑缓存的问题吗?
- 急问,如何在程序中用dtd验证xml,在线等!
- 怎么会事啊?
- Session问题 请 近来帮帮忙
- 在一个类中封装的native方法,外部无法访问吗?
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.ModuleException;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;import com.yourcompany.struts.form.UpForm;import dao.Up;
import dao.UpDAO;public class UpAction extends DispatchAction { private UpDAO dao; public void setDao(UpDAO dao) {
this.dao = dao;
} public UpDAO getDao() {
return dao;
} public ActionForward upload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UpForm upForm = (UpForm) form; Up up = new Up(); up.setId(3); try {
up.setFilecontent(upForm.getFilecontent().getFileData());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} up.setFileName("aa");
System.out.println("aa");
this.dao.save(up);
System.out.println("����ɹ�����������������������������"); return mapping.findForward("Rs");
} public ActionForward listAllFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// UpForm upForm = (UpForm) form; List list = this.dao.LoadAll(); request.setAttribute("list", list); return mapping.findForward("down");
} public ActionForward download(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws ModuleException, IOException { UpForm upForm = (UpForm) form; Up up = new Up(); up = this.dao.findById(upForm.getId()); String fileName = up.getFileName(); response.setContentType("application/x-msdownload");
try {
response.setHeader("Content-Disposition", "attachment;"
+ " filename="
+ new String(fileName.getBytes(), "ISO-8859-1"));
response.getOutputStream().write(up.getFilecontent());
response.getOutputStream().flush();
OutputStream os = response.getOutputStream();
os.write(up.getFilecontent());
os.flush();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return null;
} public void getD() { WebApplicationContext wac = WebApplicationContextUtils
.getWebApplicationContext(this.getServlet().getServletContext()); dao = (UpDAO) wac.getBean("UpDAO");
}}
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.form;import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.apache.struts.validator.ValidatorForm;/**
* MyEclipse Struts
* Creation date: 06-28-2008
*
* XDoclet definition:
* @struts.form name="upForm"
*/
public class UpForm extends ValidatorForm { private FormFile filecontent;
private String name;
private String fileName; private int id; public FormFile getFilecontent() {
return filecontent;
} public void setFilecontent(FormFile filecontent) {
this.filecontent = filecontent;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
} /**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
}
}
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class UpDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(UpDAO.class); // property constants
public static final String FILECONTENT = "filecontent"; protected void initDao() {
// do nothing
} public void save(Up transientInstance) {
//log.debug("saving Up instance");
try {
getHibernateTemplate().save(transientInstance);
//log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
} public void delete(Up persistentInstance) {
log.debug("deleting Up instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
} public Up findById(java.lang.Integer id) {
log.debug("getting Up instance with id: " + id);
try {
Up instance = (Up) getHibernateTemplate().get("dao.Up", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
} public List findByExample(Up instance) {
log.debug("finding Up instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
} public List findByProperty(String propertyName, Object value) {
log.debug("finding Up instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Up as model where model." + propertyName
+ "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
} public List findByFilecontent(Object filecontent) {
return findByProperty(FILECONTENT, filecontent);
} public List findAll() {
log.debug("finding all Up instances");
try {
String queryString = "from Up";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
} public Up merge(Up detachedInstance) {
log.debug("merging Up instance");
try {
Up result = (Up) getHibernateTemplate().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} public void attachDirty(Up instance) {
log.debug("attaching dirty Up instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public void attachClean(Up instance) {
log.debug("attaching clean Up instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public static UpDAO getFromApplicationContext(ApplicationContext ctx) {
return (UpDAO) ctx.getBean("UpDAO");
} public List LoadAll() { return this.getHibernateTemplate().loadAll(Up.class);
}
}<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="dao.Up" table="up" >
<id name="id">
<column name="id" />
<generator class="increment" />
</id>
<property name="fileName">
<column name="fileName"></column>
</property>
<property name="filecontent"
type="org.springframework.orm.hibernate3.support.BlobByteArrayType"
lazy="true">
<column name="filecontent" />
</property>
</class>
</hibernate-mapping>
package dao;/**
* Up generated by MyEclipse Persistence Tools
*/public class Up implements java.io.Serializable { private Integer id; private byte[] filecontent; private String fileName; public byte[] getFilecontent() {
return filecontent;
} public void setFilecontent(byte[] filecontent) {
this.filecontent = filecontent;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
}}
spring2.0出这么久了,利用pointcut的事务配置比这个简单多了,楼主为什么不用呢,我给你改了一下,你试试行不行
<?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: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-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="mysql"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/test"> </property>
<property name="username" value="root"> </property>
<property name="password" value="1"> </property>
</bean>
<bean id="lobhandler"
class="org.springframework.jdbc.support.lob.DefaultLobHandler"
lazy-init="true">
</bean>
<bean id="sf"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="mysql" />
</property>
<!-- 为处理Blob类型字段的句柄声明 //-->
<property name="lobHandler" ref="lobHandler" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true </prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>dao/Up.hbm.xml </value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sf" />
</property>
</bean>
<tx:advice id="txAdvice">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="fooServiceOperation"
expression="execution(* dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="fooServiceOperation" />
</aop:config> <bean id="UpDAO" class="dao.UpDAO">
<property name="sessionFactory" ref="sf"></property>
</bean> <bean name="/up" class="com.yourcompany.struts.action.UpAction">
<property name="dao" ref="UpDAO"> </property>
</bean>
</beans>