下载struts2.5.26jar包

官网下载

更新jar

新增或替换

asm-7.3.1.jar
asm-analysis-7.3.1.jar
asm-commons-7.3.1.jar
asm-tree-7.3.1.jar
commons-lang3-3.8.1.jar
commons-io-2.6.jar
commons-fileupload-1.4.jar
javassist-3.20.0-GA.jar
log4j-api-2.12.1.jar
ognl-3.1.28.jar
struts2-core-2.5.26.jar
struts2-json-plugin-2.5.26.jar
struts2-junit-plugin-2.5.26.jar
struts2-spring-plugin-2.5.26.jar
xpp3_min-1.1.4c.jar
xstream-1.4.11.1.jar
xmlpull-1.1.3.1.jar

删除

xwork-core-2.3.32.jar
2.5.X已把xwork-core整合到struts2-core,所以删除

修改web.xml

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

修改weblogic.xml(如果有)

新增session-descriptor

<weblogic-web-app><session-descriptor><cookie-name>JSESSIONID1</cookie-name></session-descriptor>
</weblogic-web-app>

修改struts.xml

2.1–》2.5

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

新增配置

  • package标签添加strict-method-invocation="false"和下列标签。
  • global-allowed-methods:放最下面,过滤自定义action的方法,不然不能访问
  • 如果已经配置struts.properties,可不加constant标签
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.action.extension" value="do" />
<package extends="struts-default" strict-method-invocation="false"><global-results></global-results><global-allowed-methods>regex:.*</global-allowed-methods>
</package>

修改struts.xml的result-types配置

  • 注释部分为struts2.3.32配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN""http://struts.apache.org/dtds/struts-2.5.dtd">
<struts><package name="struts-default" abstract="true"  strict-method-invocation="false"><result-types><result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/><!-- <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult"/> --><result-type name="dispatcher" class="org.apache.struts2.result.ServletDispatcherResult" default="true"/><result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult" default="true"/><!-- <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/><result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/><result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/><result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/><result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> --><result-type name="httpheader" class="org.apache.struts2.result.HttpHeaderResult"/><result-type name="redirect" class="org.apache.struts2.result.ServletRedirectResult"/><result-type name="redirectAction" class="org.apache.struts2.result.ServletActionRedirectResult"/><result-type name="stream" class="org.apache.struts2.result.StreamResult"/><result-type name="velocity" class="org.apache.struts2.result.VelocityResult"/><result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/><!-- <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /><result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" /> --><result-type name="plainText" class="org.apache.struts2.result.PlainTextResult" /><result-type name="postback" class="org.apache.struts2.result.PostbackResult" /><!--自定义result-type--><result-type name="json" class="org.apache.struts2.json.JSONResult"><param name="root">jsonRoot</param></result-type>         </result-types><interceptors><interceptor name="i18n" class="org.apache.struts2.interceptor.I18nInterceptor"/><!-- <interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/> --></interceptors><!-- 其他配置不变,这里略 --><default-class-ref class="com.opensymphony.xwork2.ActionSupport" /><global-allowed-methods>regex:.*</global-allowed-methods></package>
</struts>

如果重写了ParametersInterceptor.java类

原方法

protected Map<String, Object> retrieveParameters(ActionContext ac) {return ac.getParameters();
}

新方法

protected Map<String, String[]> retrieveParameters(ActionContext ac) {HttpParameters httpParameters = ac.getParameters();return httpParameters.toMap();
}

处理上传附件问题(2021.03.30修改)

修改struts.xml的interceptors配置

  • 注释部分为struts2.3.32配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN""http://struts.apache.org/dtds/struts-2.5.dtd">
<struts><package name="struts-default" abstract="true"  strict-method-invocation="false"><result-types><result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/><!-- <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult"/> --><result-type name="dispatcher" class="org.apache.struts2.result.ServletDispatcherResult" default="true"/><result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult" default="true"/><!-- <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/><result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/><result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/><result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/><result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> --><result-type name="httpheader" class="org.apache.struts2.result.HttpHeaderResult"/><result-type name="redirect" class="org.apache.struts2.result.ServletRedirectResult"/><result-type name="redirectAction" class="org.apache.struts2.result.ServletActionRedirectResult"/><result-type name="stream" class="org.apache.struts2.result.StreamResult"/><result-type name="velocity" class="org.apache.struts2.result.VelocityResult"/><result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/><!-- <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /><result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" /> --><result-type name="plainText" class="org.apache.struts2.result.PlainTextResult" /><result-type name="postback" class="org.apache.struts2.result.PostbackResult" /><!--自定义result-type--><result-type name="json" class="org.apache.struts2.json.JSONResult"><param name="root">jsonRoot</param></result-type>         </result-types><interceptors><interceptor name="i18n" class="org.apache.struts2.interceptor.I18nInterceptor"/><!-- <interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/> --><interceptor name="fileUpload" class="com.struts2.interceptor.FileUploadInterceptor"/>
<!--        <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/> --></interceptors><!-- 其他配置不变,这里略 --><default-class-ref class="com.opensymphony.xwork2.ActionSupport" /><global-allowed-methods>regex:.*</global-allowed-methods></package>
</struts>

FileUploadInterceptor.java

package com.struts2.interceptor;import java.io.File;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;import javax.servlet.http.HttpServletRequest;import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.LocalizedMessage;
import org.apache.struts2.dispatcher.Parameter;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.apache.struts2.dispatcher.multipart.UploadedFile;
import org.apache.struts2.util.ContentTypeMatcher;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.LocaleProviderFactory;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.TextProviderFactory;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.ValidationAware;
import com.opensymphony.xwork2.util.TextParseUtil;public class FileUploadInterceptor extends AbstractInterceptor {private static final long serialVersionUID = -4764627478894962478L;protected static final Logger LOG = LogManager.getLogger(FileUploadInterceptor.class);protected Long maximumSize;protected Set<String> allowedTypesSet = Collections.emptySet();protected Set<String> allowedExtensionsSet = Collections.emptySet();private ContentTypeMatcher matcher;private Container container;@Injectpublic void setMatcher(ContentTypeMatcher matcher) {this.matcher = matcher;}@Injectpublic void setContainer(Container container) {this.container = container;}/*** Sets the allowed extensions** @param allowedExtensions A comma-delimited list of extensions*/public void setAllowedExtensions(String allowedExtensions) {allowedExtensionsSet = TextParseUtil.commaDelimitedStringToSet(allowedExtensions);}/*** Sets the allowed mimetypes** @param allowedTypes A comma-delimited list of types*/public void setAllowedTypes(String allowedTypes) {allowedTypesSet = TextParseUtil.commaDelimitedStringToSet(allowedTypes);}/*** Sets the maximum size of an uploaded file** @param maximumSize The maximum size in bytes*/public void setMaximumSize(Long maximumSize) {this.maximumSize = maximumSize;}/* (non-Javadoc)* @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation)*/public String intercept(ActionInvocation invocation) throws Exception {ActionContext ac = invocation.getInvocationContext();HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);if (!(request instanceof MultiPartRequestWrapper)) {if (LOG.isDebugEnabled()) {ActionProxy proxy = invocation.getProxy();LOG.debug(getTextMessage("struts.messages.bypass.request", new String[]{proxy.getNamespace(), proxy.getActionName()}));}return invocation.invoke();}ValidationAware validation = null;Object action = invocation.getAction();if (action instanceof ValidationAware) {validation = (ValidationAware) action;}MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request;if (multiWrapper.hasErrors() && validation != null) {TextProvider textProvider = getTextProvider(action);for (LocalizedMessage error : multiWrapper.getErrors()) {String errorMessage;if (textProvider.hasKey(error.getTextKey())) {errorMessage = textProvider.getText(error.getTextKey(), Arrays.asList(error.getArgs()));} else {errorMessage = textProvider.getText("struts.messages.error.uploading", error.getDefaultMessage());}validation.addActionError(errorMessage);}}// bind allowed FilesEnumeration fileParameterNames = multiWrapper.getFileParameterNames();while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {// get the value of this input tagString inputName = (String) fileParameterNames.nextElement();// get the content typeString[] contentType = multiWrapper.getContentTypes(inputName);if (isNonEmpty(contentType)) {// get the name of the file from the input tagString[] fileName = multiWrapper.getFileNames(inputName);if (isNonEmpty(fileName)) {// get a File object for the uploaded FileUploadedFile[] files = multiWrapper.getFiles(inputName);if (files != null && files.length > 0) {List<File> acceptedFiles = new ArrayList<>(files.length);List<String> acceptedContentTypes = new ArrayList<>(files.length);List<String> acceptedFileNames = new ArrayList<>(files.length);String contentTypeName = inputName + "ContentType";String fileNameName = inputName + "FileName";for (int index = 0; index < files.length; index++) {if (acceptFile(action, files[index], fileName[index], contentType[index], inputName, validation)) {acceptedFiles.add((File)files[index].getContent());acceptedContentTypes.add(contentType[index]);acceptedFileNames.add(fileName[index]);}}if (!acceptedFiles.isEmpty()) {Map<String, Parameter> newParams = new HashMap<>();newParams.put(inputName, new Parameter.File(inputName, acceptedFiles.toArray(new File[acceptedFiles.size()])));newParams.put(contentTypeName, new Parameter.File(contentTypeName, acceptedContentTypes.toArray(new String[acceptedContentTypes.size()])));newParams.put(fileNameName, new Parameter.File(fileNameName, acceptedFileNames.toArray(new String[acceptedFileNames.size()])));ac.getParameters().appendAll(newParams);}}} else {if (LOG.isWarnEnabled()) {LOG.warn(getTextMessage(action, "struts.messages.invalid.file", new String[]{inputName}));}}} else {if (LOG.isWarnEnabled()) {LOG.warn(getTextMessage(action, "struts.messages.invalid.content.type", new String[]{inputName}));}}}// invoke actionreturn invocation.invoke();}/*** Override for added functionality. Checks if the proposed file is acceptable based on contentType and size.** @param action      - uploading action for message retrieval.* @param file        - proposed upload file.* @param filename    - name of the file.* @param contentType - contentType of the file.* @param inputName   - inputName of the file.* @param validation  - Non-null ValidationAware if the action implements ValidationAware, allowing for better*                    logging.* @return true if the proposed file is acceptable by contentType and size.*/protected boolean acceptFile(Object action, UploadedFile file, String filename, String contentType, String inputName, ValidationAware validation) {boolean fileIsAcceptable = false;// If it's null the upload failedif (file == null) {String errMsg = getTextMessage(action, "struts.messages.error.uploading", new String[]{inputName});if (validation != null) {validation.addFieldError(inputName, errMsg);}if (LOG.isWarnEnabled()) {LOG.warn(errMsg);}} else if (file.getContent() == null) {String errMsg = getTextMessage(action, "struts.messages.error.uploading", new String[]{filename});if (validation != null) {validation.addFieldError(inputName, errMsg);}if (LOG.isWarnEnabled()) {LOG.warn(errMsg);}} else if (maximumSize != null && maximumSize < file.length()) {String errMsg = getTextMessage(action, "struts.messages.error.file.too.large", new String[]{inputName, filename, file.getName(), "" + file.length(), getMaximumSizeStr(action)});if (validation != null) {validation.addFieldError(inputName, errMsg);}if (LOG.isWarnEnabled()) {LOG.warn(errMsg);}} else if ((!allowedTypesSet.isEmpty()) && (!containsItem(allowedTypesSet, contentType))) {String errMsg = getTextMessage(action, "struts.messages.error.content.type.not.allowed", new String[]{inputName, filename, file.getName(), contentType});if (validation != null) {validation.addFieldError(inputName, errMsg);}if (LOG.isWarnEnabled()) {LOG.warn(errMsg);}} else if ((!allowedExtensionsSet.isEmpty()) && (!hasAllowedExtension(allowedExtensionsSet, filename))) {String errMsg = getTextMessage(action, "struts.messages.error.file.extension.not.allowed", new String[]{inputName, filename, file.getName(), contentType});if (validation != null) {validation.addFieldError(inputName, errMsg);}if (LOG.isWarnEnabled()) {LOG.warn(errMsg);}} else {fileIsAcceptable = true;}return fileIsAcceptable;}private String getMaximumSizeStr(Object action) {return NumberFormat.getNumberInstance(getLocaleProvider(action).getLocale()).format(maximumSize);}/*** @param extensionCollection - Collection of extensions (all lowercase).* @param filename            - filename to check.* @return true if the filename has an allowed extension, false otherwise.*/private boolean hasAllowedExtension(Collection<String> extensionCollection, String filename) {if (filename == null) {return false;}String lowercaseFilename = filename.toLowerCase();for (String extension : extensionCollection) {if (lowercaseFilename.endsWith(extension)) {return true;}}return false;}/*** @param itemCollection - Collection of string items (all lowercase).* @param item           - Item to search for.* @return true if itemCollection contains the item, false otherwise.*/private boolean containsItem(Collection<String> itemCollection, String item) {for (String pattern : itemCollection)if (matchesWildcard(pattern, item))return true;return false;}private boolean matchesWildcard(String pattern, String text) {Object o = matcher.compilePattern(pattern);return matcher.match(new HashMap<String, String>(), text, o);}private boolean isNonEmpty(Object[] objArray) {boolean result = false;for (int index = 0; index < objArray.length && !result; index++) {if (objArray[index] != null) {result = true;}}return result;}protected String getTextMessage(String messageKey, String[] args) {return getTextMessage(this, messageKey, args);}protected String getTextMessage(Object action, String messageKey, String[] args) {if (action instanceof TextProvider) {return ((TextProvider) action).getText(messageKey, args);}return getTextProvider(action).getText(messageKey, args);}private TextProvider getTextProvider(Object action) {TextProviderFactory tpf = container.getInstance(TextProviderFactory.class);return tpf.createInstance(action.getClass());}private LocaleProvider getLocaleProvider(Object action) {LocaleProvider localeProvider;if (action instanceof LocaleProvider) {localeProvider = (LocaleProvider) action;} else {LocaleProviderFactory localeProviderFactory = container.getInstance(LocaleProviderFactory.class);localeProvider = localeProviderFactory.createLocaleProvider();}return localeProvider;}
}

修改xwork-conversion.properties配置

java.io.File=com.struts2.UploadedFileConverter

UploadedFileConverter.java

package com.struts2;import java.io.File;
import java.lang.reflect.Array;
import java.lang.reflect.Member;
import java.util.Map;import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.dispatcher.multipart.StrutsUploadedFile;
import org.apache.struts2.dispatcher.multipart.UploadedFile;import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;public class UploadedFileConverter extends DefaultTypeConverter {private static final Logger LOG = LogManager.getLogger(UploadedFileConverter.class);@Overridepublic Object convertValue(Map<String, Object> context, Object target, Member member, String propertyName, Object value, Class toType) {if (File.class.equals(toType)) {LOG.debug("Converting {} into {}, consider switching to {} and do not access {} directly!",File.class.getName(), UploadedFile.class.getName(), UploadedFile.class.getName(), File.class.getName());Object obj;if (value.getClass().isArray() && Array.getLength(value) == 1) {obj = Array.get(value, 0);} else {obj = value;}if (obj instanceof UploadedFile) {UploadedFile file = (UploadedFile) obj;if (file.getContent() instanceof File) {return file.getContent();}return new File(file.getAbsolutePath());}else {return new File(obj.toString());}}return super.convertValue(context, target, member, propertyName, value, toType);}}

struts2.3.32升级到struts2.5.26相关推荐

  1. Struts2.0.11升级到2.5.30

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.设计到的jar包 二.web.xml的更改 三.strust.xml的更改 四.jsp中strust相关标签的更改 ...

  2. Struts2 2.3升级2.5.22版本遇到的问题

    jdk 1.7 升级jar包 asm-7.0.jar asm-commons-7.0.jar asm-tree-7.0.jar commons-fileupload-1.3.3.jar commons ...

  3. java面试准备之---Struts2体系知识点,系统复习,struts2原理,ognl,el支持.---随时更新

    1.MVC 和 JSP Model 2 **   a.:M-Model 模型 包含两部分:业务数据和业务处理逻辑   b.V-View 视图:视图(View)的职责是负责显示界面和用户交互(收集用户信 ...

  4. Struts2 学习笔记 — 第一个struts2项目

    创建struts2项目 勾选生成web.xml文件 配置项目 打开项目后,首先导入struts2所需的jar包,关于这个,我们可以在下载的struts包中struts-2.3.34/apps中找到st ...

  5. java struts2上传文件_java Struts2框架下实现文件上传功能

    本文实例为大家分享了struts2框架实现文件上传的方法,供大家参考,具体内容如下 struts2的配置过程 (1)在项目中加入jar包 (2)web.xml中filter(过滤器)的配置 xmlns ...

  6. Struts2框架使用(十)之struts2的上传和下载

    Struts2  文件上传 首先是Struts2的上传,Struts2 文件上传是基于 Struts2 拦截器实现的,使用的是fileupload组件: 首先如果想要上传文件,则需要在表单处添加 en ...

  7. struts2教程_Struts 2教程– Struts2教程

    struts2教程 Welcome to Struts 2 Tutorial index post. Struts 2 is one of the mostly used Java Web Appli ...

  8. struts2 2.3 升级2.5.22配置(附带漏洞检测工具)

    1.首先更新jar包.从官网下载对应版本 struts-2.5.22-lib.下面是我选择的替换需要用的包 2.一定要删除自己项目中的xwork-core-2.3.20.jar,因为这个类库在stru ...

  9. 关于struts2漏洞(升级struts到最新版本)

    由于struts旧版本爆出多个高危漏洞,对于安全性考虑需对struts升级到最新版本. 升级过程中遇到很多坑,经过一段时间的盘查发现,其实这个升级很简单. 我升级的是struts2.5.16,当前官网 ...

最新文章

  1. ganglia访问时出现“You don‘t have permission to access /ganglia/ on this server“
  2. 数据中心机房安全知识培训
  3. 如何id变动自动保存html软件,Wordpress折腾小记:彻底解决ID不连续的问题-自动保存、自动修订 | 地瓜哥博客网...
  4. mysql relaylog 慢_MySQL 主从同步延迟的原因及解决办法
  5. Python小游戏(消消乐)
  6. 循环队列的介绍与实现
  7. Win7系统转到Win10系统的装机方法
  8. 23个实用 Javascript Tab导航插件
  9. linux下./xx做了什么?
  10. 想入职阿里的Java开发者必看,阿里巴巴面试官实战经验分享!
  11. thinkphp redis队列处理_教你用ThinkPHP中thinkphpqueue
  12. 2009年上半年网工考试试题分析41-50
  13. Fortran入门教程(九)——文件
  14. CSAPP家庭作业答案7 8 9章
  15. 显示器分辨率、显示器大小和视频分辨率的关系
  16. linux系统分辨率无法修改,Redhat9重新安装后无法修改分辨率
  17. EXCEL表单元格内特定内容的提取
  18. FPGA学习-基于FPGA的高速串行通信GTX知识梳理
  19. Swift基础(六)解包
  20. 记住这些快捷键,让你的电脑效率事半功倍

热门文章

  1. 软件测试拿到项目之后该怎么做?请仔细看完这篇文章
  2. 东莞横沥潇洒职业培训学校丹丹老师分享避免因模具设计不良而使制品出现不均匀收缩
  3. Python金融数据挖掘 第7章 复习思考题 3
  4. ResultSet的介绍与使用
  5. RPC response exceeds maximum data length Cannot assign requested address java.net.UnknownH
  6. oracle性能调优之--Share Pool 调整与优化
  7. python实践输出星星_python中怎么打印星星
  8. js onbeforepaste事件用法
  9. 华为存储IA篇仿真器搭建
  10. Echarts图表(canvas)转base64图片