SSH整合系列2--在Struts2.1.8.1中整合Spring3.0.3

本文介绍如何在Struts2框架中整合Spring3.0,包括引入必要的jar包、配置Spring监听器以及设置自动装配等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自:http://scorpiomiracle.iteye.com/blog/615635

 

由于Spring3.0的版本与以前的版本是一个重大的分割点。所以有很多变化,这里仅仅阐述整合的方法,不研究其新的使用方法。

1.引入spring的jar包
2.创建Spring的配置文件
3.整合Struts2和Spring3.0
 3.1.Usage(使用),配置目标工厂为Spring。
 3.2.Autowiring(自动装配),配置监听器


1.引入spring的必需 jar包
将\dist文件夹下所有jar包复制到lib文件夹下。
由于3.0版本和以前的版本有很大的不同,包结构也不一样,不会再提供spring.jar这个完整包了,由于没有查到哪些包是必须的,测试一些包后也依然报错,就暂时将其全部考进来了 -_-

2.创建Spring的配置文件
Spring的org.springframework.web.context.ContextLoaderListener监听器默认读取的是WEB-INF目录下的applicationContext.xml。
如果需要加载其它配置文件,或者放在了其它路径,则需要在web.xml中进行如下配置:

Xml代码 复制代码
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>/WEB-INF/applicationContext*.xml,classpath*:applicationContext-*.xml</param-value>  
  4. </context-param>  
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>


说明:
a.这部分代码来自Apache官网Struts项目的Documentation。在Struts项目的首页点击Documentation下Struts的最新版本,然后就会出现三个巨大的按钮,Download Now,Get Started,Plugin Registry,其中Get Started会教你如何使用Struts,而Plugin Registry则会教你如何注册各种插件。点击进入后就会在Plugins栏目中看到在struts中使用各种插件方法的超链接,点击Spring Plugin进入使用spring的说明页面,点击链接后就会看到使用的详细的方法。----这是一个好的学习技巧。
b.上面的classpath就是.java文件编译成的.class文件所在的位置,即/WEB-INF/classes文件夹。另外,写上.xml文件的全称也可以啊。
其实,可以从Struts2源码包的例子工程中拷贝一份applicationContext.xml放到WEB-INF目录下,然后只留下文件头就可以了,主要就是DTD。

3.整合Struts2和Spring3.0
本方法依然取自Apache网站,更加详细的说明请参见Apache网站。
3.1.Usage(使用):
引入struts提供的包struts2-spring-plugin-xxx.jar即可。
如果使用了多个object factory,还必须进行如下配置:
由于struts同时支持.properties和.xml配置文件,选择其一即可。
struts.properties:

Xml代码 复制代码
  1. struts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory  
struts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory


struts.xml:

Xml代码 复制代码
  1. <struts>  
  2. <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />  
  3.   ...    
  4. </struts>  
<struts>
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
  ... 
</struts>


3.2.Autowiring(自动装配):
在web.xml配置spring的监听器。

Xml代码 复制代码
  1. <listener>  
  2.   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  3. </listener>  
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


这一监听器主要是提供spring的自动装配功能,默认是依靠名字自动装配,可以在<bean>或<beans>标签中更改default-autowire属性,一般就是byName。此外也可以在struts.properties中作为整体配置修改struts.objectFactory.spring.autoWire = name/type/auto/constructor。

解决错误:> Could not resolve all files for configuration &#39;:hd-todo-server:runtimeClasspath&#39;. > Could not find org.opensaml:opensaml-xacml-impl:4.3.0. Required by: project :hd-todo-server > com.hd.cloud:hd-user-server-client:8.1.0.31-JDK17 > com.hd.rcugrc:hd-security-impl:8.1.0.31-JDK17 > Could not find org.opensaml:opensaml-xacml-saml-impl:4.3.0. Required by: project :hd-todo-server > com.hd.cloud:hd-user-server-client:8.1.0.31-JDK17 > com.hd.rcugrc:hd-security-impl:8.1.0.31-JDK17 > Could not find org.opensaml:opensaml-saml-impl:4.3.0. Required by: project :hd-todo-server > com.hd.cloud:hd-user-server-client:8.1.0.31-JDK17 > com.hd.rcugrc:hd-security-impl:8.1.0.31-JDK17 > Could not find org.opensaml:opensaml-saml-impl:4.3.0. Required by: project :hd-todo-server > com.hd.cloud:hd-user-server-client:8.1.0.31-JDK17 > com.hd.rcugrc:hd-security-impl:8.1.0.31-JDK17 > org.apache.wss4j:wss4j-ws-security-dom:3.0.3 > org.apache.wss4j:wss4j-ws-security-common:3.0.3 > Could not find org.opensaml:opensaml-xacml-impl:4.3.0. Required by: project :hd-todo-server > com.hd.cloud:hd-user-server-client:8.1.0.31-JDK17 > com.hd.rcugrc:hd-security-impl:8.1.0.31-JDK17 > org.apache.wss4j:wss4j-ws-security-dom:3.0.3 > org.apache.wss4j:wss4j-ws-security-common:3.0.3 > Could not find org.opensaml:opensaml-xacml-saml-impl:4.3.0. Required by: project :hd-todo-server > com.hd.cloud:hd-user-server-client:8.1.0.31-JDK17 > com.hd.rcugrc:hd-security-impl:8.1.0.31-JDK17 > org.apache.wss4j:wss4j-ws-security-dom:3.0.3 > org.apache.wss4j:wss4j-ws-security-common:3.0.3
05-28
<think>嗯,用户遇到了Gradle构建时缺少opensaml依赖的问题。首先,我需要确认opensaml的依赖仓库是否正确。因为ApacheOpenSAML通常不在MavenCentral,可能需要在仓库中添加Spring的Repo或者JitPack。然后,用户可能没有正确声明依赖的版本。比如,4.3.0版本是否存在?或者是否应该使用更新的版本?需要检查官方文档或仓库确认可用版本。另外,依赖传递的问题也可能导致缺失。有时候,某个模块可能需要其他模块的支持,比如opensaml-core或者opensaml-security。用户是否遗漏了这些依赖?还有可能是Gradle的配置问题,比如是否使用了正确的配置(如implementation或api),或者是否需要排除冲突的依赖。这时候需要检查build.gradle文件,看看依赖声明是否正确,是否有冲突需要排除。最后,建议用户使用Gradle的依赖树命令(./gradlew dependencies)来查看具体的依赖路径,找出哪里出了问题。可能还需要清除缓存或重新同步项目。</think>### Gradle缺少opensaml依赖的解决方案 出现`Could not resolve all files for configuration runtimeClasspath`错误通常由依赖仓库缺失或版本冲突导致。以下是分步解决方案: 1. **添加专用仓库** Apache OpenSAML 4.x依赖通常位于Spring仓库: ```groovy repositories { mavenCentral() maven { url "https://build.shibboleth.net/nexus/content/repositories/releases/" } maven { url "https://repo.spring.io/release" } } ``` 2. **声明完整依赖组** OpenSAML 4.x需要模块化声明: ```groovy implementation &#39;org.opensaml:opensaml-core:4.3.0&#39; implementation &#39;org.opensaml:opensaml-xacml-impl:4.3.0&#39; implementation &#39;org.opensaml:opensaml-security-impl:4.3.0&#39; ``` 3. **处理传递依赖** 检查是否缺少必要组件: ```groovy implementation(&#39;org.opensaml:opensaml-xacml-impl:4.3.0&#39;) { exclude group: &#39;xml-apis&#39;, module: &#39;xml-apis&#39; } ``` 4. **验证依赖树** ```bash ./gradlew :app:dependencies --configuration runtimeClasspath ``` 5. **清除缓存** ```bash rm -rf $HOME/.gradle/caches/ ./gradlew clean build --refresh-dependencies ``` [^1]: OpenSAML官方推荐使用Shibboleth Nexus仓库获取稳定版本依赖 [^2]: Gradle官方文档建议使用--refresh-dependencies强制更新缓存
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值