最近工程从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. bugku ctf 逆向题

    1.逆向入门 2.Easy_vb 直接找出来. 3.easy_re 4.游戏过关 摁着嗯着就出来了... 5.Timer{阿里ctf} apk文件,不会搞. 6.逆向入门 发现是base64,直接转图 ...

  2. 【编程之美】超时重传,滑动窗口,可靠性传输原理C语言实现

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://www.cnblogs.com/lihuidashen/p/128003 ...

  3. 2019-2020-1 20199328《Linux内核原理与分析》第八周作业

    笔记部分 2019/11/4 17:55:22 elf文件代码默认加载到0x8048000,然后是一段首部信息,然后到达程序的真实入口 正常的系统调用会先进入内核态->用户态->系统调用下 ...

  4. 【手把手教你】win10 虚拟机 VMware Workstation Pro 15下安装Ubuntu 19.04

    虚拟机 VMware Workstation Pro 15.5.0 及永久激活密钥 https://www.cnblogs.com/zero-vic/p/11584437.html Ubuntu19. ...

  5. 【JAVA基础】11 Scanner类

    1. Scanner的概述 一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器. Scanner 使用分隔符模式将其输入分解为标记,默认情况下该分隔符模式与空白匹配.然后可以使用不同的 ne ...

  6. windows服务程序的编写

    服务编写https://blog.csdn.net/lanuage/article/details/77937407 #include <windows.h> #include <s ...

  7. dhcpd.conf(5) - Linux man page

    http://linux.die.net/man/5/dhcpd.conf Name dhcpd.conf - dhcpd configuration file Description   The d ...

  8. 常用的CSS小技巧

    实际开发过程中会遇到一些需要用CSS小技巧处理的布局问题,现在分享几个个人工作中遇到的小问题和解决方案. 1.inline元素间的空白间隙 这里要介绍一个神器font-size:0. 如果你写了个列表 ...

  9. 《Android的设计与实现:卷I》——第1章 1.2.2动态视角的体系结构

    1.2.2 动态视角的体系结构静态的体系结构是从横向分层的角度诠释Android是什么.如果静态的体系结构不足以让读者理解Android的运行机制,我们可以看看Google工程师Sans Serif是 ...

  10. 一只简单的网络爬虫(基于linux C/C++)————利用正则表达式解析页面

    我们向一个HTTP的服务器发送HTTP的请求后,服务器会返回可能一个HTML页面(当然也可以是其他的资源),我们可以利用返回的HTML页面,在其中寻找其他的Url,例如我们可以这样在浏览器上查看一下H ...