JSCookMenu and passing value to manged-bean
Hi
Is it possible to pass a value to a managed-bean after clicking menu item of the JSCookMenu? The problem is that I have to pass an id (related with selected menu item) to a managed-bean . The id is necessary to retrieve data on the next page.
I'm using MyFaces 1.1.4.
Regards,
Mariusz
Hi!
Thanks for the answer. It helped me a lot. At the beginning i thought that the following code:
String categoryId = (String) command.getValue();
would have returned a label value. But after testing it turned out that my predictions were wrong.
Below I put my code maybe it will be useful for somebody. Once again thank you.
Regards,
Mariusz
package forum.view.managedbeans;
import java.util.List;
import javax.faces.FactoryFinder;
import javax.faces.application.Application;
import javax.faces.application.ApplicationFactory;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import org.apache.myfaces.custom.navmenu.NavigationMenuItem;
import org.apache.myfaces.custom.navmenu.jscookmenu.HtmlCommandJSCookMenu;
import forum.business.entities.CategoryBO;
import forum.business.facades.CategoryFacade;
import forum.util.exceptions.ForumException;
public class DynamicMenuBean implements ActionListener {
private NavigationMenuItem[] categoryItems;
private CategoryFacade categoryFacade;
public DynamicMenuBean() {
}
public void processAction(ActionEvent event)
throws AbortProcessingException {
HtmlCommandJSCookMenu command = (HtmlCommandJSCookMenu) event
.getSource();
String categoryId = (String) command.getValue();
FacesContext facesContext = FacesContext.getCurrentInstance();
FrontTableBean frontTableBean = (FrontTableBean) getValueBinding(
"#{frontTableBean}").getValue(facesContext);
frontTableBean.setCategoryId(new Long(categoryId));
}
private ValueBinding getValueBinding(String valueRef) {
ApplicationFactory factory = (ApplicationFactory) FactoryFinder
.getFactory(FactoryFinder.APPLICATION_FACTORY);
Application application = factory.getApplication();
return application.createValueBinding(valueRef);
}
public NavigationMenuItem[] getCategoryItems() {
List categories = null;
try {
categories = categoryFacade.listCategories();
categoryItems = new NavigationMenuItem[categories.size()];
for (int i = 0; i < categories.size(); i++) {
CategoryBO categoryBO = (CategoryBO) categories.get(i);
categoryItems[i] = new NavigationMenuItem(categoryBO.getName(), "mycategory");
categoryItems[i].setValue(categoryBO.getId().toString());
categoryItems[i].setActionListener("#{dynamicMenuBean.processAction}");
}
} catch (ForumException e) {
System.err.println(e.getMessage()+e.getCause());
}
return categoryItems;
}
public void setCategoryItems(NavigationMenuItem[] categoryItems) {
this.categoryItems = categoryItems;
}
public void setCategoryFacade(CategoryFacade categoryFacade) {
this.categoryFacade = categoryFacade;
}
}
One more thing... I realized that it would be better to inject FrontTableBean directly via faces-config.xml thus one can get rid of the following code:
private ValueBinding getValueBinding(String valueRef) {
ApplicationFactory factory = (ApplicationFactory) FactoryFinder
.getFactory(FactoryFinder.APPLICATION_FACTORY);
Application application = factory.getApplication();
return application.createValueBinding(valueRef);
}
as well as:
FacesContext facesContext = FacesContext.getCurrentInstance();
FrontTableBean frontTableBean = (FrontTableBean) getValueBinding(
"#{frontTableBean}").getValue(facesContext);
Regards
Mariusz
I'd like to call a function of a managed bean. I did like that but the function isn't called.
Here is the code of the managed bean:
public ArrayList<NavigationMenuItem> getNavItems()
{
ArrayList<NavigationMenuItem> menu = new ArrayList<NavigationMenuItem>();
// layers
ArrayList<NavigationMenuItem> layerItems = new ArrayList<NavigationMenuItem>();
Iterator it = layers.iterator();
while (it.hasNext())
{
WebigeoLayer webigeoLayer = (WebigeoLayer) it.next();
NavigationMenuItem layerItem = new NavigationMenuItem(webigeoLayer
.getAliasName(), null, "./images/galigeo/new_ana.gif",
false);
ArrayList<RendererAssistant> renderers = webigeoLayer
.getRenderers();
if (renderers.size() > 0)
{
ArrayList<NavigationMenuItem> rendererItems = new ArrayList<NavigationMenuItem>();
Iterator it2 = renderers.iterator();
while (it2.hasNext())
{
RendererAssistant renderer = (RendererAssistant) it2.next();
NavigationMenuItem rendererItem = new NavigationMenuItem(
renderer.getTitle(), null);
if (renderer.isEnabled())
rendererItem.setIcon("./images/galigeo/checked.gif");
else
rendererItem.setIcon("./images/galigeo/unchecked.gif");
rendererItem.setValue(renderer.getId());
rendererItem
.setActionListener("#{rendererList.enableRendererFromSmallMap}");
rendererItems.add(rendererItem);
}
layerItem.setNavigationMenuItems(rendererItems);
}
layerItems.add(layerItem);
}
ResourceBundle rb = ResourceBundle.getBundle("i18n/messages");
NavigationMenuItem menuItem = new NavigationMenuItem(rb
.getString("renderers"), null);
menuItem.setNavigationMenuItems(layerItems);
// root items
menu.add(menuItem);
return menu;
}
public void enableRendererFromSmallMap(ActionEvent event)
throws AbortProcessingException
{
HtmlCommandJSCookMenu command = (HtmlCommandJSCookMenu) event
.getSource();
String id = (String) command.getValue();
System.out.println("id : " + id);
}
Here is the code of the jsp:
<td align="left"><t:jscookMenu layout="hbr" theme="ThemeIE">
<t:navigationMenuItems value="#{rendererList.navItems}" />
</t:jscookMenu></td>