On 11-11-23 05:45 PM, Pascual Pereda wrote:
Hi,
I'm using tiles 2.2 with Spring MVC. I use Expression Language in my
tiles-defs.xml file, so it's full of definitions like this:
<definition name="test" extends="base">
<put-attribute name="body" expression="/view/${variable.property ? 'foo' :
'bar'}/test.jspx"/>
</definition>
As the same big expression is replicated across all my definitions, I was
wondering if there was some way to define it only once, and use a simpler
expression like ${folder} instead of the complicated one.
Thanks in advance
Hi,
I don't know any expession language that will allow you to declare
variables, and I don't think you can use tiles attributes directly (I
think Antonio will correct me if I'm wrong)
But since this is decision logic, I think tiles-defs.xml is not the
right place for it. What if you need to decide something more
complicated than "foo" or "bar" in the future?
I would make the decision in the controller, or if you really don't want
to, in a ViewPreparer like this (see also
http://tiles.apache.org/2.2/framework/tutorial/advanced/preparer.html):
public class ThemePreparer implements ViewPreparer {
public void execute(TilesRequestContext tilesContext,
AttributeContext attributeContext) {
MyVariable variable = (MyVariable) tilesContext
.getRequestScope().get("variable");
String theme = variable.getProperty() ? "foo" : "bar";
tilesContext.getRequestScope().put("theme", theme);
}
}
<definition name="base" preparer="org.example.ThemePreparer" ... >
...
</definition>
<definition name="test" extends="base">
<put-attribute name="body" expression="/view/${theme}/test.jspx"/>
</definition>
Hope this helps.
Nick