Skip to content

Commit 16aa399

Browse files
committed
optional EL support (only when Tiles EL module present); got rid of tiles-servlet-wildcard dependency (implemented wildcard support locally)
1 parent 3db5a29 commit 16aa399

File tree

2 files changed

+71
-25
lines changed

2 files changed

+71
-25
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/SpringTilesApplicationContextFactory.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@
1616

1717
package org.springframework.web.servlet.view.tiles2;
1818

19+
import java.io.IOException;
20+
import java.net.URL;
1921
import java.util.Enumeration;
22+
import java.util.HashSet;
2023
import java.util.LinkedHashMap;
2124
import java.util.Map;
25+
import java.util.Set;
2226
import javax.servlet.ServletContext;
2327

2428
import org.apache.tiles.Initializable;
2529
import org.apache.tiles.TilesApplicationContext;
2630
import org.apache.tiles.context.AbstractTilesApplicationContextFactory;
27-
import org.apache.tiles.servlet.context.wildcard.WildcardServletTilesApplicationContext;
31+
import org.apache.tiles.servlet.context.ServletTilesApplicationContext;
32+
33+
import org.springframework.core.io.Resource;
34+
import org.springframework.core.io.support.ResourcePatternResolver;
35+
import org.springframework.web.context.support.ServletContextResourcePatternResolver;
2836

2937
/**
3038
* Spring-specific subclass of the standard Tiles AbstractTilesApplicationContextFactory,
@@ -52,10 +60,12 @@ public TilesApplicationContext createApplicationContext(Object context) {
5260
* Custom subclass of the standard Tiles WildcardServletTilesApplicationContext,
5361
* passing given properties through as Tiles init-param map.
5462
*/
55-
private static class SpringWildcardServletTilesApplicationContext extends WildcardServletTilesApplicationContext {
63+
private static class SpringWildcardServletTilesApplicationContext extends ServletTilesApplicationContext {
5664

5765
private final Map<String, String> mergedInitParams;
5866

67+
private final ResourcePatternResolver resolver;
68+
5969
public SpringWildcardServletTilesApplicationContext(ServletContext servletContext, Map<String, String> params) {
6070
super(servletContext);
6171
this.mergedInitParams = new LinkedHashMap<String, String>();
@@ -67,12 +77,36 @@ public SpringWildcardServletTilesApplicationContext(ServletContext servletContex
6777
if (params != null) {
6878
this.mergedInitParams.putAll(params);
6979
}
80+
this.resolver = new ServletContextResourcePatternResolver(servletContext);
7081
}
7182

7283
@Override
7384
public Map<String, String> getInitParams() {
7485
return this.mergedInitParams;
7586
}
87+
88+
@Override
89+
public URL getResource(String path) throws IOException {
90+
URL retValue = null;
91+
Set<URL> urlSet = getResources(path);
92+
if (urlSet != null && !urlSet.isEmpty()) {
93+
retValue = urlSet.iterator().next();
94+
}
95+
return retValue;
96+
}
97+
98+
@Override
99+
public Set<URL> getResources(String path) throws IOException {
100+
Set<URL> urlSet = null;
101+
Resource[] resources = this.resolver.getResources(path);
102+
if (resources != null && resources.length > 0) {
103+
urlSet = new HashSet<URL>();
104+
for (Resource resource : resources) {
105+
urlSet.add(resource.getURL());
106+
}
107+
}
108+
return urlSet;
109+
}
76110
}
77111

78112
}

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/TilesConfigurer.java

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@
6262
import org.springframework.web.context.ServletContextAware;
6363

6464
/**
65-
* Helper class to configure Tiles2 for the Spring Framework. See
65+
* Helper class to configure Tiles 2.x for the Spring Framework. See
6666
* <a href="http://tiles.apache.org">http://tiles.apache.org</a>
6767
* for more information about Tiles, which basically is a templating
6868
* mechanism for JSP-based web applications.
6969
*
7070
* <b>Note: Spring 3.0 requires Tiles 2.1.2 or above, with explicit support for Tiles 2.2.</b>
71-
* Tiles 2.1's EL support will be activated by default when running on JSP 2.1 or above.
72-
* Note that EL support is <i>not</> active by default when running against Tiles 2.2.
71+
* Tiles 2.1's EL support will be activated by default when running on JSP 2.1 or above
72+
* and when the Tiles EL module is present in the classpath.
7373
*
7474
* <p>The TilesConfigurer simply configures a TilesContainer using a set of files
7575
* containing definitions, to be accessed by {@link TilesView} instances. This is a
@@ -103,8 +103,11 @@
103103
*/
104104
public class TilesConfigurer implements ServletContextAware, InitializingBean, DisposableBean {
105105

106-
private static final boolean jsp21Present = ClassUtils.isPresent(
107-
"javax.servlet.jsp.JspApplicationContext", TilesConfigurer.class.getClassLoader());
106+
private static final boolean tilesElPresent = // requires JSP 2.1 as well as Tiles EL module
107+
ClassUtils.isPresent(
108+
"javax.servlet.jsp.JspApplicationContext", TilesConfigurer.class.getClassLoader()) &&
109+
ClassUtils.isPresent(
110+
"org.apache.tiles.evaluator.el.ELAttributeEvaluator", TilesConfigurer.class.getClassLoader());
108111

109112
private static final boolean tiles22Present = ClassUtils.isPresent(
110113
"org.apache.tiles.evaluator.AttributeEvaluatorFactory", TilesConfigurer.class.getClassLoader());
@@ -140,8 +143,8 @@ public TilesConfigurer() {
140143
Boolean.toString(false));
141144
this.tilesPropertyMap.put(DefinitionsFactory.LOCALE_RESOLVER_IMPL_PROPERTY,
142145
SpringLocaleResolver.class.getName());
143-
this.tilesPropertyMap.put(TilesContainerFactory.ATTRIBUTE_EVALUATOR_INIT_PARAM,
144-
jsp21Present ? ELAttributeEvaluator.class.getName() : DirectAttributeEvaluator.class.getName());
146+
this.tilesPropertyMap.put(TilesContainerFactory.ATTRIBUTE_EVALUATOR_INIT_PARAM, tilesElPresent ?
147+
"org.apache.tiles.evaluator.el.ELAttributeEvaluator" : DirectAttributeEvaluator.class.getName());
145148
}
146149

147150

@@ -308,25 +311,12 @@ public void afterPropertiesSet() throws TilesException {
308311
}
309312
}
310313

311-
if (jsp21Present && this.tilesInitializer instanceof SpringTilesInitializer) {
314+
if (tilesElPresent && this.tilesInitializer instanceof SpringTilesInitializer) {
312315
// Again, we need to do this after initialization since SpringTilesContainerFactory
313316
// cannot override template methods that refer to Tiles 2.2 classes: in this case,
314317
// AttributeEvaluatorFactory as createAttributeEvaluatorFactory return type.
315-
try {
316-
BasicTilesContainer container = (BasicTilesContainer) ServletUtil.getContainer(this.servletContext);
317-
Class aef = getClass().getClassLoader().loadClass("org.apache.tiles.evaluator.AttributeEvaluatorFactory");
318-
Class baef = getClass().getClassLoader().loadClass("org.apache.tiles.evaluator.BasicAttributeEvaluatorFactory");
319-
Constructor baefCtor = baef.getConstructor(AttributeEvaluator.class);
320-
ELAttributeEvaluator evaluator = new ELAttributeEvaluator();
321-
evaluator.setApplicationContext(container.getApplicationContext());
322-
evaluator.init(new HashMap<String, String>());
323-
Object baefValue = baefCtor.newInstance(evaluator);
324-
Method setter = container.getClass().getMethod("setAttributeEvaluatorFactory", aef);
325-
setter.invoke(container, baefValue);
326-
}
327-
catch (Exception ex) {
328-
throw new IllegalStateException("Cannot activate ELAttributeEvaluator", ex);
329-
}
318+
BasicTilesContainer container = (BasicTilesContainer) ServletUtil.getContainer(this.servletContext);
319+
TilesElActivator.registerEvaluator(container);
330320
}
331321
}
332322

@@ -432,4 +422,26 @@ protected PreparerFactory createPreparerFactory(TilesApplicationContext applicat
432422
}
433423
}
434424

425+
426+
private static class TilesElActivator {
427+
428+
public static void registerEvaluator(BasicTilesContainer container) {
429+
try {
430+
ClassLoader cl = TilesElActivator.class.getClassLoader();
431+
Class aef = cl.loadClass("org.apache.tiles.evaluator.AttributeEvaluatorFactory");
432+
Class baef = cl.loadClass("org.apache.tiles.evaluator.BasicAttributeEvaluatorFactory");
433+
Constructor baefCtor = baef.getConstructor(AttributeEvaluator.class);
434+
ELAttributeEvaluator evaluator = new ELAttributeEvaluator();
435+
evaluator.setApplicationContext(container.getApplicationContext());
436+
evaluator.init(new HashMap<String, String>());
437+
Object baefValue = baefCtor.newInstance(evaluator);
438+
Method setter = container.getClass().getMethod("setAttributeEvaluatorFactory", aef);
439+
setter.invoke(container, baefValue);
440+
}
441+
catch (Exception ex) {
442+
throw new IllegalStateException("Cannot activate ELAttributeEvaluator", ex);
443+
}
444+
}
445+
}
446+
435447
}

0 commit comments

Comments
 (0)