以前SSH用的struts1做的开发 现在需要用struts2 现在有2个问题
1、struts2 和 spring整合 action创建的问题 一个action处理多个动作的问题(struts1中的dispatchAction)怎么实现
2、在spring配置文件中加入hibernate.cfg.xml文件 会报错no matching editors or conversion strategy found我的spring配置文件如下:<?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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 配置datasource -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/user"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置sessionFactory method=2 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" value="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/lusha/user/pojo/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean> <!-- 配置sessionFactory method=1
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
<!-- 设置不需要实现接口来代理
<property name="proxyTargetClass">
<value>true</value>
</property>
-->
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/> </tx:attributes>
</tx:advice> <!-- 那些类的哪些方法参与事务 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* cn.lusha.user.dao.*.*(..))"/>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>
</beans>
1、struts2 和 spring整合 action创建的问题 一个action处理多个动作的问题(struts1中的dispatchAction)怎么实现
2、在spring配置文件中加入hibernate.cfg.xml文件 会报错no matching editors or conversion strategy found我的spring配置文件如下:<?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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 配置datasource -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/user"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置sessionFactory method=2 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" value="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/lusha/user/pojo/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean> <!-- 配置sessionFactory method=1
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
<!-- 设置不需要实现接口来代理
<property name="proxyTargetClass">
<value>true</value>
</property>
-->
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/> </tx:attributes>
</tx:advice> <!-- 那些类的哪些方法参与事务 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* cn.lusha.user.dao.*.*(..))"/>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>
</beans>
public class BussinessAction { public String test1() throws Exception
{
return SUCCESS;
}
public String test2() throws Exception
{
return SUCCESS;
}这就定义了2个方法,我们接下来所做的就是在struts2的配置文件中进行定义:<action name="test1" class="BussinessAction" method="test1">
<result name="success">index.jsp</result>
</action>
<action name="test2" class="BussinessAction" method="test2">
<result name="success">index.jsp</result>
</action> method 就是要执行的action类中方法名,class需要时一个类路径,如果没有在spring中定义,需要写完整的路径,如com.BussinessAction
1、struts2和spring整合 是想弄明白 spring怎么来创建和管理struts2的action
比如struts1 是在struts-config.xml文件中修改type属性 spring配置文件中用name属性关联2、我把hibernate配置文件删除后 只用spring的配置文件来创建 就会报错 no matching editors or conversion strategy found
如果原始方式 保留hibernate配置文件 用spring来读取classpath下的hibernate配置文件就没问题
{
return SUCCESS;
}
public String test2() throws Exception
{
return SUCCESS;
}struts.xml<action name="test" class="BussinessAction" method="test1">
<result name="success">index.jsp</result>
</action>
以前在页面访问action的时候是test.action
把它改成这样就可以了test!方法名.action 比如<s:form action="test!test1.action">
这样可以减少xml文件的配置量这个你要把struts2的action交给spring去创建的话。有两个办法
1.在src下新建一个struts.properties文件里面一句话struts.objectFactory = spring
2.在struts.xml加上一句<constant name="struts.objectFactory" value="spring" />