javaspringstruts2tiles2wildcard-mapping

Tiles 2 Wildcard to remove duplicate not working


I'm using Spring, Struts 2, and Tiles 2 in my project and I'm trying to use wildcard notation here to remove the duplicate in my setup from here

<definition name="home-template1" template="/WEB-INF/jsp/templates/template1.jsp">
    <put-attribute name="banner" value="/WEB-INF/jsp/sitePages/banner.jsp" />
    <put-attribute name="header" value="/WEB-INF/jsp/sitePages/header.jsp" />
    <put-attribute name="body" value="/WEB-INF/jsp/sitePages/body.jsp" />
    <put-attribute name="archive" value="/WEB-INF/jsp/sitePages/archive.jsp" />
    <put-attribute name="footer" value="/WEB-INF/jsp/sitePages/footer.jsp" />
</definition>
<definition name="home-template2" template="/WEB-INF/jsp/templates/template2.jsp">
    <put-attribute name="banner" value="/WEB-INF/jsp/sitePages/banner.jsp" />
    <put-attribute name="header" value="/WEB-INF/jsp/sitePages/header.jsp" />
    <put-attribute name="body" value="/WEB-INF/jsp/sitePages/body.jsp" />
    <put-attribute name="archive" value="/WEB-INF/jsp/sitePages/archive.jsp" />
    <put-attribute name="footer" value="/WEB-INF/jsp/sitePages/footer.jsp" />
</definition>
<definition name="home-template3" template="/WEB-INF/jsp/templates/template3.jsp">
    <put-attribute name="banner" value="/WEB-INF/jsp/sitePages/banner.jsp" />
    <put-attribute name="header" value="/WEB-INF/jsp/sitePages/header.jsp" />
    <put-attribute name="body" value="/WEB-INF/jsp/sitePages/body.jsp" />
    <put-attribute name="archive" value="/WEB-INF/jsp/sitePages/archive.jsp" />
    <put-attribute name="footer" value="/WEB-INF/jsp/sitePages/footer.jsp" />
</definition>

to

<definition name="home-*" template="/WEB-INF/jsp/templates/{1}.jsp">
    <put-attribute name="banner" value="/WEB-INF/jsp/sitePages/banner.jsp" />
    <put-attribute name="header" value="/WEB-INF/jsp/sitePages/header.jsp" />
    <put-attribute name="body" value="/WEB-INF/jsp/sitePages/body.jsp" />
    <put-attribute name="archive" value="/WEB-INF/jsp/sitePages/archive.jsp" />
    <put-attribute name="footer" value="/WEB-INF/jsp/sitePages/footer.jsp" />
</definition>

but tiles gives me the following error:

org.apache.tiles.definition.NoSuchDefinitionException: home-template2

below is my struts.xml file snipped with only the two action responsible for calling it

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>    
<constant name="struts.action.extension" value=","/> 

<package name="test" extends="struts-default" namespace="/">
    
    <result-types>
        <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>
    
    <action name="checkBlogUrl" class="checkBlogUrl">
        <result name="success" type="redirectAction">
            <param name="actionName">postPreview</param>
            <param name="nextAction">${nextAction}</param>
            <param name="blogSiteUrl">${blogSiteUrl}</param>
        </result>
        <result name="error">/WEB-INF/jsp/cmsPages/index.jsp</result>
    </action>
    
    <action name="postPreview" class="blogPostAction">
        <result name="success" type="tiles">${nextAction}</result>
    </action>
    
</package>
</struts>

sample URL call:

mycms:8080/CMS/postPreview?nextAction=home-template2&blogSiteUrl=qsdcza

(Note: I removed the extension .action/.do of Struts)

also, can I use the wildcards in the extension? eg:

<definition name="about-*" extends="home-*">
    <put-attribute name="body" value="/WEB-INF/jsp/sitePages/about.jsp" />
</definition>

want to it myself try but can't make the basic wildcard work.


Solution

  • Tiles has wildcard support in versions 2.1, 2.2.

    By default, Tiles 2.2 supports wildcards in definition names. Wildcards help a lot in writing less code to declare your definitions.

    For better usage of Tiles and Struts2 see Tiles 2.2.2 Integration.

    You can use tiles definition like this

    <definition name="home*" template="/WEB-INF/jsp/templates/{1}.jsp">
        <put-attribute name="banner" value="/WEB-INF/jsp/sitePages/banner.jsp" />
        <put-attribute name="header" value="/WEB-INF/jsp/sitePages/header.jsp" />
        <put-attribute name="body" value="/WEB-INF/jsp/sitePages/body.jsp" />
        <put-attribute name="archive" value="/WEB-INF/jsp/sitePages/archive.jsp" />
        <put-attribute name="footer" value="/WEB-INF/jsp/sitePages/footer.jsp" />
    </definition>