« Return to Thread: Help with custom tag attribute

Re: Help with custom tag attribute

by Jacob Hookom :: Rate this Message:

Reply to Author | View in Thread

Did you try a ternary expression?

#{(empty rendered) ? true : rendered}

Mike Kienenberger wrote:

> I'm not certain how you can handle an attribute that might not be
> there, but if you know the attribute is always going to be used in
> your composition but you don't know if the argument will be specified,
> you can use this pattern.  Maybe this will help.
>
> <c:if test="${empty rendered}">
> <c:set var="rendered" value="${true}" />
> </c:if>
>
> <h:panelGroup rendered="#{rendered}">
>
>
> On Tue, Jul 7, 2009 at 7:42 AM, Felipe Jaekel<fkjaekel@...> wrote:
>  
>> Thanks for the help.
>>
>> I'll try to explain better. My component doesn't have a Java class, its only
>> a tag. It looks like this:
>>
>>    
>>> <ui:composition
>>>     xmlns:h="http://java.sun.com/jsf/html"
>>>     xmlns:ui="http://java.sun.com/jsf/facelets"
>>>      
>>>     <h:panelGrid
>>>         columns="2"
>>>         cellpadding="0"
>>>         cellspacing="0"
>>>     >
>>>         <h:selectBooleanCheckbox
>>>             id="#{id}"
>>>             value="#{value}"
>>>         >
>>>             <ui:insert />
>>>         </h:selectBooleanCheckbox>
>>>         <h:outputLabel
>>>             value="#{label}"
>>>             for="#{id}"
>>>         />
>>>     </h:panelGrid>
>>> </ui:composition>
>>>      
>> The id attribute is expecting a value, so every time I use the component I
>> have to set it, otherwise I'll get an IllegalArgumentException. If I
>> hardcode the id I'll get a duplicated id exception if I have more than 1
>> component on the page. If I had a Java class for the component I could
>> simply do something like this:
>>
>>    
>>> if(id != null && !id.trim().isEmpty())
>>> {
>>>    component.setId(id);
>>> }
>>>      
>> How to achieve this on the tag? As I mentioned above f:attribute is not
>> working as expected.
>>
>> Thanks,
>> Felipe
>>
>> 2009/6/19 Fırat KÜÇÜK <firatkucuk@...>
>>    
>>> Hi,
>>> i didn't understand exactly what you mean. But may be this will helps you.
>>>
>>> this is my jsf managed bean:
>>> -----------------------------
>>> package mypackage;
>>>
>>> public class Sample {
>>>
>>>   private String label = "label";
>>>   private String value = "value";
>>>   private int id = 0;
>>>
>>>   public Sample() {
>>>   }
>>>
>>>   public int getId() {
>>>     return id;
>>>   }
>>>
>>>   public void setId(int id) {
>>>     this.id = id;
>>>   }
>>>
>>>   public String getLabel() {
>>>     return label;
>>>   }
>>>
>>>   public void setLabel(String label) {
>>>     this.label = label;
>>>   }
>>>
>>>   public String getValue() {
>>>     return value;
>>>   }
>>>
>>>   public void setValue(String value) {
>>>     this.value = value;
>>>   }
>>>
>>>   public String submit() {
>>>     label = value;
>>>     return null;
>>>   }
>>> }
>>>
>>>
>>> --------------------------------------------------
>>> and this is facelets file:
>>>
>>>
>>> <?xml version='1.0' encoding='UTF-8' ?>
>>>
>>> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
>>> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
>>> <html xmlns="http://www.w3.org/1999/xhtml"
>>>       xmlns:c="http://java.sun.com/jstl/core"
>>>       xmlns:f="http://java.sun.com/jsf/core"
>>>       xmlns:ui="http://java.sun.com/jsf/facelets"
>>>       xmlns:h="http://java.sun.com/jsf/html">
>>>   <body>
>>>     <h:form>
>>>       <h:outputLabel value="#{sample.label}" />
>>>       <h:outputLabel value="#{sample.id}" />
>>>       <h:messages />
>>>       <h:inputText label="#{sample.label}" value="#{sample.value}">
>>>         <f:convertNumber />
>>>       </h:inputText>
>>>       <h:commandButton value="submit" action="#{sample.submit}">
>>>         <c:if test="#{!empty sample.id}">
>>>           <f:setPropertyActionListener target="#{sample.id}"
>>> value="#{sample.value}" />
>>>         </c:if>
>>>       </h:commandButton>
>>>     </h:form>
>>>   </body>
>>> </html>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> 2009/6/19 Felipe Jaekel <fkjaekel@...>
>>>      
>>>> Actually the problem is that f:attribute is not evaluating the the EL. If
>>>> I pass the value literally it accepts, but if I pass #{maxlength} it is
>>>> trying to set the string "#{maxlength}" as maxleght, so thats why the I get
>>>> the ClassCastException.
>>>>
>>>>        
>>>>> <h:outputLabel value="#{label}" />
>>>>>     <h:inputText
>>>>>         label="#{label}"
>>>>>         value="#{value}"
>>>>>     >
>>>>>         <c:if test="#{! empty id}">
>>>>>             <f:attribute
>>>>>                 name="id"
>>>>>                 value="#{id}"
>>>>>             />
>>>>>         </c:if>
>>>>>         <ui:insert />
>>>>>     </h:inputText>
>>>>>          
>>>> The id attribute is a better exemple of what I'm trying to do. When it's
>>>> not set in the custom tag I'd like that it is not set in the h:inputText tag
>>>> so that JSF generates the id. Otherwise always when I use the custom tag
>>>> I'll have to set the id or write some crazy workaround to generate it so
>>>> that I don't get a duplicated id exception.
>>>>
>>>> Any ideias? Does f:attribute only accepts literals?
>>>>
>>>> Thanks,
>>>> Felipe
>>>>
>>>> 2009/5/6 joseph j marini <josephjmarini@...>
>>>>        
>>>>> Try mapping value to a backing bean #{foo.maxLength}
>>>>> in your backing if max length is optional return null, else return
>>>>> Integer object with value set. It will get converted automatically.
>>>>>
>>>>> /Joe
>>>>>
>>>>>
>>>>> --------------------------------------------------
>>>>> From: "Zarar Siddiqi" <zarars@...>
>>>>> Sent: Wednesday, May 06, 2009 11:26 AM
>>>>> To: <users@...>
>>>>> Subject:  Re: Help with custom tag attribute
>>>>>
>>>>>          
>>>>>> There's a difference between saying something is optional by passing
>>>>>> in a null, entirely another to pass in a string and expect it to be
>>>>>> ignored just because its not an integer.
>>>>>>
>>>>>> What is the value of maxlength when you pass it in? Is it null or a
>>>>>> string?
>>>>>>
>>>>>>
>>>>>>
>>>>>> On Fri, Apr 24, 2009 at 1:16 PM, Felipe Jaekel <fkjaekel@...>
>>>>>> wrote:
>>>>>>            
>>>>>>> I have a custom tag that is composed by a <h:outputLabel /> +
>>>>>>> <h:inputText
>>>>>>> />. I need the maxlength attribute to be optional. The problem is that
>>>>>>> when
>>>>>>> I use <f:attribute /> I get a class cast exception.
>>>>>>>
>>>>>>> Tag:
>>>>>>>
>>>>>>>              
>>>>>>>> <ui:composition
>>>>>>>> xmlns:h="http://java.sun.com/jsf/html"
>>>>>>>> xmlns:c="http://java.sun.com/jstl/core"
>>>>>>>> xmlns:f="http://java.sun.com/jsf/core"
>>>>>>>> xmlns:ui="http://java.sun.com/jsf/facelets"
>>>>>>>>                
>>>>>>>> <h:outputLabel value="#{label}" />
>>>>>>>> <h:inputText
>>>>>>>> label="#{label}"
>>>>>>>> value="#{value}"
>>>>>>>>                
>>>>>>>> <c:if test="#{! empty maxlength}">
>>>>>>>> <f:attribute
>>>>>>>> name="maxlength"
>>>>>>>> value="#{maxlength}"
>>>>>>>> />
>>>>>>>> </c:if>
>>>>>>>> <ui:insert />
>>>>>>>> </h:inputText>
>>>>>>>> </ui:composition>
>>>>>>>>                
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> Exception:
>>>>>>>
>>>>>>>              
>>>>>>>> java.lang.ClassCastException: java.lang.String cannot be cast to
>>>>>>>> java.lang.Integer
>>>>>>>>                
>>>>>>>
>>>>>>> Any ideas?
>>>>>>>
>>>>>>> Thanks,
>>>>>>> Felipe
>>>>>>>
>>>>>>>              
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: users-unsubscribe@...
>>>>>> For additional commands, e-mail: users-help@...
>>>>>>
>>>>>>
>>>>>>            
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: users-unsubscribe@...
>>>>> For additional commands, e-mail: users-help@...
>>>>>
>>>>>          
>>>
>>> --
>>> FIRAT KÜÇÜK
>>>      
>>    
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>  
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.375 / Virus Database: 270.13.7/2222 - Release Date: 07/07/09 05:53:00
>
>  


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...

 « Return to Thread: Help with custom tag attribute