Question

I search for ideas on how to reduce some of the boilerplate added by Tiles. Here is how my project organized.

In my spring-mvc config I have Tiles configured this way:

...

<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>

<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/layouts/layouts.xml</value>
            <!-- Scan views directory for Tiles configurations -->
            <value>/WEB-INF/views/**/views.xml</value>
        </list>
    </property>
</bean>

...

Main layout I use defined in /WEB-INF/layouts/layouts.xml:

...

<definition name="default" template="/WEB-INF/layouts/default.jspx">
    <put-attribute name="header" value="/WEB-INF/views/general/header.jspx" />
    <put-attribute name="footer" value="/WEB-INF/views/general/footer.jspx" />
</definition>

...

... and the template that is used (/WEB-INF/layouts/default.jspx) looks similar to this:

...

<tiles:insertAttribute name="header" ignore="true" />
    <div id="main">
        <tiles:insertAttribute name="body" />
    </div>
<tiles:insertAttribute name="footer" ignore="true" />

...

I as you might guess from spring config the actual view definitions are located in "/WEB-INF/views"

I also have some subfolders under "/WEB-INF/views" to keep my views organised.

In general case to add view I have to:

  1. add template located at "/WEB-INF/views/{category}/{viewname}.jspx"

  2. update /WEB-INF/views/{category}/views.xml by adding new definition like:

    ...
    
    <definition extends="default" name="{category}/{viewname}">
        <put-attribute name="body" value="/WEB-INF/views/{category}/{viewname}.jspx"/>
    </definition>
    

As you can see the only attribute I put in most of my definitions is "body" and it's value corellates with location of the template and view name.

Over time it gets harder to refactor/maintain those views, so I am searching some sort of simplification for defining them.

So my question sounds like this: is it possible to make tiles discover view definitions based on template name/location? The definition should use relevant jspx as "body" (or any other attribure) and have corresponding name.

For example, having jsps placed: /WEB-INF/views/general/login.jspx

Something like this should be defined by tiles automatically:

<tiles-definitions>

    ...

    <definition extends="default" name="general/login">
        <put-attribute name="body" value="/WEB-INF/views/general/login.jspx"/>
    </definition>

</tiles-definitions>
Was it helpful?

Solution

This boilerplate configuration is a hang-up from tiles-1 days. It really isn't necessary with tiles-2 when wildcards were introduced, and especially with tiles-3 along with the OptionsRenderer.

Here's a tutorial that will help you with

  • spring to tiles integration,
  • definitions with wildcards,
  • implementing a fallback pattern using the OptionsRenderer, and
  • definitions composition.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top