You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tiles.apache.org by Eric B <eb...@hotmail.com> on 2013/10/29 16:45:55 UTC

Cascading preference when a view name matches several templates

Given multiple definitions in the tiles.xml file or tiles*.xml files that may be scattered throughout the classpath, how does tiles.xml decide which 
definition(s) to use?

For example, given the following view name:

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(ModelMap model) {
		return "secure/login";
	}


and the following tiles*.xml files:


tiles.xml
<tiles-definitions>

<definition name="REGEXP:([^.]+)" template="/WEB-INF/views/tiles/layouts/default.jsp">
     <put-attribute name="meta" value="/WEB-INF/views/tiles/{options[defaultPaths]}/meta.jsp"/>
     <put-attribute name="head" value="/WEB-INF/views/tiles/{options[defaultPaths]}/head.jsp"/>
     <put-attribute name="header" value="/WEB-INF/views/tiles/{options[defaultPaths]}/header.jsp"/>
     <put-attribute name="menu" value="/WEB-INF/views/tiles/{options[defaultPaths]}/menu.jsp"/>
     <put-attribute name="body" value="/WEB-INF/views/tiles/{options[defaultPaths]}/body.jsp"/>
     <put-attribute name="footer" value="/WEB-INF/views/tiles/{options[defaultPaths]}/footer.jsp"/>

	<put-list-attribute name="defaultPaths" cascade="true" >
		<add-attribute value="{1}"/>
		<add-attribute value="common"/>
	</put-list-attribute>
</definition>


<definition name="REGEXP:secure/(.*)" template="/WEB-INF/views/tiles/layouts/secure.jsp">
     <put-attribute name="body" value="/WEB-INF/views/tiles/secure/{1}/body.jsp"/>
</definition>

</tiles-definitions>


tiles-login.xml:


<tiles-definitions>
<definition name="secure/login" template="/WEB-INF/views/tiles/secure/layout/secure.jsp">
     <put-attribute name="body" value="/WEB-INF/views/tiles/secure/login/body.jsp"/>
     <put-attribute name="captcha" value="/WEB-INF/views/tiles/secure/login/captcha.jsp"/>
</definition>

</tiles-definitions>



How does tiles know which definition to use?  Is there a precedence order?  Is it based on the order in which the tile*.xml files are registered/loaded?

Thanks,

Eric



Re: Cascading preference when a view name matches several templates

Posted by Mick Semb Wever <mc...@apache.org>.
ClamAV 0.96.5

> >> How does tiles know which definition to use?  Is there a precedence
> >> order?  Is it based on the order in which the tile*.xml files are
> >> registered/loaded?
> >
> > Without double-checking the code my memory from experience tells me that
> > the first definition found (as files are loaded and as found in the
> > file) is the definition that is used.
> 
> [snip], but I would have loved to have a feature that uses "closest
> match" algorithm as opposed to a "first-defn found".


One way to solve this is to have a custom
PrefixedPatternDefinitionResolver overriding
AbstractPatternDefinitionResolver.addDefinitionsAsPatternMatchers(..)

so that the loop over the defsMap happens in the order you want.

And return this custom PrefixedPatternDefinitionResolver from your
TilesContainerFactory.createPatternDefinitionResolver(..)


I guess the closest match strategy could be done using
StringUtils.getLevenshteinDistance(..)


> I'm surprised that no-one has seen the need for something like this
> before.

I guess just adjusting the order of the definitions in the tiles xml has
been good enough for everyone.

But something like described above, a la an
EditDistanceOrderedPrefixedPatternDefinitionResolver, has value and i'd
vote for accepting it into the tiles.

~mck



-- 
Nature does not hurry, yet everything is accomplished. - Lao Tzu 

| http://github.com/finn-no | http://tech.finn.no |


Re: Cascading preference when a view name matches several templates

Posted by Eric B <eb...@hotmail.com>.
On 13-11-03 11:09 AM, Mick Semb Wever wrote:
> ClamAV 0.96.5
>
> On Tue, 2013-10-29 at 11:45 -0400, Eric B wrote:
>> How does tiles know which definition to use?  Is there a precedence
>> order?  Is it based on the order in which the tile*.xml files are
>> registered/loaded?
>
> Without double-checking the code my memory from experience tells me that
> the first definition found (as files are loaded and as found in the
> file) is the definition that is used.

That sounds similar to what I remember reading in the code, and is understandable, but I would have loved to have a feature that uses "closest match" 
algorithm as opposed to a "first-defn found".

For example:
defn= "REGEXP: ^some.+\.jsp"
vs
defn="something.jsp"

I realize that this opens a quagmire of how to determine which is the closest match (ex: is REGEXP: ^some.+\.jsp" a closer match than WILDCARD: 
some*.jsp).  In which case, maybe a weighting or priority option, such that if I have one regex that is more explicit, to use it before using one that 
is less explicit.  If I remember correctly, non-pattern based expressions are checked first, but otherwise, it is somewhat pseudo-random order.

I'm surprised that no-one has seen the need for something like this before.

Thanks,

Eric



Re: Cascading preference when a view name matches several templates

Posted by Mick Semb Wever <mc...@apache.org>.
ClamAV 0.96.5

On Tue, 2013-10-29 at 11:45 -0400, Eric B wrote:
> How does tiles know which definition to use?  Is there a precedence
> order?  Is it based on the order in which the tile*.xml files are
> registered/loaded?

Without double-checking the code my memory from experience tells me that
the first definition found (as files are loaded and as found in the
file) is the definition that is used.

~mck

-- 
"Only two things are infinite, the universe and human stupidity, and I'm
not sure about the former." Albert Einstein 

| http://github.com/finn-no | http://tech.finn.no |