0 Replies Latest reply on Jun 19, 2011 10:47 AM by Thiago Luiz Rodrigues

    Migrating dynamic PanelMenu richfaces 3.x to 4.x

    Thiago Luiz Rodrigues Newbie

      Hello everyone, I have a dynamic menu where I get data from database to create it in version 3.x of richfaces, but I'm moving to 4.x and I have a problem with

       

      import org.ajax4jsf.component. html.HtmlActionParameter;
      

       

      therefore not found in 4.x. I tried to use

       


      import org.richfaces.component.UIParameter;
      import org.ajax4jsf.component.UIActionParameter;
      


      however does not work as HtmlActionParameter. What would be the new class version 4.x for HtmlActionParameter? Here my code

       

      Version richFaces 4.x

       

       

      package br.com.sixcred.ManagedBeans;
      
      
      import java.util.ArrayList;
      import java.util.List;
      
      import javax.el.MethodExpression;
      import javax.el.ValueExpression;
      import javax.faces.application.Application;
      import javax.faces.bean.ManagedBean;
      import javax.faces.bean.SessionScoped;
      import javax.faces.context.FacesContext;
      
      
      import org.richfaces.component.UIParameter;
      import org.ajax4jsf.component.UIActionParameter;
      import org.richfaces.component.UICommandButton;
      import org.richfaces.component.UIPanelMenu;
      import org.richfaces.component.UIPanelMenuGroup;
      import org.richfaces.component.UIPanelMenuItem;
      
      
      import br.com.sixcred.EJBeans.DAO.ModulosDAO;
      import br.com.sixcred.EJBeans.bean.ItensModulo;
      import br.com.sixcred.EJBeans.bean.Modulos;
      import br.com.sixcred.EJBeans.util.HibernateUtil;
      
      
      @ManagedBean(name="principal")
      @SessionScoped
      public class Principal {
      
      
                private UIPanelMenu menu;
                private UIPanelMenuGroup group;
                private UIPanelMenuItem item;
                private UICommandButton button;
                private String paginaAtual;
                private String texto;
      
        
                public Principal() {
        
                          setPaginaAtual("/pages/xhtml/inicial.xhtml");
        
                }
        
                public void setMenu(UIPanelMenu menu) {
                          this.menu = menu;
                }
      
      
                public void nada() {
      //                    System.out.println("funciona mano");
                }
        
                public UIPanelMenu getMenu() {
        
                          FacesContext ctx = FacesContext.getCurrentInstance();  
                          Application app = ctx.getApplication(); 
        
                          menu = new UIPanelMenu();
        
                          ModulosDAO modulosDAO = new ModulosDAO(new HibernateUtil());
        
                          for (Modulos modulo : modulosDAO.listarTodos()) {
        
                                    group = new UIPanelMenuGroup();
                                    group.setName("modulo_" + modulo.getId());
                                    group.setLabel(modulo.getNome());
        
                                    for (ItensModulo menuItem : modulo.getItensModulo()) {
        
                                              //cria o methodo action
                                              Class[] param = {};
                                              MethodExpression expression = app.getExpressionFactory()
                                              .createMethodExpression(ctx.getELContext(), "#{principal.nada}", String.class, param);
        
                                              //cria uma expressão de valor
                                              ValueExpression valueExpression = FacesContext.getCurrentInstance().getApplication().getExpressionFactory().  
                                              createValueExpression(ctx.getELContext(), "#{principal.paginaAtual}", String.class); 
        
                                              //cria um html Action parameter
                                              UIParameter action = new UIParameter();
                                              action.setName("action_nome" + menuItem.getId());
                                              action.setValue(menuItem.getPagina());
                                              action.setAssignToExpression(valueExpression);
                                              action.setValueExpression(null, valueExpression);
      //                                        .setAssignToBinding(valueExpression);
        
                                              item = new UIPanelMenuItem();
                                              item.setLabel(menuItem.getNome());
                                              item.setName("item_nome" + menuItem.getId());
                                              item.setRender("principal");
                                              item.setActionExpression(expression);
                                              item.addActionListener(action);
                                              item.getChildren().add(action);
        
      //                                        <a4j:commandButton value="mudar" render="principal">
      //                                        <a4j:param value="treta mano" assignTo="#{principal.paginaAtual}"/>
      //                              </a4j:commandButton>
        
                                              group.getChildren().add(item);
                                    }
        
                                    menu.getChildren().add(group);
                          }
        
                          return menu;
                }
      
      
      
      
                public void setItem(UIPanelMenuItem item) {
                          this.item = item;
                }
      
      
                public UIPanelMenuItem getItem() {
                          return item;
                }
      
      
                public void setGroup(UIPanelMenuGroup group) {
                          this.group = group;
                }
      
      
                public UIPanelMenuGroup getGroup() {
                          return group;
                }
      
      
                public void setTexto(String texto) {
                          this.texto = texto;
                }
      
      
                public String getTexto() {
                          return texto;
                }
      
      
                public void setPaginaAtual(String paginaAtual) {
                          this.paginaAtual = paginaAtual;
                          System.out.println(paginaAtual);
                }
      
      
                public String getPaginaAtual() {
                          return paginaAtual;
                }
      
      
                public void setButton(UICommandButton button) {
                          this.button = button;
                }
      
      
                public UICommandButton getButton() {
                          return button;
                }
      }
      
      

       

       

      Version 3.x

       

       

      package br.com.sixcred.ManagedBeans;
      
      import java.util.ArrayList;
      import java.util.List; 
      
      import javax.el.MethodExpression;
      import javax.el.ValueExpression;
      import javax.faces.application.Application;
      import javax.faces.context.FacesContext;
      
      import org.ajax4jsf.component.html.HtmlActionParameter;
      import org.richfaces.component.html.HtmlPanelMenu;
      import org.richfaces.component.html.HtmlPanelMenuGroup;
      import org.richfaces.component.html.HtmlPanelMenuItem;
      
      import br.com.sixcred.EJBeans.DAO.ModulosDAO;
      import br.com.sixcred.EJBeans.bean.ItensModulo;
      import br.com.sixcred.EJBeans.bean.Modulos;
      import br.com.sixcred.EJBeans.util.HibernateUtil;
      
      public class Principal {
      
      
                private HtmlPanelMenu menu;
                private HtmlPanelMenuGroup group;
                private HtmlPanelMenuItem item;
                private String paginaAtual;
                private String texto;
        
        
                public Principal() {
        
                }
        
                public void setMenu(HtmlPanelMenu menu) {
                          this.menu = menu;
                }
      
      
                public void nada() {
      //                    System.out.println("funciona mano");
                }
        
                public HtmlPanelMenu getMenu() {
        
                          FacesContext ctx = FacesContext.getCurrentInstance();  
                          Application app = ctx.getApplication(); 
        
                          menu = new HtmlPanelMenu();
        
                          ModulosDAO modulosDAO = new ModulosDAO(new HibernateUtil());
        
                          for (Modulos modulo : modulosDAO.listarTodos()) {
        
                                    group = new HtmlPanelMenuGroup();
                                    group.setName("modulo_" + modulo.getId());
                                    group.setLabel(modulo.getNome());
        
                                    for (ItensModulo menuItem : modulo.getItensModulo()) {
        
                                              //cria o methodo action
                                              Class[] param = {};
                                              MethodExpression expression = app.getExpressionFactory()
                                              .createMethodExpression(ctx.getELContext(), "#{principal.nada}", String.class, param);
        
                                              //cria uma expressão de valor
                                              ValueExpression valueExpression = FacesContext.getCurrentInstance().getApplication().getExpressionFactory().  
                                              createValueExpression(ctx.getELContext(), "#{principal.paginaAtual}", String.class); 
        
                                              //cria um html Action parameter
                                              HtmlActionParameter action = new HtmlActionParameter();
                                              action.setName("action_nome" + menuItem.getId());
                                              action.setValue(menuItem.getPagina());
                                              action.setAssignToBinding(valueExpression);
        
                                              item = new HtmlPanelMenuItem();
                                              item.setLabel(menuItem.getNome());
                                              item.setName("item_nome" + menuItem.getId());
                                              item.setReRender("principal");
                                              item.setActionExpression(expression);
                                              item.addActionListener(action);
                                              item.getChildren().add(action);
        
                                              group.getChildren().add(item);
                                    }
        
                                    menu.getChildren().add(group);
                          }
        
                          return menu;
                }
      
      
      
      
                public void setItem(HtmlPanelMenuItem item) {
                          this.item = item;
                }
      
      
                public HtmlPanelMenuItem getItem() {
                          return item;
                }
      
      
                public void setGroup(HtmlPanelMenuGroup group) {
                          this.group = group;
                }
      
      
                public HtmlPanelMenuGroup getGroup() {
                          return group;
                }
      
      
                public void setTexto(String texto) {
                          this.texto = texto;
                }
      
      
                public String getTexto() {
                          return texto;
                }
      
      
                public void setPaginaAtual(String paginaAtual) {
                          this.paginaAtual = paginaAtual;
                }
      
      
                public String getPaginaAtual() {
                          return paginaAtual;
                }
      }