Tiles 3如何在put-attribute中引用另一个定义 [英] Tiles 3 how to reference another definition in put-attribute

查看:119
本文介绍了Tiles 3如何在put-attribute中引用另一个定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够定义一个基本定义,在这里我可以继承样式和脚本的列表。然后定义一个继承基本定义的页面定义,并添加页面特定的样式和脚本。这可能吗?或者我是否没有以正确的方式考虑?我本以为这是一个基本的想法。

I'd like to be able to define a base definition, where I can inherit a list of styles and scripts. then define a page definition that inherits base definition, and adds page specific styles and scripts. Is this possible -or am I not thinking about this in the right way? I would have thought this to be a fairly basic idea.

基本定义

<tiles-definitions>
    <!-- base styles -->
    <definition name="base.styles" >
        <put-list-attribute name="styles" cascade="true" >
            <add-attribute value="/view/common/jquery-ui-theme-base-v1.12.1.css" />
        </put-list-attribute>
    </definition>
    <!-- base scripts -->
    <definition name="base.scripts" >
        <put-list-attribute name="scripts" cascade="true" >
            <add-attribute value="https://code.jquery.com/jquery-3.1.0.min.js" />
            <add-attribute value="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" />
        </put-list-attribute>
    </definition>
    <!-- base definition -->
    <definition name="base.definition" template="/WEB-INF/page/defaultLayout.jsp" >
        <put-attribute name="title" />
        <put-attribute name="styles" value="base.styles.styles" cascade="true" />
        <put-attribute name="header" value="/WEB-INF/page/common/header.jsp" />
        <put-attribute name="body" />
        <put-attribute name="scripts" value="base.scripts.scripts" cascade="true" />
        <put-attribute name="footer" value="/WEB-INF/page/common/footer.jsp" />
    </definition>
</tiles-definitions>

请注意放置属性的值与它们上方定义的名称匹配。 (我猜这是不正确的)

页面特定定义

<tiles-definitions>
    <!-- page specific styles -->
    <definition name="samplePage.styles" extends="base.styles" >
        <put-list-attribute name="styles" inherit="true" >
            <add-attribute value="/view/page/samplePage/samplePageStyles.css" />
        </put-list-attribute>
    </definition>
    <!-- page specific scripts -->
    <definition name="samplePage.scripts" extends="base.scripts" >
        <put-list-attribute name="scripts" inherit="true" >
            <add-attribute value="/view/page/samplePage/samplePageScript.js" />
        </put-list-attribute>
    </definition>
    <!-- page specific definition -->
    <definition name="samplePage" extends="base.definition" >
        <put-attribute name="title" value="Sample Page" />
        <put-attribute name="styles" value="samplePage.styles" cascade="true" />
        <put-attribute name="body" value="/WEB-INF/page/samplePage/samplePageBody" />
        <put-attribute name="scripts" value="samplePage.scripts" cascade="true" />
    </definition>
</tiles-definitions>

-注意put-attribute的值与它们上面定义的名称匹配。 (可能不正确吗?)

我目前收到IllegalArgumentException

I'm currently getting an IllegalArgumentException

Caused by: java.lang.IllegalArgumentException: Cannot convert samplePage.styles of type class java.lang.String to class org.apache.tiles.Attribute
at com.sun.el.lang.ELSupport.coerceToType(ELSupport.java:428)
at com.sun.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:85)
... 104 more

似乎样式和脚本的put-attributes并未在它们的上方使用相同名称的定义...但是我我不确定该如何纠正。有想法吗?

It appears as if the put-attributes for the styles and scripts are not picking up the definitions of the same name above them... but I'm not sure what to do to correct it. Any ideas?

推荐答案

要回答问题如何在put-attribute中引用另一个定义,使用以下jsp组件似乎相当容易(注意定义和'type'属性):

To answer the question how to reference another definition in put-attribute, this appears to be fairly easy with jsp components such as the following (note the definition, and the 'type' attribute):

<definition name="default.header" template="/WEB-INF/page/common/header.jsp" />

...然后在其他地方...

... then elsewhere...

<put-attribute name="header" type="definition" value="default.header" />

但是,使用样式表和脚本之类的资源似乎无法做到这一点,因为他们不会转换回实际的模板。因此,我对基础定义进行了如下修改,这仍然使我可以拥有全局资源和特定于页面的资源。还允许我编写一次jsp组件,定义一次,并多次引用它们。

However, it doesn't look like this can be done with resources such as stylesheets and scripts, as they don't translate back to an actual template. So, I reworked my base-definition as follows, which still allows me to have 'global' resources, as well as 'page-specific' resources. Also allows me to write the jsp components once, define them once, and reference them many times.

常用定义(tiles.common.xml)

<tiles-definitions>
    <definition name="default.header" template="/WEB-INF/page/common/header.jsp" />
    <definition name="default.footer" template="/WEB-INF/page/common/footer.jsp" />

    [... all the other common components...]

</tiles-definitions>

基本定义(tiles.base.xml)

<tiles-definitions>
    <definition name="base.definition" template="/WEB-INF/page/baseLayout.jsp" >
        <put-attribute name="title" type="string" />
        <put-attribute name="header" type="definition" value="default.header" />
        <put-attribute name="body" />
        <put-attribute name="footer" type="definition" value="default.footer" />
        <put-list-attribute name="styles" inherit="true" >
            <add-attribute value="/view/common/jquery-ui-theme-base-v1.12.1.css" />

            [... other styles common to all pages...]

        </put-list-attribute>
        <put-list-attribute name="scripts" inherit="true" >
            <add-attribute value="https://code.jquery.com/jquery-3.1.0.min.js" />
            <add-attribute value="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" />

            [... other scripts common to all pages...]

        </put-list-attribute>
    </definition>
</tiles-definition>

页面特定定义(tiles.samplePage.xml)

<tiles-definitions>
    <definition name="samplePage" extends="base.definition" >
        <put-attribute name="title" value="Sample Page" />
        <put-attribute name="body" value="/WEB-INF/page/samplePage.jsp" />
        <put-list-attribute name="styles" inherit="true" >
            <add-attribute value="/view/pages/samplePage.css" />

            [... other page specific styles ...]

        </put-list-attribute>
        <put-list-attribute name="scripts" inherit="true" >
            <add-attribute value="/view/pages/samplePage.js" />

            [... other page specific scripts ...]

        </put-list-attribute>
    </definition>
</tiles-definition>

这篇关于Tiles 3如何在put-attribute中引用另一个定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆