读书人

struts2和spring3整合项目action无法

发布时间: 2013-01-25 15:55:29 作者: rapoo

struts2和spring3整合项目,action无法调用业务逻辑组件的方法
本帖最后由 papaxiong7658 于 2012-12-01 11:42:19 编辑 开发一个SSH2项目,在一个页面使用一个<s:action>load一个图书类别,后台的action调用业务逻辑组件的方法总是不成功,很奇怪。项目其他的部分都运行的好好的。为了检查,把无关的部分都去掉。只留了一些相关的部分,代码如下,大家帮我看看是哪里写错了?
以下代码完全是为了检查和测试这个错误,所以有一些直接输出结果的代码。
give.jsp页面相关代码:
<s:form name="addGiveForm" action="addGivePro" method="post">
<s:action name="itemcls1Pro" id="icls1List"></s:action>
<s:property value="#icls1List.msg" />
<br>
<s:property value="#icls1List.msg1" />
</s:form>
</td>
</tr>
</table>

struts.xml相关代码:
<package name="kevin" extends="struts-default">
<action name="itemcls1Pro" class="com.kevin.action.Itemcls1Action" method="findItemcls1List">
<result name="success">index.jsp</result>
</action>
</package>

Itemcls1Action代码,里面的msg,msg1都是为了测试用的:
package com.kevin.action;

import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.kevin.domain.Itemcls1;
import com.kevin.service.ItemclsService;

public class Itemcls1Action extends ActionSupport
{
private List<Itemcls1> itemcls1List;

private String msg1;
private String msg;

//依赖注入的业务逻辑组件
private ItemclsService itemclsService;

public String getMsg1()
{
return msg1;
}

public void setMsg1(String msg1)
{
this.msg1 = msg1;
}

public String getMsg()
{
return msg;
}

public void setMsg(String msg)
{
this.msg = msg;
}

public List<Itemcls1> getItemcls1List()
{
return itemcls1List;
}

public void setItemcls1List(List<Itemcls1> itemcls1List)
{
this.itemcls1List = itemcls1List;
}

public void setItemClsService(ItemclsService itemclsService)
{
this.itemclsService = itemclsService;
}

public String findItemcls1List() throws Exception
{
this.msg1 = "abcdfsfsdfd";
System.out.println("Actionsuccess");
System.out.println(msg1);
this.msg = itemclsService.testResust();
System.out.println(msg);
return SUCCESS;
}
}

service.xml代码,配置bean的依赖关系,使用的是action和业务逻辑组件bean自动装配的形式,去掉了无关的部分:


<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的Schema信息 -->
<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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<bean id="itemclsService" class="com.kevin.service.impl.ItemclsServiceImpl">
</bean>

</beans>

ItemclsService接口代码:

package com.kevin.service;

import java.util.List;

public interface ItemclsService
{
String testResust();
}

ItemclsServiceImpl类代码,实现ItemclsService接口
package com.kevin.service.impl;

import java.util.List;
import com.kevin.service.ItemclsService;

public class ItemclsServiceImpl implements ItemclsService
{
@Override
public String testResust()
{
System.out.println("Springsuccess");
return "Resultsuccess";
}

}

另外还有applicationContext.xml代码,里面有很多和本错误无关的部分:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的Schema信息 -->
<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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 指定连接数据库的驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<!-- 指定连接数据库的URL -->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/exchange"/>
<!-- 指定连接数据库的用户名 -->
<property name="user" value="root"/>
<!-- 指定连接数据库的密码 -->
<property name="password" value="123"/>
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="40"/>
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1"/>
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="1"/>
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="20"/>
</bean>

<!-- 定义Hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 依赖注入数据源,注入正是上面定义的dataSource -->
<property name="dataSource" ref="dataSource"/>
<!-- mappingResouces属性用来列出全部映射文件 -->
<property name="mappingResources">


<list>
<!-- 以下用来列出Hibernate映射文件 -->
<value>com/kevin/domain/Customer.hbm.xml</value>
<value>com/kevin/domain/Province.hbm.xml</value>
<value>com/kevin/domain/City.hbm.xml</value>
<value>com/kevin/domain/Sex.hbm.xml</value>
<value>com/kevin/domain/Give.hbm.xml</value>
<value>com/kevin/domain/Itemcls1.hbm.xml</value>
<value>com/kevin/domain/Itemcls2.hbm.xml</value>
<value>com/kevin/domain/Itemcls3.hbm.xml</value>
<value>com/kevin/domain/Message.hbm.xml</value>
<value>com/kevin/domain/Pingjia.hbm.xml</value>
<value>com/kevin/domain/Shoucang.hbm.xml</value>
<value>com/kevin/domain/Suoqu.hbm.xml</value>
</list>
</property>
<!-- 定义Hibernate的SessionFactory的属性 -->
<property name="hibernateProperties">
<!-- 配置Hibernate属性 -->
<value>
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true;
</value>
</property>
</bean>

<!-- 配置Hibernate的局部事务管理器,使用HibernateTransactionManager类 -->
<!-- 该类实现PlatformTransactionManager接口,是针对Hibernate的特定实现-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 配置HibernateTransactionManager时需要依注入SessionFactory的引用 -->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 配置事务增强处理,指定事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 用于配置详细的事务语义 -->
<tx:attributes>
<!-- 所有以'get'开头的方法是read-only的 -->
<tx:method name="get*" read-only="true"/>
<!-- 其他方法使用默认的事务设置 -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>

<aop:config>
<!-- 配置一个切入点 -->
<aop:pointcut id="kevinPointcut"
expression="bean(checkLoginService)||bean(registService)||bean(addressService)||bean(sexService)||bean(itemclsService)" />
<!-- 指定在kevinPointcut切入点应用txAdvice事务增强处理 -->
<aop:advisor advice-ref="txAdvice"
pointcut-ref="kevinPointcut"/>
</aop:config>

</beans>

web.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<!-- 配置Spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,
/WEB-INF/daoContext.xml,/WEB-INF/service.xml</param-value>
</context-param>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>


</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>


运行的结果就是:控制台和页面里都能输出在action里直接赋值的msg1,但action调用itemclsService返回的字符串msg都没有输出。另外,action里的System.out.println("Actionsuccess");也能在控制台输出,但ItemclsServiceImpl里的System.out.println("Springsuccess");没有输出。

程序不报任何错,就是该输出的不输出。

高手帮我看一下,哪里有错啊,多谢。






[解决办法]
<action name="itemcls1Pro" class="com.kevin.action.Itemcls1Action" method="findItemcls1List">这块应该是指向spring配置里的bean id.

读书人网 >J2EE开发

热点推荐