10 Replies Latest reply on Dec 18, 2009 10:06 AM by rafaelk

    Is there a way to create a sub-component?

    edilmar

      Hi,

       

      I have used RichFaces 3.3.2 to create my xhtml pages.

      Sometimes I have to create many rich:calendar with specific inputSize/datePattern/etc,

      many outputText with specific convertDateTime/convertNumber/etc.

       

      Then, I would like to know if it is possible to create, for example, a component

      named "mycalendar", "myOutputDate" or "myOutputNumber", with some standard

      values for many properties, but these comps must be inherited from default JSF

      or RichFaces components. I don't want to implement complete new comps, only

      to setup "default values" for actual comps. Is this possible easily?

       

      I didn't find on internet articles to setup default values, only to create complete

      new components...

       

      Thanks in advance,

        • 1. Re: Is there a way to create a sub-component?
          ilya_shaikovsky
          please explore facelets documentation. custom facelet creation - should be perfect solution for your case.
          • 2. Re: Is there a way to create a sub-component?
            rafaelk

            If I got it correct, I would suggest you to use Facelets capabilities to create custom tags.

            You can define your own taglib and custom tags without any giant effort. It gets a pretty clean and effective code.

             

            Take a look at the 3.5.5 item at this document:

            https://facelets.dev.java.net/nonav/docs/dev/docbook.html

            I guess that you want something like it.

             

            Hope that helps.

            • 3. Re: Is there a way to create a sub-component?
              rafaelk

              Sorry for replying the same thing.

              I wrote the answer then get busy and forgot to submit the message and just do it now.

               

               

              • 4. Re: Is there a way to create a sub-component?
                ilya_shaikovsky
                no.. thats cool - clarification with link which I forgot
                • 5. Re: Is there a way to create a sub-component?
                  edilmar

                  Hi friends,

                   

                  I look at Facelets docs "3.5.6 Custom Tags" and "7. Extending Facelets".

                  But I don't know if this is the way to solve my doubt.

                  I wouldn't like to create Java code for a new component.

                  Suppose I have the following rich:calendar:

                  <rich:calendar value="#{consCliente.dataIni}" id="dataIni" 
                  
                  zindex="1000" inputSize="8"
                  datePattern="dd/MM/yyyy" enableManualInput="true" 
                  
                  showWeeksBar="false"
                  cellWidth="20px" cellHeight="20px" />
                  

                   

                  Then, I would like to create in my templates a child from rich:calendar like this:

                  <MYcalendar value="#{consCliente.dataIni}" id="dataIni" />
                  

                  where all other properties (zindex,inputSize,datePattern,etc) are default to values above.

                   

                  =====================================

                  Or, other example, if I have these outputText's:

                  <h:outputText value="#{item.data}"><f:convertDateTime pattern="dd/MM/yyyy"/></h:outputText>
                  <h:outputText value="#{item.valor}"><f:convertNumber minFractionDigits="2" maxFractionDigits="2"/></h:outputText>
                  

                   

                  I would like to create like this:

                  <h:MYoutputDate value="#{item.data}" />
                  <h:MyoutputNum value="#{item.valor}" />
                  

                  and default values for converting date/number are used.

                   

                  Is the only way to do this to create new components in Java using Facelets documentation,

                  and then inherited from rich:calendar or outputText? Or is there some XML JSF/Facelets

                  commands to create a "templating component from default component"?

                   

                  Thanks in advance,

                  • 6. Re: Is there a way to create a sub-component?
                    ilya_shaikovsky

                    section 3.5.5 is what you really need.

                     

                    In a few words to create such facelet from template you should:

                     

                    • create template itself
                    • create taglib and register tag which uses source template.
                    • add your taglib namespace to the page and add the new tag itself passing defined params.
                    • 7. Re: Is there a way to create a sub-component?
                      rafaelk

                      Edilmar,

                       

                      looks like that you're brazilian, I guess.

                      If you are, just take a look at this:

                      http://www.devmedia.com.br/articles/viewcomp.asp?comp=5332

                       

                      For who don't know portuguese, just looking to the code arrangements give an idea of how it works.

                      • 8. Re: Is there a way to create a sub-component?
                        edilmar

                        Hi friend, I'm brazilian too...

                         

                        I tried to follow the code from JavaMagazine, but now the component disappear.

                        Look at these steps:

                        1) Create SubMacroFaces.taglib.xml file into WEB-INF subdir:

                        <?xml version="1.0" encoding="UTF-8"?>
                        <!DOCTYPE facelet-taglib PUBLIC
                          "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
                          "facelet-taglib_1_0.dtd">
                        <facelet-taglib>
                          <namespace>http://submacro.dev.java.net/facelets</namespace>
                          <tag>
                            <tag-name>smdate</tag-name>
                            <source>smdate.xhtml</source>
                          </tag>
                        </facelet-taglib>
                        

                        2) Configure web.xml:

                          <context-param>
                            <param-name>Facelets.LIBRARIES</param-name>
                            <param-value>/WEB-INF/SubMacroFaces.taglib.xml</param-value>
                          </context-param>
                        

                        3) Create smdate.xhtml file into WEB-INF subdir:

                        <?xml version="1.0" encoding="UTF-8"?>
                        <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:f="http://java.sun.com/jsf/core"
                              xmlns:a4j="http://richfaces.org/a4j"
                              xmlns:rich="http://richfaces.org/rich">
                        <ui:composition>
                          <rich:calendar value="#{value}" id="#{id}"
                                         zindex="1000" inputSize="8"
                                         datePattern="dd/MM/yyyy" enableManualInput="true" showWeeksBar="false"
                                         cellWidth="20px" cellHeight="20px" />
                        </ui:composition>
                        </html>
                        

                        4) Use the new component sm:smdate into a .xhtml file... two configs:

                        4.1) Header of the .xhtml file:

                        <?xml version='1.0' encoding='UTF-8' ?>
                        <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                        <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"
                                        xmlns:a4j="http://richfaces.org/a4j"
                                        xmlns:rich="http://richfaces.org/rich"
                                        xmlns:sm="http://submacro.dev.java.net/facelets"
                                        template="/menuCliente.xhtml">
                        

                        4.2) Using the component...

                        <sm:smdate value="#{consViagensCliente.dataIni}" id="dataIni" />
                        

                         

                        The rich:calendar disappeared. The value uses "consViagensCliente" managed bean and his "dataIni" variable.

                        • 9. Re: Is there a way to create a sub-component?
                          ilya_shaikovsky
                          if the component not appears - check generated html. if you will be able to see something like <sm:smdate value="#{consViagensCliente.dataIni}" id="dataIni" /> there - problems with configuration.
                          • 10. Re: Is there a way to create a sub-component?
                            rafaelk

                            That's a usual problem that appears in all project starts

                             

                            Try reconfiguring facelets, pay attention to the paths, mainly to the declared paths of the custom components that you've implemented.