最近工程从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. Java集合案例(产生不重复随机数)

    获取10个1-20之间的随机数,要求不能重复 用数组实现,但是数组的长度是固定的,长度不好确定.所以我们使用集合实现. 分析:A:创建产生随机数的对象B:创建一个存储随机数的集合C:定义一个统计变量. ...

  2. 分享一个404页面(猴子动态SVG图)

    说明 在CSDN看到的一个404界面,简洁大气非常棒我的个人网站已经用上了! 代码 防止原页面失效,代码 粘贴在下面 <!DOCTYPE html> <html lang=" ...

  3. tensorflow1.0 构建lstm做图片分类

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #this is data mni ...

  4. Java 多线程 -- 线程安全 双重检测(double checking)

    先看一个经典的12306案例: public class SynBlockTest { public static void main(String[] args) { // 一份资源 SynWeb1 ...

  5. 编程是要偷懒的--option简练写法

    没改前: if(!empty($search)){ $where['personal_name'] = array('like','%'. $search . '%'); $this -> as ...

  6. docker企业级镜像仓库Harbor管理

    Harbor概述 Harbor是由VMWare公司开源的容器镜像仓库.事实上,Harbor是在Docker Registry上进行了相应的企业级扩展,从而获得了更加广泛的应用,这些新的企业级特性包括: ...

  7. 动画图解Git命令

    ​Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理,是目前使用范围最广的版本管理工具 尽管Git是一个非常强大的工具,但我认为大多数人都会同意我的说法,即它也可以 ...

  8. Java阻塞队列四组API介绍

    Java阻塞队列四组API介绍 通过前面几篇文章的学习,我们已经知道了Java中的队列分为阻塞队列和非阻塞队列以及常用的七个阻塞队列.如下图: 本文来源:凯哥Java(kaigejava)讲解Java ...

  9. Linux hostname主机名查看和设置

    查询主机名: uname -n hostname [root@oldboy ~]# uname -n oldboy [root@oldboy ~]# hostname oldboy Linux操作系统 ...

  10. 【Linux常见命令】pwd命令

    pwd - print name of current/working directory pwd命令用于显示工作目录. 执行pwd指令可立刻得知您目前所在的工作目录的绝对路径名称. 语法:pwd [ ...