0

I'm usign Mojarra 2.1.23 and Primefaces 3.5. I'm trying to make this code work, but for some reason I can't get the listener to be invoked.

<h:form id="menu" >
    <p:growl id="messages" autoUpdate="true" showDetail="true" />
    <p:panelMenu>
        <p:submenu label="Ajax Menuitems">
            <p:menuitem value="#{MenuController.test}"
                actionListener="#{MenuController.save}" ajax="true" 
                update="messages" />
            <p:menuitem value="Update" actionListener="#{MenuController.save}"
                update="messages" />
        </p:submenu>
    </p:panelMenu>
</h:form>

MenuController is a session scoped bean managed by Spring 3 and even its properties are properly displayed on the same xhtml (button shows the text "Save").

The MenuController class is as follows:

public class MenuController() implements Serializable{

    public static Logger log;
    public String test="Save";

    public MenuController() {
        log = LoggerFactory.getLogger(this.getClass());
        log.debug("Hello MenuController.");
    }

    public void save(ActionEvent event) {  
        addMessage("Data saved");  
        log.debug(" saving");
    }

    public void addMessage(String summary) {  
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
            summary,  null);  
        FacesContext.getCurrentInstance().addMessage(null, message);  
    }
    //getters and setters...
}

I'm also using facelets, the following is Layout.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">

<h:head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

 <title><ui:insert name="title" /></title>
 </h:head>
 <h:body>
<div id="layout">       
<table>
<tr>
    <td>
    <div id="menu">
        <ui:include src="menu.xhtml"/>
    </div>

    </td>
    <td>
    <div id="content">
        <ui:insert name="content">              
        </ui:insert>
    </div>
    </td>   
</tr>
<tr>
<td>
    <div id="footer">
        <p><a href="#">© Footer</a></p>
    </div>
</td>
</tr>           
</table>
</div>

When I click the menu entry, the method is not being called.

pablocmg
  • 409
  • 2
  • 5
  • 11
  • `` is not closed. Is this a typo ? – Andy Jul 08 '13 at 04:46
  • sorry, it was a typo, it is closed in the original file. I just corrected it here – pablocmg Jul 09 '13 at 14:39
  • And you're not getting any error messages ? – Andy Jul 09 '13 at 16:20
  • I'm not sure what you are doing wrong. I tested your code and it works on my end. I was thinking maybe you had your id wrong in `update` for ``. It *should* have been `update=:menu:messages` but I was expecting an error. Weirdly it still works with just the value `messages`. I'll admit though I'm not that well versed with primefaces. – Andy Jul 09 '13 at 17:49
  • Another thing also is in your code you have methods and property defined in the constructor. I'm assuming that's a typo. No getter and setter for the property `test`. Again, assuming it's a typo. I'm going to give you an answer. Go through the list and try to see if you're not doing something that the author is describing. One sec, looking for it. – Andy Jul 09 '13 at 17:53
  • Here, go through this list http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked/2120183#2120183 or post more code I guess. Good luck. – Andy Jul 09 '13 at 17:55
  • Thats great help, thank you! most of the things you mentioned come from shortening my code to put it here, I will go through the list you just passed me and let you know how it goes! – pablocmg Jul 09 '13 at 22:31
  • Try removing the `ActionEvent` parameter from your methods used in the listener. – Luiggi Mendoza Jul 10 '13 at 23:20
  • I've gone pretty much thru all the list, and tried removing the actionEvent from parameters also, nothing seemed to work. I have to say hoewever I'm new to JSF and I'm not really sure how to test #10 and #11 from the link @Andy presented. – pablocmg Jul 11 '13 at 23:05
  • Try to post a complete code (smaller version) so I can see the big picture. – Andy Jul 12 '13 at 00:34
  • It works now!! ok, now I feel like I should've said this before, but I'm also using facelets, what I did to make it work was to rename form id from "menu" to "menuForm" and it started working :) – pablocmg Jul 12 '13 at 03:34
  • lol, good. Try to stick with JSF as much as you can with your code also. Instead of `` consider using `` and ``. Good luck with everything.
    – Andy Jul 12 '13 at 05:37
  • Thanks a lot! I will apply that also! – pablocmg Jul 12 '13 at 16:26
  • @Andy you can use plain HTML altogether with JSF. Usage of `` and/or `` may come in hand if you need to manipulate the components using pure JSF components like ``, but if not then define the layout with plain HTML to not overload the view state. – Luiggi Mendoza Nov 15 '13 at 15:53

1 Answers1

2

The problem was solved by changing the id on the form from "menu" to "menuForm", to not be confused by the one on the layout with id "menu"

<h:form id="menuForm" >
   <p:growl id="messages" autoUpdate="true" showDetail="true" />
    <p:panelMenu>
     <p:submenu label="Ajax Menuitems">
        <p:menuitem value="#{MenuController.test}"
            actionListener="#{MenuController.save}" ajax="true" 
            update="messages" />
        <p:menuitem value="Update" actionListener="#{MenuController.save}"
            update="messages" />
    </p:submenu>
   </p:panelMenu>
</h:form>
pablocmg
  • 409
  • 2
  • 5
  • 11