I have a Spring MVC project that uses Apache Tiles. I have implemented so that titles can be read from a message source like this:
<tiles-definitions>
<definition name="some-definition" extends="public.base">
<put-attribute name="title" value="some-definition.title" cascade="true" />
</definition>
</tiles-definitions>
And in my template file (defined by public.base
), I do the following:
<title><spring:message text="" code="${title}" /></title>
Now this works great for static translated titles, but I also want to support dynamic titles, e.g. for displaying a company name. I could do it like this:
<tiles-definitions>
<definition name="some-definition" extends="public.base">
<put-attribute name="title" expression="${company.name}" />
</definition>
</tiles-definitions>
And then simply output the title in my template like this: <c:out value="${title}" />
. However, the problem then is that my code breaks because the value of the title
attribute is no longer a message key. I want to be able to support the following scenarios:
- Static titles, e.g. "About Us"
- Purely dynamic titles, e.g. "${company.name}"
- Dynamic titles with translated content, e.g. "Welcome to ${company.name}"
Ideally, I could use expression language within my message source, but I could not get that to work. I have experimented quite a bit with various solutions, but I cannot seem to find a decent one. If I could use expression language within my message source, then this would be easy. For instance, would it be possible to do the following somehow?
some-definition.title = Hello there, ${company.name}
And in my template:
<spring:message text="" code="some-definition.title" var="test" />
<c:out value="${test}" />
The above does not work, as it outputs ${company.name}
rather than the actual contents of the variable. Is there a way to make something like this work? Or are there any other ways in which I can support the scenarios that I listed above?
I thought about creating a custom JSTL tag where I would parse a string expression in plain Java code (the string that has been translated), but I realized that I would probably have to explicitly specify the root object for the "variable replacement" to work, as documented here. Then it does not seem like such a dynamic solution.
Is there any way in which I can accomplish this task? Any help is much appreciated!