日期:2014-05-19  浏览次数:20845 次

关于ssh Struts2.1 + Spring3.0 + Hibernate3.3的问题
以前用Struts1.1 Spring2.0 Hibernate3的时候, ApplicationContext是如下配置的.

ApplicationContext.xml


  <bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=">
</property>
<property name="username" value="sa"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/xxx/entity/User.hbm.xml</value>
</list>
</property>
</bean>

<bean id="userDao" class="com.xxx.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean name="/user" class="com.xxx.struts.action.UserAction">
<property name="userDao" ref="userDao" />
</bean>

<!-- <bean id="userDao" class="com.xxx.dao.impl.UserDaoImpl"-->
<!-- autowire="byName" />-->
<!-- <bean name="/user" class="com.xxx.struts.action.UserAction"-->
<!-- autowire="byName" />-->






struts-config.xml

<struts-config>
<data-sources />
<form-beans>
<form-bean name="userForm" type="com.xxx.struts.form.UserForm" />

</form-beans>

<global-exceptions />
<global-forwards />
<action-mappings>
<action attribute="userForm" name="userForm" parameter="type"
path="/user" scope="request"
type="org.springframework.web.struts.DelegatingActionProxy" validate="false">
<forward name="toShow" path="/show.jsp" />
<forward name="toLogin" path="/login.jsp" />
<forward name="toEdit" path="/edit.jsp" />
<forward name="toAdd" path="/add.jsp" />
</action>

</action-mappings>

<message-resources parameter="com.xxx.struts.ApplicationResources" />
</struts-config>


在ApplicationContext中 放了Dao.在Action中 定义一个userDao给它Set的方法,就可以用了.
现在换了以后.不知道 怎么在ApplicationContext中放Dao,用以前的肯定是不行了.会报空指针.有谁会,请说下,感谢万分!




------解决方案--------------------
这个有点复杂,struts1与struts2有很大的区别,
告诉你步骤吧:
1、在struts.xml中加 <constant name="struts.objectFactory" value="spring"/>
2、在struts.xml中加ACTION配置:
<action name="user" method="调用执行的方法" class="userAction">
 3、在ApplicationContext.xml文件中配置
<bean id="userAction" class="com.xxx.struts.action.UserAction&quo