【问题标题】:Extending JSF commandLink component扩展 JSF commandLink 组件
【发布时间】:2011-06-04 05:29:21
【问题描述】:

我创建了一个 Facelet 组件来扩展 h:commandLink(以添加一些功能和圆角)。

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
    <span class="btn-left btn-corners">&#160;</span>
    <span type="submit" class="submit">
        <h:commandLink id="#{id}" value="#{label}" action="#{action}" />
    </span>
    <span class="btn-right btn-corners">&#160;</span> </ui:composition>

可以使用

访问我的新组件
<my:commandLink id="continue" label="continue" action="#{applyBacking.submit}"/>

Java 代码是

public String submit(){
    ...
}

但是它给了我一个错误“ApplyBacking 没有属性提交”。 我理解此错误的原因,因为在呈现 my:commandLink 时,它会尝试将 #{applyBacking.submit} 评估为属性。相反,我希望将有关被调用 (applyBacking.submit) 方法的信息传递给模板并在渲染 h:commandLink 时进行评估。

有什么建议吗?

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

    改为创建composite component (tutorial here),它使您能够将 bean 操作定义为属性。

    这是一个启动示例:

    /resources/components/commandLink.xhtml

    <ui:component
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:cc="http://java.sun.com/jsf/composite">
        <cc:interface>
            <cc:attribute name="id" required="true" />
            <cc:attribute name="label" required="true" />
            <cc:attribute name="action" method-signature="java.lang.String action()" required="true" />
        </cc:interface>
        <cc:implementation>
            <span class="btn-left btn-corners">&#160;</span>
            <span type="submit" class="submit">
                <h:commandLink id="#{cc.attrs.id}" value="#{cc.attrs.label}" action="#{cc.attrs.action}" />
            </span>
            <span class="btn-right btn-corners">&#160;</span>
        </cc:implementation>
    </ui:component>
    

    /somepage.xhtml

    <!DOCTYPE html>
    <html lang="en"
        xmlns="http://www.w3.org/1999/xhtml" 
        xmlns:h="http://java.sun.com/jsf/html" 
        xmlns:my="http://java.sun.com/jsf/composite/components">
        <h:head>
            <title>SO question 4608030</title>
        </h:head>
        <h:body>
            <h:form>
                <my:commandLink id="continue" label="continue" action="#{applyBacking.submit}"/>
            </h:form>
        </h:body>
    </html>
    

    顺便说一句,我个人更喜欢将 JS/jQuery 用于圆角部分,例如 jQuery corner plugin。只需给你的命令链接一个特定的styleClass 并让 JS 发挥作用。

    【讨论】:

      猜你喜欢
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 2023-03-28
      • 2023-03-27
      • 2012-08-04
      相关资源
      最近更新 更多