Conditionally attribute of an attribute element in a composite component

I have the following composite component:

<?xml version="1.0" encoding="UTF-8"?> <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:c="http://java.sun.com/jsp/jstl/core" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:composite="http://java.sun.com/jsf/composite"> <composite:interface> <composite:attribute required="true" name="field" /> <composite:attribute required="true" name="value" /> <composite:attribute required="false" name="size"/> </composite:interface> <composite:implementation> ... <div class="wrapper"> <h:inputText value="#{cc.attrs.value}" id="#{field.id}" rendered="#{field.rendered}" size="#{cc.attrs.size}"> </h:inputText> <h:messages for="#{field.id}" styleClass="errorMessage"/> </div> ... </composite:implementation> </ui:component> 

The problem is that when I use this component without setting its size attribute, it still displays as size=0 in the html input element.

I want to display the nested attribute h:inputText only if it has a valid value (for example, not empty). In addition, I would like to show all attributes of a nested element if they are not explicitly redefined.

How is this possible?

+6
source share
3 answers

You can use JSTL <c:if> to conditionally create a view and <f:attribute> to specify an attribute separately:

 <h:inputText ...> <c:if test="#{not empty cc.attrs.size}"> <f:attribute name="size" value="#{cc.attrs.size}" /> </c:if> </h:inputText> 

An alternative is to specify a default value for the attribute of the composite component:

 <cc:attribute name="size" required="false" default="10" /> 
+10
source

In addition to the BalusC post:

You have to use

type = "int" in the cc: attribute-tag:

cc: attribute name = "maxlength" type = "int"

+1
source

I believe there is an alternative method of accessing attributes. I used this with JSF 2 when accessing an attribute with the java reserved keyword.

{cc.attrs ['size']}

0
source

Source: https://habr.com/ru/post/927752/


All Articles