简体   繁体   English

扩展JSF commandLink组件

[英]Extending JSF commandLink component

I created a Facelet component to extend h:commandLink (to add some functionality and rounded corners). 我创建了一个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 new component can be accessed using 我的新组件可以使用

<my:commandLink id="continue" label="continue" action="#{applyBacking.submit}"/>

and the Java code is 而Java代码是

public String submit(){
    ...
}

However it gives me an error "ApplyBacking does not have the property submit". 但它给了我一个错误“ApplyBacking没有提交属性”。 I understand the reason for this error because while rendering my:commandLink, it tries to evaluate #{applyBacking.submit} to a property. 我理解这个错误的原因是因为在渲染my:commandLink时,它试图将#{applyBacking.submit}评估为属性。 Instead, I want the info about the method to the invoked (applyBacking.submit) to be passed to the template and evaluated while rendering h:commandLink. 相反,我希望将有关调用方法的信息(applyBacking.submit)传递给模板,并在渲染h:commandLink时进行评估。

Any suggestions? 有什么建议?

Create a composite component instead ( tutorial here ), it enables you to define bean actions as attribtues. 相反,创建一个复合组件此处为教程 ),它使您能够将bean操作定义为属性。

Here's a kickoff example: 这是一个启动示例:

/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>

By the way, I would personally prefer using JS/jQuery for the rounded corners part, for example the jQuery corner plugin . 顺便说一句,我个人更喜欢使用JS / jQuery作为圆角部分,例如jQuery角落插件 Just give your commandlink a specific styleClass and let JS do the magic. 只需给你的commandlink一个特定的styleClass ,让JS发挥魔力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM