日期:2014-05-20  浏览次数:20735 次

关于Struts2+Spring2.5整合问题讨论
环境jdk1.5+tomcat6
网上看了很多文章 例如一下几篇
http://www.blogjava.net/191499719/archive/2008/06/12/207355.html
http://hpfyeah.javaeye.com/blog/57066
http://www.javaeye.com/topic/33387

可能因为版本不同的原因 大致方法上差不多 但是细节差别上很大 小弟对一些问题实在有点疑惑 请各位大侠指教
1.struts.objectFactory = spring 到底要不要设置 我看到很多文章都说要设置 但是我跑起来为什么刚好是没有设置的那次
2.bean的自动装配属性到底应该怎么设置 我不明白整合struts2的时候应该是使用哪种自动装配方式 或者不用
3.如果我采用 http://kaituofuture.javaeye.com/blog/294913 的 “2 使用自动装配 struts.xml配置:与不整合时配置一样。applicationContext.xml配置:业务逻辑组件的id与action中业务逻辑组件属性同名 ”来整合Struts2和Spring要怎样调试才能知道Spring接管Struts2的Bean装配了
4.我是刚开始是先调试Spring的 然后再第一次整合Struts功能的时候 老是在AccountAction中调用accountDAOImpl的时候报NULL异常

Java code

import com.opensymphony.xwork2.ActionSupport;

public class AccountAction extends ActionSupport {
    
    private AccountDAOImpl accountDAOImpl;
    
    public AccountAction(AccountDAOImpl accountDAOImpl)
    {
        this.accountDAOImpl=accountDAOImpl;
    }

    public String execute() throws Exception
    {
        accountDAOImpl.createAccount(0,0);
        return "ok";
    }

}


Java code

import java.util.HashMap;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class AccountDAOImpl extends SqlMapClientDaoSupport implements AccountDAO
    //implements AccountDAO
{
    
    public void createAccount(final int cid,final double balance) throws DataAccessException{    
        Account account=new Account();
        account.setCid(cid);
        account.setBalance(balance);
        getSqlMapClientTemplate().insert("insertAccount", account);        
    }
    
    public void transferAccount(final int toid,final double amount) throws DataAccessException{
        
    }
    
    public void savingMoney(final double amount) throws DataAccessException{
        
    }
    
    public void withdrawMoney(final double amount) throws DataAccessException{
        
    } 

}


XML code

    <bean id="accountDao" class="cn.jacocc.ssitry.AccountDAOImpl">
        <property name="sqlMapClient" ref="sqlMapClient"/>
    </bean>
    
    <bean id="accountAction" class="cn.jacocc.ssitry.AccountAction">
        <constructor-arg index="0">
            <ref bean="accountDao" />
        </constructor-arg>
    </bean>
    




大概代码就是这样



------解决方案--------------------
1。spring插件通过覆盖struts2的objectfactory来工作,不设置能跑起来?
不过这个覆盖用不着你来做啊,struts-plugin.xml里就有,在struts2-plugin-2.0.11.jar里
2.struts.objectFactory.spring.autoWire=auto,如果发现默认构造器,则用type方式,否则constructor
3,4待高人
------解决方案--------------------
不用这么复杂,在struts.xml配置的action的class指定为Spring的Bean id就交给spring管理了(这样就集成了)。
struts.xml:
Java code

<package name="article"  extends="struts-default">
    <global-results>
         <result name="message">/message.jsp</result>
     </global-results>
     <action name="article_*" class="articleAction" method="{1}">
        <result name="list">/article_list.jsp</result>
        <result name="listAction" type="redirect-action">article_list.do</result>
        <result name="add">/article_add.jsp</result>
    </action>
</package>