Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending JSF commandLink component

Tags:

jsf

jsf-2

I created a Facelet component to extend h:commandLink (to add some functionality and rounded corners).

<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

public String submit(){
    ...
}

However it gives me an error "ApplyBacking does not have the property submit". I understand the reason for this error because while rendering my:commandLink, it tries to evaluate #{applyBacking.submit} to a property. 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.

Any suggestions?

like image 979
user552809 Avatar asked Jan 05 '11 18:01

user552809


1 Answers

Create a composite component instead (tutorial here), it enables you to define bean actions as attribtues.

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. Just give your commandlink a specific styleClass and let JS do the magic.

like image 166
BalusC Avatar answered Nov 16 '22 12:11

BalusC