This example is in response to this post.
The solution uses two managed beans, one in request and one in session scope. The session bean will keep the menu state. You can easily extend this example to use Seam. I need to remember two things. First is which group was opened/closed and second which item inside the group was clicked. Remembering which item inside the group was clicked is done via selectedChild=”#{menuState.selectedMenuItem}”. Remembering which group was opened is done via value attribute on each group. The value is saved automatically.
I’m setting action=”page.xhtml” because I’m using a custom navigation handler. Standard navigation rules will work the same way.
start.xhtml:
MenuBean managed bean (in request scope):
package test;
import javax.faces.event.ActionEvent;
public class MenuBean {
private MenuState menuState;
public MenuState getMenuState() {
return menuState;
}
public void setMenuState(MenuState menuState) {
this.menuState = menuState;
}
public MenuBean() {}
public void select (ActionEvent event) {
menuState.setSelectedMenuItem(event.getComponent().getId());
}
}
In select listener, we are remembering which rich:panelMenuItem was clicked. When we come back to the page, the clicked item will be italicized via selectedChild=”#{menuState.selectedMenuItem}”.
MenuState bean (in session scope) keeps the menu state.
package test;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
public class MenuState {
private Map menu;
private String selectedMenuItem;
public String getSelectedMenuItem() {
return selectedMenuItem;
}
public Map getMenu() {
return menu;
}
public void setMenu(Map menu) {
this.menu = menu;
}
public void setSelectedMenuItem(String selectedMenuItem) {
this.selectedMenuItem = selectedMenuItem;
}
public MenuState() {
}
@PostConstruct
public void init () {
menu = new HashMap ();
menu.put("group1", false);
menu.put("group2", false);
}
}
rich:panelMenuGroup value attribute is bound to the menu HashMap and is saved automatically.
JSF configuration file:
menuBean
test.MenuBean
request
menuState
test.MenuState
#{menuState}
menuState
test.MenuState
session
select.xhtml is very simple:
Go
Leave a reply to Renato Velasquez Cancel reply