Design Question: How do you know witch item is selected in a
jan-martijnw Jul 2, 2008 4:31 AMHi Everyone!
I'm really struggling with this one. In our project we are using a rich panelmenu witch is dynamicly constructed in a backing bean. I'm a bit new witch JSF and now i ran into a problem. The menu can consist of three submenu's, each step deeper in the 'tree' the menu we point to another method of the backingbean ( and eventually to another page).
But now my question, when a menuItem is pressed, and de method in the backingbean is triggerd, how can i know witch item was selected? on the panel menu, i can get the "selectedItem" but than i get the value of the button, but what i really want is the ID of the item, so that Hibernate can collect that item from the DB.
I hope someone can point me in the right direction, i'm a bit stuck now.
this is the beacking bean which is constructing the HtmlPanelMenu:
import java.util.Iterator; import javax.faces.application.Application; import javax.faces.context.FacesContext; import javax.faces.event.ActionEvent; import javax.faces.model.DataModel; import javax.faces.model.ListDataModel; import nl.minjus.dji.sscno.kennismanagement.entities.Kennisdeelgebied; import nl.minjus.dji.sscno.kennismanagement.entities.Kennisdomein; import nl.minjus.dji.sscno.kennismanagement.entities.Kennisgebied; import nl.minjus.dji.sscno.kennismanagement.service.KennisgebiedService; import org.richfaces.component.html.HtmlPanelMenu; import org.richfaces.component.html.HtmlPanelMenuGroup; import org.richfaces.component.html.HtmlPanelMenuItem; /** * UserMenuController.java dynamicly creates a <code>HtmlPanelMenu</code> * based on the 'Kennisgebieden' found in the model. The model is accessed by * the <code>kennisgebiedService</code> witch itself is injected by Spring framework. * * * @author ing. J.M. Wijnholds * */ public class UserMenuController { private HtmlPanelMenu panelMenu = null; /* * The Spring Bean which provides the Kennisgebied services that do the JPA * queries */ private KennisgebiedService kennisgebiedService; /** * @return the kennisgebiedService */ public KennisgebiedService getKennisgebiedService() { return kennisgebiedService; } /** * @param kennisgebiedService * the kennisgebiedService to set */ public void setKennisgebiedService(KennisgebiedService kennisgebiedService) { this.kennisgebiedService = kennisgebiedService; } /** * Creation of the <code>HtmlPanelMenu</code> based on the * 'Kennisgebieden', 'Kennisdomeinen' and 'Kennisdeelgebieden' * found in the model * * @return A HtmlPanelMenu witch can be used in * <code>kms-template.xhtml</code> */ @SuppressWarnings("unchecked") public HtmlPanelMenu getPanelMenu() { if (panelMenu == null) { /* * Initialize HtmlPanelMenu */ panelMenu = new HtmlPanelMenu(); panelMenu.setId("panelMenu"); panelMenu.setStyle("width:100%"); panelMenu.setMode("ajax"); panelMenu.setExpandMode("ajax"); panelMenu.setIconExpandedGroup("disc"); panelMenu.setIconCollapsedGroup("disc"); panelMenu.setIconExpandedTopGroup("chevronUp"); panelMenu.setIconGroupTopPosition("left"); panelMenu.setIconCollapsedTopGroup("chevronDown"); panelMenu.setIconGroupTopPosition("left"); FacesContext facesContext = FacesContext.getCurrentInstance(); Application appl = facesContext.getApplication(); Iterator iter = kennisgebiedService.getKennisgebieden(0, 0) .iterator(); // For every 'Kennisgebied' create a new HtmlPanelMenuGroup(); while (iter.hasNext()) { Kennisgebied kennisgebied = (Kennisgebied) iter.next(); HtmlPanelMenuGroup menuItemKennisgebied = new HtmlPanelMenuGroup(); menuItemKennisgebied.setLabel(kennisgebied.getTitel()); menuItemKennisgebied.setName(kennisgebied.getTitel()); menuItemKennisgebied.setIconCollapsed("triangle"); menuItemKennisgebied.setIconExpanded("triangleDown"); menuItemKennisgebied.setAction(appl.createMethodBinding("#{userMenuController.go}", new Class[0])); Iterator iterKennisDomeinen = kennisgebied.getCollectieKennisDomein().iterator(); while (iterKennisDomeinen.hasNext()) { Kennisdomein kennisdomein = (Kennisdomein) iterKennisDomeinen.next(); if (kennisdomein.getCollectieKennisdeelgebied().isEmpty()) { HtmlPanelMenuItem menuItem_Kennisdomein = new HtmlPanelMenuItem(); menuItem_Kennisdomein.setLabel(kennisdomein.getTitel()); menuItem_Kennisdomein.setName(kennisdomein.getTitel()); // Binding the HtmlMenuItem to the temporary 'Go'method menuItem_Kennisdomein.setAction(appl.createMethodBinding("#{userMenuController.go}", new Class[0])); menuItemKennisgebied.getChildren().add(menuItem_Kennisdomein); } else { HtmlPanelMenuGroup menuItem_Kennisdomein = new HtmlPanelMenuGroup(); menuItem_Kennisdomein.setLabel(kennisdomein.getTitel()); menuItem_Kennisdomein.setName(kennisdomein.getTitel()); // Binding the HtmlMenuItem to the temporary 'Go'method menuItem_Kennisdomein.setAction(appl.createMethodBinding("#{userMenuController.go}", new Class[0])); Iterator iterDeelgebieden = kennisdomein.getCollectieKennisdeelgebied().iterator(); while(iterDeelgebieden.hasNext()){ Kennisdeelgebied kennisdeelgebied = (Kennisdeelgebied)iterDeelgebieden.next(); HtmlPanelMenuItem menuItem_Kennisdeelgebied = new HtmlPanelMenuItem(); menuItem_Kennisdeelgebied.setLabel(kennisdeelgebied.getTitel()); menuItem_Kennisdeelgebied.setName(kennisdeelgebied.getTitel()); // Binding the HtmlMenuItem to the temporary 'Go'method menuItem_Kennisdeelgebied.setAction(appl.createMethodBinding("#{userMenuController.go}", new Class[0])); menuItem_Kennisdomein.getChildren().add(menuItem_Kennisdeelgebied); } menuItemKennisgebied.getChildren().add(menuItem_Kennisdomein); } } panelMenu.getChildren().add(menuItemKennisgebied); } } return panelMenu; } /** * Set a instantion of the panelMenu when a page is reloaded. * * @param panelMenu */ public void setPanelMenu(HtmlPanelMenu panelMenu) { this.panelMenu = panelMenu; } public String go(){ System.out.println("GET SELECTED NAME: " + panelMenu.getSelectedName()); System.out.println("GET SUBMITTED VALUE: " + panelMenu.getSubmittedValue()); System.out.println("GET VALUE: " + (panelMenu.getValue())); System.out.println("KennisId: " + FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("kennisId")); System.out.println("nav_to_kennisgebieden_overzicht"); return "nav_to_kennisgebieden_overzicht"; } }
I hope to hear from you all soon :)
Thnx in advance
Jan-Martijn