3
votes

I am trying to create composite components. I defined 4 attributes in composite:interface section. Here is code

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:composite="http://java.sun.com/jsf/composite">

<composite:interface >
    <composite:attribute name="id" />
    <composite:attribute name="maxlength" />
    <composite:attribute name="required"/>
    <composite:attribute name="value" />
</composite:interface>

<composite:implementation  xmlns:kc="http://java.sun.com/jsf/composite/components/kaysComposite">
        <p:inputTextArea id="#{cc.attrs.id}" value="#{cc.attrs.value}" maxlength="#{cc.attrs.maxlength}" required="#{cc.attrs.required}" counterTemplate="{0} /  #{cc.attrs.maxlength}" counter="#{cc.attrs.id}_counter"/>

        <h:outputText id="#{cc.attrs.id}_counter"/>
</composite:implementation>
</html>

This is the page i use my component

     <kc:kaysInputTextArea id="gpAdres" value="#{someBean.variable}"   maxlength="250" required="true"/>
<p:message for="gpAdres" />

The weird part is required attribute doesn't work, but others work fine. I couldn't find why it is behaving like this.

4

4 Answers

4
votes

(Not a true answer, but too long for a comment. Just wanted to share some ideas which might help... please edit or replace if appropriate)

You did not describe the behavior you encountered, so I'm guessing the value inside the component does not change with the value you pass in.

I had a similar problem with the same setup, but when I passed in "true" or "false" directly (as your example does), it worked. Only if I handed over an EL expression, the value inside the component is not set anymore, regardless of what the expression evaluates to. In my case I had an explicit type set on the attribute, e.g. type="java.lang.Boolean" Removing this definition did the trick.

My guess is that when forcing the attribute to expect boolean it can't handle an EL and resolves it to the default value of Boolean (which seems to be true, unless default="false" is set).

By not setting a type it seems the component can retain the EL and passes it on to the next target, e.g. the rendered attribute of whatever h:tag.

Does not seem to be your exact problem, but maybe it helps in tracing the issue?

0
votes

You can use this approach.

<kc:kaysInputTextArea id="gpAdres" value="#{someBean.variable}"   maxlength="250" required="true" rendered="#{yourBooleanExpression}"/>
<kc:kaysInputTextArea id="gpAdres" value="#{someBean.variable}"   maxlength="250" required="false" rendered="#{!yourBooleanExpression}"/>

This is hack, but work )

0
votes

I also experienced strange behavior of components once. It turned out that there was a problem with the value id in

<composite:attribute name="id" />

So try out renaming the attribute to ident. Maybe other commonly named attributes like required or value are also a problem...

The concrete, even more funny situation I had was the following:

<composite:attribute name="id" requred="true" />

worked. Notice the typo in requred. When I fixed the typo, the component no longer worked, complaining that I didn't specify a value for as required marked id attribute, although I did provide a value for that. The solution was to rename the composite attribute:

<composite:attribute name="ident" required="true" />
0
votes

just try using another attribute: id is reserved for the composite tag so you should try this:

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:composite="http://java.sun.com/jsf/composite">

<composite:interface >
    <composite:attribute name="inputTextId" />
    <composite:attribute name="maxlength" />
    <composite:attribute name="required"/>
    <composite:attribute name="value" />
</composite:interface>

<composite:implementation  xmlns:kc="http://java.sun.com/jsf/composite/components/kaysComposite">
        <p:inputTextArea id="#{cc.attrs.inputTextId}" value="#{cc.attrs.value}" maxlength="#{cc.attrs.maxlength}" required="#{cc.attrs.required}" counterTemplate="{0} /  #{cc.attrs.maxlength}" counter="#{cc.attrs.id}_counter"/>

        <h:outputText id="#{cc.attrs.inputTextId}_counter"/>
</composite:implementation>
</html>