2 liaodaer liaodaer 于 2016.03.09 19:44 提问

struts整合spring,请教大神们

在myeclipse中已经导入了自带的struts框架,怎么把struts整合到spring中,也就是action交给spring管理

6个回答

qq_30408111
qq_30408111   2016.03.10 14:01

我的spring配置,你可以参考一下

application-action.xml
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="baseAction" class="cn.itcast.shop.action.BaseAction" scope="prototype">
    <property name="accountService" ref="accountService" />
    <property name="categoryService" ref="categoryService" />
    <property name="goodsService" ref="goodsService" />
    <property name="fileUploadUtil" ref="fileUploadUtil" />
    <property name="sorderService" ref="sorderService" />
    <property name="forderService" ref="forderService" />
    <property name="usersService" ref="usersService" />
    <property name="onlinePayService" ref="onlinePayService" />
    <property name="shopEmaiUtill" ref="shopEmaiUtill" />
    <property name="luceneServiceImpl" ref="luceneServiceImpl" />
    <property name="roleService" ref="roleService" />
    <property name="privilegeService" ref="privilegeService" />
</bean>
<bean id="forderAction" class="cn.itcast.shop.action.ForderAction" parent="baseAction" scope="prototype" />
<bean id="accountAction" class="cn.itcast.shop.action.AccountAction" parent="baseAction" scope="prototype"  />
<bean id="categoryAction" class="cn.itcast.shop.action.CategoryAction" parent="baseAction" scope="prototype" />
<bean id="goodsAction" class="cn.itcast.shop.action.GoodsAction" parent="baseAction" scope="prototype" />
<bean id="sorderAction" class="cn.itcast.shop.action.SorderAction" parent="baseAction" scope="prototype" />
<bean id="usersAction" class="cn.itcast.shop.action.UsersAction" parent="baseAction" scope="prototype" />
<bean id="payAction" class="cn.itcast.shop.action.PayAction" parent="baseAction" scope="prototype" />
<bean id="roleAction" class="cn.itcast.shop.action.RoleAction" parent="baseAction" scope="prototype" />

application-bean.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- 
    1: Spring来管理Hibernate配置文件,就是取代了HibernateSessionFactory
 -->
 <!-- 1: -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <!-- 加载hibernate的配置文件 -->
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
 </bean>

 <!-- 2 -->
 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
     <!-- 需要sessionFactory -->
     <property name="sessionFactory" ref="sessionFactory" />
 </bean>

 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 <bean id="baseService" class="cn.itcast.shop.service.impl.BaseServiceImpl" lazy-init="true">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
 </bean>
 <!-- parent 在Spring配置文件中 找父类, 父类通过Spring的方式创建, 这样父类就可以实现依赖注入 -->
 <bean id="logsService" class="cn.itcast.shop.service.impl.LogsServiceImpl" parent="baseService" />
 <bean id="usersService" class="cn.itcast.shop.service.impl.UsersServiceImpl" parent="baseService" />
 <bean id="forderService" class="cn.itcast.shop.service.impl.ForderServiceImpl" parent="baseService" />
 <bean id="sorderService" class="cn.itcast.shop.service.impl.SorderServiceImpl" parent="baseService" />
 <bean id="accountService" class="cn.itcast.shop.service.impl.AccountServiceImpl" parent="baseService" />
 <bean id="categoryService" class="cn.itcast.shop.service.impl.CategoryServiceImpl" parent="baseService" />
 <bean id="goodsService" class="cn.itcast.shop.service.impl.GoodsServiceImpl" parent="baseService" />
 <bean id="roleService" class="cn.itcast.shop.service.impl.RoleServiceImpl" parent="baseService" />
 <bean id="privilegeService" class="cn.itcast.shop.service.impl.PrivilegeServiceImpl" parent="baseService" />
 <bean id="fileUploadUtil" class="cn.itcast.shop.util.FileUploadUtil">
    <property name="filePath" value="D:\apache-tomcat-6.0.18\webapps\shop\image\" />
 </bean>
 <bean id="shopTimerTask" class="cn.itcast.shop.util.ShopTimerTask">
    <property name="categoryService" ref="categoryService" />
    <property name="goodsService" ref="goodsService" />
 </bean>
 <bean id="onlinePayService" class="cn.itcast.shop.service.impl.OnlinePayService" />
 <bean id="shopEmaiUtill" class="cn.itcast.shop.util.ShopEmaiUtill" />

liaodaer
liaodaer 多谢啦
接近 2 年之前 回复
qq_20039385
qq_20039385   2016.03.09 19:52

用spring IOC控制反转。配置下就行。

qq_20039385
qq_20039385 回复liaodaer: 就是要配置beans的xml文件,不用xml用注解也可以。百度下应该有很多资料
接近 2 年之前 回复
liaodaer
liaodaer 就是不会配置,能教我一下吗
接近 2 年之前 回复
wojiushiwo945you
wojiushiwo945you   Ds   Rxr 2016.03.10 08:03

myEclipse整合SSH三大框架很方便的,它提供了右键菜单的,你选择项目然后右键就有添加SSH功能的选项了。

rui888
rui888   Ds   Rxr 2016.03.10 10:01

myeclipse 里面也有自带 spring 的

wangkang80
wangkang80   2016.03.10 10:28

虽然没有struts但是用了spring mvc,可参考:http://git.oschina.net/wangkang/llsfw

可快速搭建开发环境,希望能够帮到你.

liaodaer
liaodaer 多谢啦
接近 2 年之前 回复
xzchaoo
xzchaoo   2016.03.11 13:58

你只要将 struts-spring-xxx.jar 这个jar包加入项目就行了, 然后自动的你的action就是由spring去管理了
不信你打开这个jar包, 里面有一个 struts插件的xml文件 你看看这个文件里写的就知道了
还不信的话你就试试看你的Action类能不能自动注入一个配置在Spring管理下的bean就行了.

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!