13 Replies Latest reply on Mar 27, 2009 9:55 AM by Rogerio Gaioso

    dynamic inputText (programmatically)

    Rogerio Gaioso Newbie

      I need to make the generation of dynamic inputText (programmatically). Adapting some examples I found on the web, my JSF page has a select selectOneMenu where the amount of inputTexts I need to show that, when altered, renders a HtmlPanel. Below are the snippets of code:

      * JSF page

      <h:selectOneMenu id="qtdeFaixa" styleClass="listbox"
       valueChangeListener="#{mapaMB.insertInputTexts}" immediate="true">
       <f:selectItem itemValue="1" itemLabel="1"/>
       <f:selectItem itemValue="2" itemLabel="2"/>
       <f:selectItem itemValue="3" itemLabel="3"/>
       <f:selectItem itemValue="4" itemLabel="4"/>
       <f:selectItem itemValue="5" itemLabel="5"/>
       <f:selectItem itemValue="6" itemLabel="6"/>
       <a4j:support event="onchange" reRender="inputTexts"/>
      </h:selectOneMenu>
      
      <h:panelGroup id="inputTexts" />
      
      


      -----------------------------------

      * managed bean
      public void insertInputTexts(ActionEvent action){
       UIComponent group = FacesContext.
       getCurrentInstance().
       getViewRoot().
       findComponent("form1" + NamingContainer.SEPARATOR_CHAR + "inputTexts");
       int count = Integer.parseInt(JSFHelper.getRequestParameter("form1" + NamingContainer.SEPARATOR_CHAR + "qtdeFaixa"));
       group.getChildren().add(new GeneratePanel().getPanel(count));
       DebugUtils.printView(FacesContext.getCurrentInstance().getViewRoot(), System.out);
      }
      


      -----------------------------------

      * GeneratePanel.java

      public GeraPainelInputText(){
       init();
      }
      
      private void init(){
       application = FacesContext.getCurrentInstance().getApplication();
       panel = (HtmlPanel)application.createComponent(HtmlPanel.COMPONENT_FAMILY);
       panel.setStyle("width:550px");
       setComponentes(1);
      }
      
      public HtmlPanel getPanel(int qtde){
       setComponentes(qtde);
       return panel;
      }
      
      private void setComponentes(int qtde){
       for(int i = 0; i < qtde; i++){
       HtmlInputText inputText = (HtmlInputText)application.createComponent(HtmlInputText.COMPONENT_TYPE);
       inputText.setId("inputText" + qtde);
       panel.getChildren().add(inputText);
       }
      }
      


      When compiling, I get the following error:

      cannot access javax.el.ValueExpression
      class file for javax.el.ValueExpression not found

      Despite this strange error, I tried this class on the web and found the file jsp-api-2.1-6.0.2.jar. The compilation error was eliminated but gave an error when running the application (one seemed incompatible with the version of Tomcat). Just to test, I created a jarfile contains only this class javax.el.ValueExpression but trying to turn, returns a VerifyError for
      "Incompatible argument to function".

      My environment:

      Tomcat 6.0.16
      MyFaces 1.2.5
      Richfaces 3.2.2-SR1

      Someone could give me an idea of how to proceed?

        • 1. dynamic inputText (programmatically)
          Rogerio Gaioso Newbie

          Addition, the compilation error appears on line (GeneratePanel.java)

          panel = (HtmlPanel)application.createComponent(HtmlPanel.COMPONENT_FAMILY);
          


          • 2. Re: dynamic inputText (programmatically)
            Nick Belaevski Master

            Hi,

            You should make the following changes:

            1. insertInputTexts method should not be used with h:selectOneMenu, here is the correct code:

            <a4j:support actionListener="#{mapaMB .insertInputTexts}">
            Also it is illegal at all to pass instance of ValueChaneEvent to metyhod accepting ActionEvent.
            2. setComponentes method should clear old children.

            You should have JSF-API in compilation classpath with all its dependencies: el-api, etc. Some libraries can already be bundled with your applications server (depends on its version and supported standards, e.g. JBoss 4.2 already ships with JSF libraries). Libraries already bundled on the server should not be deployed in application, or the bundled ones should be disabled in that case (if application server allows that). If you use Maven you can checkout RF sources from SVN and check dependencies configuration.

            • 3. Re: dynamic inputText (programmatically)
              Rogerio Gaioso Newbie

              thanks for your reply, nbelaevski.

              In his second comment ("method should clear old children"), this is my intent, but thanks for the warning.

              I had not noticed that the el-api.jar (in the Tomcat/lib) contained the ValueExpression class, just enter it in the classpath and the error was eliminated.

              I changed the file JSF (inserting the code suggested after the sixth selectItem) and tried to run the application but received the following error:

              javax.faces.FacesException: java.lang.VerifyError: (class: org/apache/jsp/index_jsp, method: _jspx_meth_f_005fselectItem_005f8 signature: (Ljavax/servlet/jsp/tagext/JspTag;Ljavax/servlet/jsp/PageContext;)Z) Incompatible argument to function
               at org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(ServletExternalContextImpl.java:347)
               at org.apache.myfaces.application.jsp.JspViewHandlerImpl.buildView(JspViewHandlerImpl.java:486)
               at org.apache.myfaces.application.jsp.JspViewHandlerImpl.renderView(JspViewHandlerImpl.java:337)
               at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
               at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:196)
               at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:41)
               at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:140)
               at javax.faces.webapp.FacesServlet.service(FacesServlet.java:155)
               at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
               at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
               at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:177)
               at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:267)
               at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:380)
               at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:507)
               at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
               at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
               at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
               at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
               at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
               at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
               at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
               at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
               at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
               at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
               at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
               at java.lang.Thread.run(Unknown Source)
              Caused by: javax.servlet.ServletException: java.lang.VerifyError: (class: org/apache/jsp/index_jsp, method: _jspx_meth_f_005fselectItem_005f8 signature: (Ljavax/servlet/jsp/tagext/JspTag;Ljavax/servlet/jsp/PageContext;)Z) Incompatible argument to function
               at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:274)
               at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
               at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
               at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
               at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
               at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
               at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
               at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
               at org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(ServletExternalContextImpl.java:341)
               ... 25 more
              Caused by: java.lang.VerifyError: (class: org/apache/jsp/index_jsp, method: _jspx_meth_f_005fselectItem_005f8 signature: (Ljavax/servlet/jsp/tagext/JspTag;Ljavax/servlet/jsp/PageContext;)Z) Incompatible argument to function
               at java.lang.Class.getDeclaredConstructors0(Native Method)
               at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
               at java.lang.Class.getConstructor0(Unknown Source)
               at java.lang.Class.newInstance0(Unknown Source)
               at java.lang.Class.newInstance(Unknown Source)
               at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:145)
               at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
               at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
               at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
               ... 33 more
              


              This is the error of incompatible argument that I had received before. Do you have any idea?

              • 4. Re: dynamic inputText (programmatically)
                Nick Belaevski Master

                Have you bundled jsp-api with your application?

                • 5. Re: dynamic inputText (programmatically)
                  Rogerio Gaioso Newbie

                  The jsp-api.jar is in Tomcat/lib. I did not put in WEB-INF/lib, only set up the classpath for compilation.

                  • 6. Re: dynamic inputText (programmatically)
                    Nick Belaevski Master

                    Try cleaning "work" folder.

                    • 7. Re: dynamic inputText (programmatically)
                      Rogerio Gaioso Newbie

                      I did. There is something strange: I tried to remove some components to try to isolate the problem, but this error always appears in a line before the last "a4j: support". It would not be any bug or incompaibilidade with this component?

                      • 8. Re: dynamic inputText (programmatically)
                        Rogerio Gaioso Newbie

                        Sorry, the last sentence is "not a bug or incompatibility with this component."

                        • 9. Re: dynamic inputText (programmatically)
                          Nick Belaevski Master

                          That doesn't look like an incompatibility, but like application configuration issue.

                          BTW, how about using Facelets instead of JSP?

                          • 10. Re: dynamic inputText (programmatically)
                            Rogerio Gaioso Newbie

                            I read very little about Facelets, and have not had the opportunity to employ it. As a small project, I will see how to adapt it to Facelets. Maybe that was the opportunity that I was missing ;).

                            Thank you for your attention, any questions post here again.

                            • 11. Re: dynamic inputText (programmatically)
                              Rogerio Gaioso Newbie

                              As a very simple project, I follow your suggestion and migrate to Facelets (my first project).

                              Only when you try to run the screen in the new format (only the title and inserting a piece that sets the selectOneMenu that insert the dynamic inputTexts) not appeared.


                              When I try to run, the browser displays the text that should be in the title. In the Eclipse console appears the message:

                              SEVERE: No component states to be saved in client response!

                              You can still help me?

                              Following the codes of the template file and the client in this template.


                              * template.xhtml

                              <?xml version="1.0" encoding="UTF-8"?>
                              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                               "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                              
                              <html xmlns="http://www.w3.org/1999/xhtml"
                               xmlns:f="http://java.sun.com/jsf/core"
                               xmlns:h="http://java.sun.com/jsf/html"
                               xmlns:ui="http://java.sun.com/jsf/facelets"
                               xmlns:a4j="http://richfaces.org/a4j"
                               xmlns:rich="http://richfaces.org/rich">
                              
                              <head>
                               <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                               <title>UE/GO - Mapas Agro - <ui:insert name="tituloPag">**** change me ****</ui:insert></title>
                              </head>
                              
                              <body>
                              <div id="camadaCentral" style="position:absolute; left:194px; top:171px; width:559px; height:327px; z-index:2">
                               <div align="center"><span class="style1">
                               <ui:insert name="camadaCentral">
                              
                               **** change me ****
                              
                               </ui:insert>
                               </span></div>
                              </div>
                              </body>
                              </html>
                              


                              * client.xhtml

                              <?xml version="1.0" encoding="UTF-8"?>
                              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                               "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                              
                              <html xmlns="http://www.w3.org/1999/xhtml"
                               xmlns:f="http://java.sun.com/jsf/core"
                               xmlns:h="http://java.sun.com/jsf/html"
                               xmlns:ui="http://java.sun.com/jsf/facelets"
                               xmlns:a4j="http://richfaces.org/a4j"
                               xmlns:rich="http://richfaces.org/rich"
                               xmlns:tr="http://myfaces.apache.org/trinidad">
                              
                               <ui:composition template="teste.xhtml">
                               <ui:define name="tituloPag">Mapa Agro - teste de tÃÂtulo</ui:define>
                              
                               <ui:define name="camadaCentral">
                               <a4j:form id="form1" focus="produto">
                               <h:panelGrid columns="2" cellpadding="3" cellspacing="3">
                               <f:facet name="header">
                               <h:outputText value="Informe os valores abaixo:" styleClass="tituloFormulario"/>
                               </f:facet>
                              
                               <h:outputLabel value="Qtde de faixas:" style="font=12px"/>
                               <h:selectOneMenu id="qtdeFaixa" styleClass="listbox" immediate="true">
                               <f:selectItem itemValue="1" itemLabel="1"/>
                               <f:selectItem itemValue="2" itemLabel="2"/>
                               <f:selectItem itemValue="3" itemLabel="3"/>
                               <f:selectItem itemValue="4" itemLabel="4"/>
                               <f:selectItem itemValue="5" itemLabel="5"/>
                               <f:selectItem itemValue="6" itemLabel="6"/>
                               <a4j:support actionListener="#{mapaMB.insereInputTexts}"
                               reRender="inputTextsDinamicos" limitToList="true" focus="xxxx"/>
                               </h:selectOneMenu>
                              
                               <h:outputLabel value="Faixas:"/>
                               <h:panelGroup id="inputTextsDinamicos" />
                              
                               <f:verbatim> </f:verbatim>
                               <tr:commandButton text="Ok" useWindow="true" action="dialog:mapa"/>
                               </h:panelGrid>
                               </a4j:form>
                               </ui:define>
                               </ui:composition>
                              </html>
                              



                              • 12. Re: dynamic inputText (programmatically)
                                Nick Belaevski Master

                                Hello,

                                I haven't ever seen such message before. Looks like an issue specific to Trinidad.

                                • 13. Re: dynamic inputText (programmatically)
                                  Rogerio Gaioso Newbie

                                  I migrate to Facelets as the suggestion. Below is the code that could run to serve as an example for others.

                                  * form.xhtml

                                  <?xml version="1.0" encoding="ISO-8859-1"?>
                                  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                                   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                                  
                                  <html xmlns="http://www.w3.org/1999/xhtml"
                                   xmlns:f="http://java.sun.com/jsf/core"
                                   xmlns:h="http://java.sun.com/jsf/html"
                                   xmlns:ui="http://java.sun.com/jsf/facelets"
                                   xmlns:a4j="http://richfaces.org/a4j"
                                   xmlns:rich="http://richfaces.org/rich"
                                   xmlns:tr="http://myfaces.apache.org/trinidad">
                                  
                                   (...)
                                  
                                   <h:outputLabel value="Qtde de faixas:" style="font=12px"/>
                                   <h:selectOneMenu id="qtdeFaixa" styleClass="listbox" immediate="true">
                                   <f:selectItem itemValue="0" itemLabel=" "/>
                                   <f:selectItem itemValue="1" itemLabel="1"/>
                                   <f:selectItem itemValue="2" itemLabel="2"/>
                                   <f:selectItem itemValue="3" itemLabel="3"/>
                                   <f:selectItem itemValue="4" itemLabel="4"/>
                                   <f:selectItem itemValue="5" itemLabel="5"/>
                                   <f:selectItem itemValue="6" itemLabel="6"/>
                                   <a4j:support event="onchange" actionListener="#{mapaMB.insereInputTexts}"
                                   reRender="inputTextsDinamicos" limitToList="true" focus="xxxx"/>
                                   </h:selectOneMenu>
                                  
                                   <h:outputLabel value="Faixas:"/>
                                   <h:panelGroup id="inputTextsDinamicos"/>
                                  
                                   (...)
                                  
                                  </html>



                                  * managed bean

                                  (...)
                                  
                                   public void insereInputTexts(ActionEvent action){
                                   UIComponent group = FacesContext.
                                   getCurrentInstance().
                                   getViewRoot().
                                   findComponent("form1" + NamingContainer.SEPARATOR_CHAR + "inputTextsDinamicos");
                                  
                                   int qtdeInputText = Integer.parseInt(JSFHelper.
                                   getRequestParameter("form1" + NamingContainer.SEPARATOR_CHAR + "qtdeFaixa"));
                                  
                                   group.getChildren().clear();
                                   group.getChildren().add(new GeraPainelInputText().getPanel(qtdeInputText));
                                  
                                   DebugUtils.printView(FacesContext.getCurrentInstance().getViewRoot(), System.out);
                                   }
                                  
                                   (...)


                                  * GeraPainelInputText.java

                                  //constructor
                                   public GeraPainelInputText(){
                                   application = FacesContext.getCurrentInstance().getApplication();
                                   panel = (HtmlPanel)application.createComponent(HtmlPanel.COMPONENT_FAMILY);
                                   }
                                  
                                   public HtmlPanel getPanel(int qtde){
                                   setComponentes(qtde);
                                   return panel;
                                   }
                                  
                                   private void setComponentes(int qtde){
                                   for(int i = 0; i < qtde; i++){
                                   HtmlOutputLabel label01 = (HtmlOutputLabel)application.
                                   createComponent(HtmlOutputLabel.COMPONENT_TYPE);
                                   label01.setValue("InÃÂcio:");
                                   label01.setId("label01_" + qtde);
                                   HtmlOutputLabel label02 = (HtmlOutputLabel)application.
                                   createComponent(HtmlOutputLabel.COMPONENT_TYPE);
                                   label02.setValue("Término:");
                                   label02.setId("label02_" + qtde);
                                  
                                   HtmlInputText inputText01 = (HtmlInputText)application.
                                   createComponent(HtmlInputText.COMPONENT_TYPE);
                                   inputText01.setId("inputText01_" + qtde);
                                   inputText01.setStyleClass("listbox");
                                   HtmlInputText inputText02 = (HtmlInputText)application.
                                   createComponent(HtmlInputText.COMPONENT_TYPE);
                                   inputText02.setId("inputText02_" + qtde);
                                   inputText02.setStyleClass("listbox");
                                  
                                   panel.getChildren().add(label01);
                                   panel.getChildren().add(inputText01);
                                   panel.getChildren().add(label02);
                                   panel.getChildren().add(inputText02);
                                   }
                                   }
                                  


                                  Thanks!