Tiles2 Wildcard not working

风流意气都作罢 提交于 2021-02-04 21:28:10

问题


I'm using Spring-Struts2-Tiles2 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 :(

Thank you for reading and I appreciate your help.


回答1:


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>


来源:https://stackoverflow.com/questions/36831491/tiles2-wildcard-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!