最近工程从Struts2.3.18升级Struts2.5.2导致相关联的插件都需要升级到相同版本,其中tiles的变化最大。

1、web.xml上

  • listener
org.apache.struts2.tiles.StrutsTilesListener
  • context-param
新增tiles资源位置定义参数:org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG

2、tiles.xml 资源声明改成使用value指定!!

由:
<definition name="web.standard.layout.witherror" template="layout.jsp">
<put-attribute name="header">header.jsp</put-attribute
</definition>
改为:
<definition name="web.standard.layout.witherror" template="layout.jsp">
<put-attribute name="header" value="header.jsp"></put-attribute
</definition>

3、默认会加载/WEB-INF/tiles.xml

这样tiles升级基本完成。


下面介绍自定义tiles资源搜索功能:

从org.apache.struts2.tiles.StrutsTilesInitializer源码:

  @Override
protected ApplicationContext createTilesApplicationContext(ApplicationContext preliminaryContext) {
ServletContext servletContext = (ServletContext) preliminaryContext.getContext(); if (servletContext.getInitParameter(DefinitionsFactory.DEFINITIONS_CONFIG) != null) {
LOG.trace("Found definitions config in web.xml, using standard Servlet support ....");
return new ServletApplicationContext(servletContext);
} else {
LOG.trace("Initializing Tiles wildcard support ...");
return new StrutsWildcardServletApplicationContext(servletContext);
}
}

上可得知当在web.xml上自定义了tiles的资源文件位置的话使用ServletApplicationContext进行资源文件的查找。但是一般工程都是使用通配符的形式或者使用spring的文件路径定义的方式配置文件,例如:

file:/WEB-INF/tiles.xml,classpath:/com/cml/test/*/resource/.tiles.xml

此时需要自己拓展了,使用spring的ResourcePatternResolver进行资源查找。

1、第一步,自定义listener

public class WildcardTilesListener extends AbstractTilesListener {

    private static final Logger LOG = LogManager.getLogger(WildcardTilesListener.class);

    @Override
protected TilesInitializer createTilesInitializer() {
LOG.info("Starting Struts Tiles 3 integration ...");
return new WildcardInitializer();
} }

2、第二步 自定义StrutsTilesInitializer

public class WildcardInitializer extends StrutsTilesInitializer {
private static final Logger LOG = LogManager.getLogger(WildcardInitializer.class); @Override
protected ApplicationContext createTilesApplicationContext(ApplicationContext preliminaryContext) {
ServletContext servletContext = (ServletContext) preliminaryContext.getContext();
return new WildcardApplicationContext(servletContext);
} @Override
protected AbstractTilesContainerFactory createContainerFactory(ApplicationContext context) {
LOG.trace("Creating dedicated Struts factory to create Tiles container");
return new StrutsTilesContainerFactory();
} }

将tiles文件搜索的ApplicationContext更改为自定义的WildcardApplicationContext

3、第三步 继承ServletApplicationContext实现通配符资源文件搜索

public class WildcardApplicationContext extends ServletApplicationContext {

    private static final Logger LOG = LogManager.getLogger(StrutsWildcardServletApplicationContext.class);

    private ServletContext context;

    public WildcardApplicationContext(ServletContext context) {
super(context);
this.context = context;
} public Collection<ApplicationResource> getResources(String path) {
Set<ApplicationResource> resources = new HashSet<>(); if (path.startsWith("/")) {
LOG.trace("Using ServletContext to load resource {}", path);
ApplicationResource resource = getResource(path);
if (resource != null) {
resources.add(resource);
}
} try {
resources.addAll(findResources(path));
} catch (IOException e) {
LOG.error("Cannot find resources for [{}]", path, e);
} return resources;
} public ApplicationResource getResource(ApplicationResource base, Locale locale) {
String localePath = base.getLocalePath(locale);
if (new File(localePath).exists()) {
try {
return new StrutsApplicationResource(URI.create("file://" + localePath).toURL());
} catch (MalformedURLException e) {
LOG.warn("Cannot access [{}]", localePath, e);
return null;
}
}
return null;
} protected Set<ApplicationResource> findResources(String path) throws IOException {
Set<ApplicationResource> resources = new HashSet<>(); LOG.trace("Using ResourceFinder to find matches for {}", path); Set<URL> matches = find(path); LOG.trace("Found resources {} matching pattern {}", matches, path); Iterator<URL> it = matches.iterator();
while (it.hasNext()) {
resources.add(new StrutsApplicationResource(it.next()));
} LOG.trace("Found resources {} for path {}", resources, path);
return resources;
} protected Set<URL> find(String patternResource) { Set<URL> pathResources = new HashSet<>(); try {
ResourcePatternResolver rpResolver = new PathMatchingResourcePatternResolver(); Resource[] resources = rpResolver.getResources(patternResource);
// FileSystemResource ClasspathContextResource
for (Resource resource : resources) { URL resourceUrl = null; if (resource instanceof UrlResource) {
resourceUrl = context.getResource(resource.getFile().toString());
} else if (resource instanceof ClassPathResource) {
ClassPathResource classpathRes = (ClassPathResource) resource;
resourceUrl = context.getResource(classpathRes.getPath());
} else {
resourceUrl = resource.getURL();
} if (resourceUrl != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Adding resource '" + resourceUrl + "' to definitions factory.");
}
pathResources.add(resourceUrl);
// pathResources.put(resourceUrl.toString(), resourceUrl);
} else {
LOG.warn("Unable to find configured definition '" + resource + "'");
}
}
} catch (IOException e) {
LOG.error("<<<<<find>>>>", e);
} return pathResources;
} }

这样就可以使用spring的配置文件路径的方式配置tiles资源文件了,满足通配符。

4、最后一步 在web.xml上配置tiles文件的位置和listener,自动搜索工程resoruce/*.tiles.xml文件

    <context-param>
<param-name>org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG</param-name>
<param-value>file:/WEB-INF/tiles.xml,classpath:/com/cml/tile/**/resource/*.tiles.xml</param-value>
</context-param> <listener>
<listener-class>xxx.x..x.WildcardTilesListener</listener-class>
</listener>

由此,Struts-Tiles 2.5.2升级配置完成。

Struts2-Tiles 2.5.2 升级指南和通配符拓展的更多相关文章

  1. 企业IT管理员IE11升级指南【17】—— F12 开发者工具

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  2. 企业IT管理员IE11升级指南【16】—— 使用Compat Inspector快速定位IE兼容性问题

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  3. 企业IT管理员IE11升级指南【15】—— 代理自动配置脚本

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  4. 企业IT管理员IE11升级指南【1】—— Internet Explorer 11增强保护模式 (EPM) 介绍

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  5. 企业IT管理员IE11升级指南【2】—— Internet Explorer 11 对Adobe Flash的支持

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  6. 企业IT管理员IE11升级指南【3】—— IE11 新的GPO设置

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  7. 企业IT管理员IE11升级指南【4】—— IE企业模式介绍

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  8. 企业IT管理员IE11升级指南【5】—— 不跟踪(DNT)例外

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  9. 企业IT管理员IE11升级指南【6】—— Internet Explorer 11面向IT专业人员的常见问题

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

随机推荐

  1. gridview 合并单元格后,选中颜色重新绘制

    gv_docargo.RowStyle += OnRowStyle; private void OnRowStyle(object sender, DevExpress.XtraGrid.Views. ...

  2. Fiddler抓取抖音视频

    目录 工具 Fiddler配置 手机端配置 工具 Android 或 ios手机均可 Fiddler 下载地址:https://www.telerik.com/fiddler Windows 操作系统 ...

  3. 原生Js贪吃蛇游戏实战开发笔记

    前言 本课程是通过JavaScript结合WebAPI DOM实现的一版网页游戏---贪吃蛇的开发全过程,采用面向以象的思想设计开发.通过这个小游戏的开发, 不仅可以掌握JS的语法的应用,还可以学会D ...

  4. TensorFlow-keras 100分类

    import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' from tensorflow.python.keras.datasets import cifa ...

  5. Java IO 流-- 文件拷贝

    IO流操作套路: 1.创建源: 2.选择流: 3.操作: 4.释放资源 上代码: package com.xzlf.io; import java.io.File; import java.io.Fi ...

  6. Python爬虫之记录一次下载验证码的尝试

      好久没有写过爬虫的文章了,今天在尝试着做验证码相关的研究时,遇到了验证码的收集问题.   一般,验证码的加载都有着比较复杂的算法和加密在里边,但是笔者今天碰到的验证码却比较幸运,有迹可循.在此,给 ...

  7. 加分二叉树 vijos1991 NOIP2003第三题 区间DP/树形DP/记忆化搜索

    描述 设一个n个节点的二叉树tree的中序遍历为(l,2,3,-,n),其中数字1,2,3,-,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都有一 ...

  8. 批量将制定文件夹下的全部Excel文件导入微软SQL数据库

    以下代码将c:\cs\文件夹下的全部Excle中数据导入到SQL数据库 declare @query vARCHAR(1000) declare @max1 int declare @count1 i ...

  9. 【K8S】基于单Master节点安装K8S集群

    写在前面 最近在研究K8S,今天就输出部分研究成果吧,后续也会持续更新. 集群规划 IP 主机名 节点 操作系统版本 192.168.175.101 binghe101 Master CentOS 8 ...

  10. php安装igbinary扩展(windows)

    pecl.php.net 是php的扩展仓库,访问此网站后,搜索需要安装的扩展,截图如下: igbinary: http://pecl.php.net/package/redis github的网址: ...