[SOLVED]Little help

UI Components for JSF
Post Reply
3man
Posts: 99
Joined: 16 Jun 2011, 16:13

05 Jul 2011, 21:10

Hello,
can someone help me with writing bean which is create dinamicly menu?
static_menu.xhtml

Code: Select all

<p:menu>
        <p:menuitem value="Menu1" action="#{menuBacking.active}" update="content">
            <f:setPropertyActionListener value="menu1" target="#{menuBacking.selection}"/>
        </p:menuitem>
        <p:menuitem value="Menu2" action="#{menuBacking.active}" update="content">
            <f:setPropertyActionListener value="menu2" target="#{menuBacking.selection}"/>
        </p:menuitem>
        <p:menuitem  value="Menu3" action="#{menuBacking.active}" update="content">
            <f:setPropertyActionListener value="menu3" target="#{menuBacking.selection}"/>
        </p:menuitem>
        <p:menuitem value="Menu4" action="#{menuBacking.active}" update="content">
            <f:setPropertyActionListener value="menu4" target="#{menuBacking.selection}"/>
        </p:menuitem>
    </p:menu>
MenuBean.java

Code: Select all

@ManagedBean
@SessionScoped
public class MenuBacking implements Serializable {
private MenuModel model;

    public MenuModel getModel() {
        return model;
    }

    private void MenuBacking() {
       initModel();
    }
    private void initModel() {
       model = new DefaultMenuModel();
       Submenu submenu;
        MenuItem item;

       FacesContext facesCtx = FacesContext.getCurrentInstance();
        ELContext elCtx = facesCtx.getELContext();
        ExpressionFactory expFact = facesCtx.getApplication().getExpressionFactory();

       while(i<5){
            item = new MenuItem();
            item.setId("m_"+i);
            item.setValue("menu"+i);
            item.setIcon("ui-icon ui-icon-document");
            item.setActionExpression(expFact.createMethodExpression(elCtx, "#{menuBacking.setActiveProgram('menu'+i)}", void.class, new Class[0]));
            item.setUpdate("content");
            item.setUrl("#");
            
            submenu.getChildren().add(item);
            model.addSubmenu(submenu);
        }
      }
dinamic_menu.xhtml

Code: Select all

    <p:menubar model="#{menuBacking.model}" styleClass="main_menuBar" autoSubmenuDisplay="false" />
, I have problem with this part:

Code: Select all

<f:setPropertyActionListener value="menu1" target="#{menuBacking.selection}"/>
Thanks in advance ...
Last edited by 3man on 10 Jul 2011, 14:16, edited 1 time in total.
-----------
primefaces-3.3-SNAPSHOT
tomcat 7.0.22
Mojarra 2.1.1 (FCS 20110408)
NetBeans 7.1

mbollman
Posts: 33
Joined: 07 Jun 2011, 10:29

06 Jul 2011, 10:32

Hello!

Code: Select all

<f:setPropertyActionListener value="menu1" target="#{menuBacking.selection}"/>
What is the type of menuBacking.selection ? Are you intending for it to be a String type, because you are currently sending it "menu1" as a String type.

Perhaps you mean to do this:

Code: Select all

<p:menuitem id="menuID1" value="Menu1" action="#{menuBacking.active}" update="content">
            <f:setPropertyActionListener value="#{menuID1}" target="#{menuBacking.selection}"/>

That said, if you have a backing bean MenuModel, can you not just bind the model directly:

Code: Select all

<p:menu model="#{menuBacking.model}" />
-Mark.

3man
Posts: 99
Joined: 16 Jun 2011, 16:13

06 Jul 2011, 10:55

Hello mbollman, thanks for replay
What is the type of menuBacking.selection ?
sorry, I didn't paste hole menuBacking file .. yes, menuBacking.selection is String type
That said, if you have a backing bean MenuModel, can you not just bind the model directly
yes, I do that:
<p:menubar model="#{menuBacking.model}" styleClass="main_menuBar" autoSubmenuDisplay="false" />
I'm trying to create static menu dinamicly ...
-----------
primefaces-3.3-SNAPSHOT
tomcat 7.0.22
Mojarra 2.1.1 (FCS 20110408)
NetBeans 7.1

mbollman
Posts: 33
Joined: 07 Jun 2011, 10:29

06 Jul 2011, 11:08

You're on the right track then.

Seems to me that you're not initialising the model object correctly. You can either do this in the constructor of the bean:

Code: Select all

public MenuBacking() {
    initModel();
}
Or, via lazy loading:

Code: Select all

private MenuModel model = null;
...
public MenuModel getModel(){
    if(this.model ==null){
         initModel();
    }
    return model;
}
I prefer the constructor method personally as you remove an if operation from the getter.


I've just re-read your original post and I think perhaps you've accidently put a void return type instead of creating a private constructor?

Code: Select all

    private MenuBacking() {
       initModel();
    }
- Mark.

3man
Posts: 99
Joined: 16 Jun 2011, 16:13

06 Jul 2011, 11:14

Hello,
this is a paste error, it stand:
public MenuBacking() {
initModel();
}
-----------
primefaces-3.3-SNAPSHOT
tomcat 7.0.22
Mojarra 2.1.1 (FCS 20110408)
NetBeans 7.1

mbollman
Posts: 33
Joined: 07 Jun 2011, 10:29

06 Jul 2011, 11:49

Hi,

Not entirely sure I'm understanding the problem of "static menu dynamically".

From what I gather, you're creating some menu items in the backing bean and using that model in the .xhtml page. You're also then trying to have some static elements (menu1 etc) already in the .xhtml page. You're wanting a composite of having both static and dynamically added? Maybe trying to have sme of the static items bound to some of the dynamically created ones?

Perhaps like this:

Code: Select all

<p:menubar model="#{testBean.model}">
	<p:submenu value="Menu1" label="labelx" binding="#{testBean.model.getSubmenus().get(0)}" >
		<p:menuitem value="subItem1" binding="#{testBean.model.getSubmenus().get(0).getChildren().get(arg0)}"  />
		<p:menuitem value="subItem2" />
	</p:submenu>
</p:menubar>

Code: Select all

@ManagedBean(name = "testBean")
@SessionScoped
public class TestBean {

	private MenuModel testModel = null;

	public TestBean() {
		System.out.println("constructing testbean....");

		testModel = new DefaultMenuModel();
		Submenu submenu = new Submenu();
		submenu.setLabel("myLabel");
		MenuItem item;
		for (int i = 1; i < 4; i++) {
			item = new MenuItem();
			item.setId("m_" + i);
			item.setValue("menu" + i);
			item.setIcon("ui-icon ui-icon-document");
			item.setUrl("#");

			submenu.getChildren().add(item);
		}
		testModel.addSubmenu(submenu);
		System.out.println("constructed testbean....");
	}

	public MenuModel getModel() {
		return this.testModel;
	}
You can provide better getters to access specific sub/menu items more directly instead of using the hack-ish get(0)'s as above.

-Mark.

3man
Posts: 99
Joined: 16 Jun 2011, 16:13

06 Jul 2011, 11:57

Hello,
as a test I create static menu ..

Code: Select all

<p:menu>
        <p:menuitem value="Menu1" action="#{menuBacking.active}" update="content">
            <f:setPropertyActionListener value="menu1" target="#{menuBacking.selection}"/>
        </p:menuitem>
        <p:menuitem value="Menu2" action="#{menuBacking.active}" update="content">
            <f:setPropertyActionListener value="menu2" target="#{menuBacking.selection}"/>
        </p:menuitem>
        <p:menuitem  value="Menu3" action="#{menuBacking.active}" update="content">
            <f:setPropertyActionListener value="menu3" target="#{menuBacking.selection}"/>
        </p:menuitem>
        <p:menuitem value="Menu4" action="#{menuBacking.active}" update="content">
            <f:setPropertyActionListener value="menu4" target="#{menuBacking.selection}"/>
        </p:menuitem>
    </p:menu>
and now I wont to replace this static menu with dinamic menu

Code: Select all

<p:menubar model="#{menuBacking.model}" styleClass="main_menuBar" autoSubmenuDisplay="false" />
and the problem is that I have no clue how to write this on bean side:

Code: Select all

<f:setPropertyActionListener value="menu4" target="#{menuBacking.selection}"/>

I'm sorry for didn't explain well ..
-----------
primefaces-3.3-SNAPSHOT
tomcat 7.0.22
Mojarra 2.1.1 (FCS 20110408)
NetBeans 7.1

mbollman
Posts: 33
Joined: 07 Jun 2011, 10:29

06 Jul 2011, 14:57

Hello,

No need for apology that last post makes it much clearer and I understand the problem now. :D

Sorr for the slower reply, been busy :( Framework of a solution follows:

Code: Select all

	private MenuModel testModel = null;

	public TestBean() {
		// TODO: build proper menumodel dynamially here:
        	testModel = new DefaultMenuModel();
		MenuItem item = new MenuItem();

		FacesContext context = FacesContext.getCurrentInstance();
		MethodExpression actionListener = context
				.getApplication()
				.getExpressionFactory()
				.createMethodExpression(context.getELContext(), "#{testBean.handlerX}", null,
						new Class[] { ActionEvent.class });
		item.addActionListener(new MethodExpressionActionListener(actionListener));
		item.setValue("myLabel");
	
		testModel.addMenuItem(item);

	}

	public MenuModel getModel() {
		return this.testModel;
	}

	public void handlerX(ActionEvent ae) {
		MenuItem itm = (MenuItem) ae.getSource();
		System.out.println("handlerx = " + itm.getValue());  // should print "mylabel".
	}
Will need some further work and adaptation to match your problem as above (I just put this together quickly as an example). Beware null pointers and casting exceptions. but hopefully should be enough to get you started once more.

Hope this helps,
- Mark.

3man
Posts: 99
Joined: 16 Jun 2011, 16:13

07 Jul 2011, 01:37

Hello again,
I test suggested code and it work well, however it doesn't work if menuModel have submenu, ex:

Code: Select all


FacesContext context = FacesContext.getCurrentInstance();
MethodExpression actionListener = context.
         getApplication().
         getExpressionFactory().
         createMethodExpression(context.getELContext(),
         "#{menuBacking.handlerX}",
         null,
         new Class[]{ActionEvent.class});

int i = 0;
submenu = new Submenu();
submenu.setId("s1");
submenu.setLabel("submenu1");
String val = "";
String id = "";
while(i<3){
      val = "menu"+i;
      id = "m"+i
      item = new MenuItem();
      item.setId(id);
      item.addActionListener(new MethodExpressionActionListener(actionListener));
      item.setValue(val);
      item.setOnclick("addTab(tabNav1, '" + val + "', '" + val + "', " + i + ")");
      submenu.getChildren().add(item);

      
      i++;
}
model.addSubmenu(submenu);
Last edited by 3man on 11 Jul 2011, 17:34, edited 1 time in total.
-----------
primefaces-3.3-SNAPSHOT
tomcat 7.0.22
Mojarra 2.1.1 (FCS 20110408)
NetBeans 7.1

3man
Posts: 99
Joined: 16 Jun 2011, 16:13

07 Jul 2011, 02:13

Sorry, it was some irrelevant mistake.

Thank you very much for helping me with this problem.
-----------
primefaces-3.3-SNAPSHOT
tomcat 7.0.22
Mojarra 2.1.1 (FCS 20110408)
NetBeans 7.1

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 29 guests