|
62 | 62 | import org.springframework.web.context.ServletContextAware;
|
63 | 63 |
|
64 | 64 | /**
|
65 |
| - * Helper class to configure Tiles2 for the Spring Framework. See |
| 65 | + * Helper class to configure Tiles 2.x for the Spring Framework. See |
66 | 66 | * <a href="http://tiles.apache.org">http://tiles.apache.org</a>
|
67 | 67 | * for more information about Tiles, which basically is a templating
|
68 | 68 | * mechanism for JSP-based web applications.
|
69 | 69 | *
|
70 | 70 | * <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. |
73 | 73 | *
|
74 | 74 | * <p>The TilesConfigurer simply configures a TilesContainer using a set of files
|
75 | 75 | * containing definitions, to be accessed by {@link TilesView} instances. This is a
|
|
103 | 103 | */
|
104 | 104 | public class TilesConfigurer implements ServletContextAware, InitializingBean, DisposableBean {
|
105 | 105 |
|
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()); |
108 | 111 |
|
109 | 112 | private static final boolean tiles22Present = ClassUtils.isPresent(
|
110 | 113 | "org.apache.tiles.evaluator.AttributeEvaluatorFactory", TilesConfigurer.class.getClassLoader());
|
@@ -140,8 +143,8 @@ public TilesConfigurer() {
|
140 | 143 | Boolean.toString(false));
|
141 | 144 | this.tilesPropertyMap.put(DefinitionsFactory.LOCALE_RESOLVER_IMPL_PROPERTY,
|
142 | 145 | 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()); |
145 | 148 | }
|
146 | 149 |
|
147 | 150 |
|
@@ -308,25 +311,12 @@ public void afterPropertiesSet() throws TilesException {
|
308 | 311 | }
|
309 | 312 | }
|
310 | 313 |
|
311 |
| - if (jsp21Present && this.tilesInitializer instanceof SpringTilesInitializer) { |
| 314 | + if (tilesElPresent && this.tilesInitializer instanceof SpringTilesInitializer) { |
312 | 315 | // Again, we need to do this after initialization since SpringTilesContainerFactory
|
313 | 316 | // cannot override template methods that refer to Tiles 2.2 classes: in this case,
|
314 | 317 | // 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); |
330 | 320 | }
|
331 | 321 | }
|
332 | 322 |
|
@@ -432,4 +422,26 @@ protected PreparerFactory createPreparerFactory(TilesApplicationContext applicat
|
432 | 422 | }
|
433 | 423 | }
|
434 | 424 |
|
| 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 | + |
435 | 447 | }
|
0 commit comments