9、ssh的集成方式2

1、在第一种的集成方式中,通过struts2-spring-plugin-2.1.8.1.jar这个插件让spring自动产生对应需要的action类,不需要在对应的spring.xml文件中进行配置,这种方法不能对action进行精确的管理和控制

第二种方式,我们在spring的xml配置文件中对action进行配置和管理

在第一种方式的基础上,我们新建一个applicationContext-action.xml文件

我们在该配置文件中,需要对action进行配置

 文件内容如下所示:

复制代码
<?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.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--name="userService 必须和AddUserAction中的    private UserService userService; 一一对应  -->        
<!-- ref="userService"必须和applicationContext-service.xml中<bean id="userService" 一一对应  -->    
<!-- 为了保证一个action对应一个线程,这里必须要配置scope="prototype" -->    
    <bean id="addUserAction" class="com.weiyuan.test.web.action.AddUserAction" scope="prototype">
        <property name="userService" ref="userService"/>
    </bean>            
</beans>
复制代码

 

上面有几点必须要注意的:

<!--name="userService 必须和AddUserAction中的    private UserService userService; 一一对应  -->        
<!-- ref="userService"必须和applicationContext-service.xml中<bean id="userService" 一一对应  -->    
<!-- 为了保证一个action对应一个线程,这里必须要配置scope="prototype" -->    
还必须把上面的配置文件加载到applicationContext.xml中
复制代码
<?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.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <import resource="applicationContext-service.xml"/>
    <import resource="applicationContext-action.xml"/>
    <import resource="applicationContext-dao.xml"/>
    <import resource="applicationContext-common.xml"/>
</beans>
复制代码

 


2、第二要修改action在struct2中的配置
复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.i18n.encoding" value="GB18030"/>
    <constant name="struts.configuration.xml.reload" value="true"/>
    <constant name="struts.devMode" value="true"/>
    <package name="user" extends="struts-default" >
     <action name="add" class="com.weiyuan.test.web.action.AddUserAction">
     <result name="sucess">/add_success.jsp</result>
     </action>
    </package>
</struts>
需要将     <action name="add" class="com.weiyuan.test.web.action.AddUserAction">修改为下面的形式
复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.i18n.encoding" value="GB18030"/>
    <constant name="struts.configuration.xml.reload" value="true"/>
    <constant name="struts.devMode" value="true"/>
    <package name="user" extends="struts-default" >
    <!-- class="addUserAction">必须和applicationContext-action.xml中配置的<bean id="addUserAction" 一一对应-->
     <action name="add" class="addUserAction">
     <result name="sucess">/add_success.jsp</result>
     </action>
    </package>
</struts>
复制代码

 

如果这里不进行修改,还是按照第一种方式spring利用 com.weiyuan.test.web.action.AddUserAction产生对应的action对象,就会有问题

经过上面的修改就可以,在项目中我们按照第二种方式集成
复制代码

 程序代码的下载地址是:https://pan.baidu.com/s/1jIckYea

posted on   luzhouxiaoshuai  阅读(139)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示