Struts2升级2.5.30的那些坑

1.版本修改

将pom.xml 文件修改为2.5.30版本

<properties>   
	<struts2-version>2.5.30</struts2-version>    
</properties>
<dependencies>
	<dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-spring-plugin</artifactId>
        <version>${struts2-version}</version>  
	</dependency>
	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts2-spring-plugin</artifactId>
		<version>${struts2-version}</version>
	</dependency>
</dependencies>

2.修改web.xml

把org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 中的.ng 去掉 修改为

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

3.修改struts.xml

把原先的头部信息修改为:

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">

4.方法访问不到的问题(404)

需要在每个action配置文件中加上 strict-method-invocation=“false”
再加上

<global-allowed-methods>regex:.*</global-allowed-methods>
<package name="projectstruts" extends="struts-default" strict-method-invocation="false">
	<global-allowed-methods>regex:.*</global-allowed-methods>
</package>

5. 依赖冲突,版本问题

依赖冲突需要根据自己的项目引入的依赖来进行排除
举例:
在这里插入图片描述
版本问题:
我升级的时候有commons-lang3 版本问题
需要将版本升为3.8.1

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.8.1</version>
</dependency>

其他问题没遇到,遇到的小伙伴可以在下方评论,帮助他人快速解决问题。

2022/04/20补充

6. 新版用法(HttpParameters)

老版本的是:

Map<String, Object> params = ServletActionContext.getContext().getParameters();

新版本的是:

HttpParameters params = ServletActionContext.getContext().getParameters();

在新版本如果想将HttpParameters转为Map 可以调用 toMap();方法,如下:

Map<String, String[]> stringMap = params.toMap();

或者遍历存入map ,如下:(提倡)

HttpParameters fileMaps = ServletActionContext.getContext().getParameters();
Map<String, Object> fileMap = new HashMap<>();
fileMaps.keySet().stream().forEach(new Consumer<String>() {
	@Override
	public void accept(String s) {
		fileMap.put(s, fileMaps.get(s));
	}
});
File[] files = (File[]) fileMap.get(key);

2022/06/7 补充

7. 新版文件上传问题(获取前端提交的文件)

我是在拦截器中处理的参数,通过继承AbstractInterceptor 来重写intercept方法处理参数,仅供参考。

/**
* 处理参数
 */
private void processRequestParamters(HttpServletRequest request) {
	if (!(request instanceof MultiPartRequestWrapper)) {
		return;
	}
	MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request;
	Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
	//从前端获取的文件
	File fileIo = null;
	while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {
		// get the value of this input tag
		String inputName = (String) fileParameterNames.nextElement();
		// get the content type
		String[] contentType = multiWrapper.getContentTypes(inputName);
		if (contentType != null && contentType.length > 0) {
			// get the name of the file from the input tag
			String[] fileName = multiWrapper.getFileNames(inputName);
			if (fileName != null && fileName.length > 0) {
				// get a File object for the uploaded File
				UploadedFile[] files = multiWrapper.getFiles(inputName);
				Object obj;
				if (files.getClass().isArray() && Array.getLength(files) == 1) {
					obj = Array.get(files, 0);
				} else {
					obj = files;
				}
				if (obj instanceof UploadedFile) {
					UploadedFile file = (UploadedFile) obj;
					if (file.getContent() instanceof File) {
						fileIo = (File) file.getContent();
					} else {
						fileIo = new File(file.getAbsolutePath());
					}
				} else {
					fileIo = new File(obj.toString());
				}
			}
		}
	}
}

若有错误,希望大佬指出。

对你有帮助给点个👍再走呗。

  • 17
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 35
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 35
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值