tomcat  报的异常:Hibernate: insert into demo.t_file (filename, re, filecontent) values (?, ?, ?)
[ERROR]http-8080-Processor25-org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/demo].[action]-Servlet.service() for servlet action threw exception
java.lang.IllegalStateException: Active Spring transaction synchronization or active JTA transaction with specified [javax.transaction.TransactionManager] required
at org.springframework.jdbc.support.lob.LobCreatorUtils.registerTransactionSynchronization(LobCreatorUtils.java:79)
at org.springframework.orm.hibernate3.support.AbstractLobType.nullSafeSet(AbstractLobType.java:185)
at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:141)
        
applicationContext  :
<?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="dataSource"
     class="org.apache.commons.dbcp.BasicDataSource"
     destroy-method="close">
     <property name="driverClassName">
        <value>org.gjt.mm.mysql.Driver</value>
     </property>
     <property name="url">
        <value>jdbc:mysql://localhost:3306/demo?useUnicode=true&amp;characterEncoding=gb2312</value>
     </property>
     <property name="username">
        <value>root</value>
     </property>
     <property name="password">
        <value>root</value>
     </property>
</bean>
<bean id="lobHandler" lazy-init="true"  class="org.springframework.jdbc.support.lob.DefaultLobHandler">
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
       <ref local="dataSource"/>
    </property>
    
    <property name="lobHandler"> 
          <ref bean="lobHandler"/>        
    </property> 
    <property name="mappingResources">
      <list>
        <value>com/demo/hibernate/beans/User.hbm.xml</value>
        <value>com/demo/hibernate/beans/Address.hbm.xml</value>
        <value>com/demo/hibernate/beans/Admin.hbm.xml</value>
        <value>com/demo/hibernate/beans/TFile.hbm.xml</value>
      </list>
    </property>
           
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">
           org.hibernate.dialect.MySQLDialect
        </prop>
        <prop key="hibernate.show_sql">true</prop>
      </props>
    </property>
</bean><bean id="transactionManager"
     class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory">
        <ref local="sessionFactory"/>
     </property>
     <property name="nestedTransactionAllowed"> <value>true</value> </property>
     
</bean>
 
<bean id="tfileDAO" class="com.demo.hibernate.dao.TFileDAO">
     <property name="sessionFactory">
        <ref local="sessionFactory"/>
     </property>
</bean>
<bean id="tfileDAOProxy"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
       <ref bean="transactionManager"/>
    </property>
    <property name="target">
       <ref local="tfileDAO"/>
    </property>
    <property name="transactionAttributes">
      <props>
        <prop key="insert *">PROPAGATION_REQUIRED</prop>
        <prop key="save*" >PROPAGATION_REQUIRED</prop>
        <prop key="*">PROPAGATION_REQUIRED</prop>
      </props>
    </property>
</bean><bean name="/upload"
   class="com.demo.struts.actions.TFileAction" scope="prototype">
  <property name="tfileDAO">
    <ref local="tfileDAO"/>
  </property>
</bean>
</beans>struts-config:
<form-bean name="fileForm" type="com.demo.struts.forms.FileActionForm"></form-bean>
<action path="/upload"
               type="org.springframework.web.struts.DelegatingActionProxy"
               name="fileForm"
               scope="request"                              
       >
    <forward name="success" path="/upload.jsp"></forward>action:
public class TFileAction extends DispatchAction {  private TFileDAO tfileDAO;
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception{
        
        FileActionForm faf=(FileActionForm)form;
        
        TFile tfile =new TFile();
        
        tfile.setRe(faf.getRe());
        tfile.setFilecontent(faf.getMyfile().getFileData());
        tfile.setFilename(faf.getMyfile().getFileName());
        
        System.out.println("***"+faf.getMyfile().getFileData().length);
        
       tfileDAO.save(tfile);
        
        return mapping.findForward("success");
    }
public TFileDAO getTfileDAO() {
return tfileDAO;
}
public void setTfileDAO(TFileDAO tfileDAO) {
this.tfileDAO = tfileDAO;
}
}TFile.hbm.xml:
<?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="com.demo.hibernate.beans.TFile" table="t_file" catalog="demo">
        <id name="fileid" type="java.lang.Integer">
            <column name="fileid" />
            <generator class="native"></generator>
        </id>
        <property name="filename" type="java.lang.String">
            <column name="filename" not-null="true" />
        </property>
        <property name="re" type="java.lang.String">
            <column name="re" />
        </property>
        <property name="filecontent" type="org.springframework.orm.hibernate3.support.BlobByteArrayType">
            <column name="filecontent" />
        </property>
    </class>
</hibernate-mapping>请各位高手帮帮忙,感激不尽。

解决方案 »

  1.   

    jsp页面呢?最好直接用Fromfile这个类,方便一点
      

  2.   

    upload.jsp:
    就这一个页面
    <%@ page contentType="text/html;charset=gbk"%>
    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
    <%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested" %>
    <html>
    <head>  
    <title><bean:message key="login.page.title"/></title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    </head>
      <body> 
      
        <form action="upload.do" method="post" enctype="multipart/form-data">
         文件说明:<input type="text" name="re"/><br>
        文件:<input type="file" name="myfile"  /><br>
        <input type="submit" value="保存"/>
    </form> 
      </body>
    </html>
      

  3.   

    private String fileName;
    public void setMyFileFileName(String fileName)  {
             this .fileName = fileName;
        } 你实体类中有没有这样设置 因为
    <input type="file" name="myfile"  /> <br>标志不仅仅是绑定到myFile,还有myFileContentType(上传文件的MIME类型)和myFileFileName(上传文件的文件名,该文件名不包括文件的路径)。因此<input type="file" name="myfile"  />对应Action类里面的xxx、xxxContentType和xxxFileName三个属性。