4 Replies Latest reply on Oct 20, 2008 3:09 AM by michael_f

    Continued: How can I generate JSF UI components ASYNCHRONOUS

    nbelaevski

      Started here: How can I generate JSF UI components ASYNCHRONOUSLY on a jsf

        • 1. Re: Continued: How can I generate JSF UI components ASYNCHRO
          joblini

          Maybe a4j:include?

          • 2. Re: Continued: How can I generate JSF UI components ASYNCHRO
            nbelaevski

            Beans code:

            package org.richfaces;
            
            public class DynamicDataBean {
            
             private String name;
            
             private String password;
            
             public void action() {
             System.out.println("DynamicBean.action()");
             System.out.println(name);
             System.out.println(password);
             }
            
             public String getName() {
             return name;
             }
            
             public String getPassword() {
             return password;
             }
            
             public void setName(String name) {
             this.name = name;
             }
            
             public void setPassword(String password) {
             this.password = password;
             }
            }


            package org.richfaces;
            
            import java.util.List;
            
            import javax.faces.application.Application;
            import javax.faces.component.UICommand;
            import javax.faces.component.UIComponent;
            import javax.faces.component.html.HtmlInputText;
            import javax.faces.component.html.HtmlPanelGrid;
            import javax.faces.context.FacesContext;
            
            import org.ajax4jsf.component.UIAjaxCommandLink;
            
            public class DynamicComponentBean {
            
             private UIComponent component;
            
             private FacesContext facesContext;
            
             private Application application;
            
             public UIComponent getComponent() {
             return component;
             }
            
             public void setComponent(UIComponent component) {
             this.component = component;
             }
            
             public FacesContext getFacesContext() {
             return facesContext;
             }
            
             public void setFacesContext(FacesContext facesContext) {
             this.facesContext = facesContext;
            
             if (facesContext == null) {
             application = null;
             } else {
             application = facesContext.getApplication();
             }
             }
            
             protected UIComponent createTextInput(String propertyName) {
             UIComponent input = application.createComponent(HtmlInputText.COMPONENT_TYPE);
             input.setValueExpression("value", application.getExpressionFactory().createValueExpression(
             facesContext.getELContext(), "#{dynamicData." + propertyName + "}", String.class));
            
             return input;
             }
            
             protected UIComponent createAjaxLink(String value, String action) {
             UICommand link = (UICommand) application.createComponent(UIAjaxCommandLink.COMPONENT_TYPE);
             link.setValue(value);
             link.setActionExpression(application.getExpressionFactory().createMethodExpression(
             facesContext.getELContext(), "#{dynamicData." + action + "}", Void.class, new Class[0]));
            
             return link;
             }
            
             public void createView() {
             System.out.println("DynamicComponentBean.createView()");
            
             List<UIComponent> children = component.getChildren();
             children.clear();
            
             UIComponent grid = application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
            
             children.add(grid);
             children = grid.getChildren();
            
             children.add(createTextInput("name"));
             children.add(createTextInput("password"));
             children.add(createAjaxLink("Submit", "action"));
             }
            }

            Page:
            <?xml version="1.0" encoding="UTF-8"?>
            
            <jsp:root version="2.1"
             xmlns:jsp="http://java.sun.com/JSP/Page"
             xmlns:c="http://java.sun.com/jsp/jstl/core"
             xmlns:f="http://java.sun.com/jsf/core"
             xmlns:h="http://java.sun.com/jsf/html"
             xmlns:rich="http://richfaces.org/rich"
             xmlns:a4j="http://richfaces.org/a4j">
            
             <jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>
             <jsp:output omit-xml-declaration="no"
             doctype-root-element="html"
             doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
             doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
            
             <f:view>
             <html xmlns="http://www.w3.org/1999/xhtml">
             <body>
             <h:form id="form">
             <a4j:outputPanel id="panel" binding="#{dynamicComponentBean.component}"/>
            
             <a4j:commandLink reRender="panel" action="#{dynamicComponentBean.createView}" value="Create view" />
             </h:form>
             </body>
             </html>
             </f:view>
            </jsp:root>

            Fragment of faces-config.xml:
            ...
             <managed-bean>
             <managed-bean-name>dynamicData</managed-bean-name>
             <managed-bean-class>org.richfaces.DynamicDataBean</managed-bean-class>
             <managed-bean-scope>request</managed-bean-scope>
             </managed-bean>
             <managed-bean>
             <managed-bean-name>dynamicComponentBean</managed-bean-name>
             <managed-bean-class>org.richfaces.DynamicComponentBean</managed-bean-class>
             <managed-bean-scope>request</managed-bean-scope>
             <managed-property>
             <property-name>facesContext</property-name>
             <value>#{facesContext}</value>
             </managed-property>
             </managed-bean>
            ...


            Another option is to use a4j:include as joblini has suggested. You can create views dynamically if you use Facelets by configuring facelets.RESOURCE_RESOLVER context parameter. It points to an instance of the class implementing com.sun.facelets.impl.ResourceResolver and used to obtain URL to resource used as the source for concrete view.

            • 3. Re: Continued: How can I generate JSF UI components ASYNCHRO
              michael_f

              Hello all together,

              i have also solved that problem in my project, but it didn't work in complicated context.

              You could generate the CommandLink and CommandButton but in my case, the focus attribute of the button component didn't work.

              Now I'am searching an other solution in combination with
              <c:forEach var="endButton"
              items="#{frameController.endButtonList}">
              <a4j:commandButton
              binding="#{endButton}"
              alt="#{endButton.alt}"
              actionListener="#{frameController.actionListener}"
              disabled="#{endButton.disabled}"
              image="#{endButton.image}"
              immediate="#{endButton.immediate}"
              label="#{endButton.label}" id="#{endButton.id}"
              rendered="#{endButton.rendered}"
              styleClass="#{endButton.styleClass}"
              type="#{endButton.type}" value="#{endButton.value}">
              </a4j:commandButton>
              </c:forEach>

              but now, the action didn't work, because it seems, that it is not possible to give an action method dynamicly to an action Expression tag.

              Does anybody have an idea ?

              • 4. Re: Continued: How can I generate JSF UI components ASYNCHRO
                michael_f