With Tiles it is possible to use EL not only in JSP pages, but also in XML definition files.
To enable EL support in XML definition files, you have to configure the class of the AttributeEvaluator.
If you are using declarative configuration in web.xml file, you have to use the org.apache.tiles.evaluator.AttributeEvaluator parameter. In the case of TilesServlet:
<servlet> <servlet-name>tiles-init</servlet-name> <servlet-class>org.apache.tiles.web.startup.TilesServlet</servlet-class> <init-param> <param-name>org.apache.tiles.evaluator.AttributeEvaluator</param-name> <param-value>org.apache.tiles.evaluator.el.ELAttributeEvaluatr</param-value> </init-param> </servlet>
If you are using programmatic configuration via Java, override the createEvaluator method, providing only the creation of the instance of the AttributeEvaluator.
If you are using Servlet 2.5 or above, you can leverage your container EL support.
/** {@inheritDoc} */ @Override protected AttributeEvaluator createEvaluator(Object context, TilesApplicationContext applicationContext, TilesRequestContextFactory contextFactory, LocaleResolver resolver) { ELAttributeEvaluator evaluator = new ELAttributeEvaluator(); evaluator.setApplicationContext(applicationContext); MultiversionExpressionFactoryFactory efFactory = new JspExpressionFactoryFactory(); efFactory.setApplicationContext(applicationContext); evaluator.setExpressionFactory(efFactory.getExpressionFactory()); ELResolver elResolver = new CompositeELResolver() { { add(new TilesContextELResolver()); add(new TilesContextBeanELResolver()); add(new ArrayELResolver(false)); add(new ListELResolver(false)); add(new MapELResolver(false)); add(new ResourceBundleELResolver()); } }; evaluator.setResolver(elResolver); return evaluator; }
If you are using Servlet 2.4, you still can use Tiles EL support but:
Let's use this example:
<definition name="test.composite.el.definition" templateExpression="${layout}" preparer="org.apache.tiles.test.preparer.RequestSettingViewPreparer"> <put-attribute name="title" value="This is a configured composite definition."/> <put-attribute name="header" value="/header.jsp"/> <put-attribute name="body" expression="${requestScope.body}"/> </definition>
Before rendering the definition: