0

I have a f:setPropertyActionListener within a data table and clicking on a command button the setpropertyaction listener is not being called. Any one see where I am going wrong?

thanks

 <?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:ui="http://java.sun.com/jsf/facelets">
      <h:form id="userDetailsForm" style="padding:5px"> 
        <p:growl id="messages" showDetail="true" autoUpdate="true" life="2000"/>
        <p:spacer height="15"></p:spacer>

    <div class="row">     
      <div class="col-lg-4">
        <div class="input-group">
          <p:inputText type="text" styleClass="form-control" value="#{emailArticleBean.searchText}" />
          <span class="input-group-btn" style="margin:3px;">
            <p:commandButton actionListener="#{emailArticleBean.search}" value="Go!" update=":userDetailsForm:emailArticleTable" />
          </span>
        </div><!-- /input-group -->
      </div><!-- /.col-lg-6 -->
    </div><!-- /.row -->

    <p:spacer height="15"></p:spacer>
    <h:messages/>

    <div class="row">
        <div class="col-lg-1">

        </div>
        <div class="col-lg-11">
        <p:dataTable var="email" value="#{emailArticleBean.emailArticles}" scrollRows="20"  
                    scrollable="true" liveScroll="true" scrollHeight="750" id="emailArticleTable" 
                    >
            <p:column>
                <p:accordionPanel multiple="true" activeIndex="-1" id="panel#{email.id}">  

                    <p:tab title="#{email.headline}" titleStyleClass="email-header">  
                           <div style="clear:both;margin-bottom:10px;">
                           <h6 style="font-weight:bold;">Summary</h6>
                            <h:outputText  
                                value="#{email.summary}" />     
                            </div>  
                            <div style="clear:both;margin-bottom:10px;">
                            <h6 style="font-weight:bold;">Implication</h6>
                            <h:outputText  
                                value="#{email.implication}" />     
                            </div>           
                            <div style="float:left;clear:both">
                                <p:commandButton  value="View Existing Actions" 
                                    oncomplete="PF('dlg2').show();" update=":userDetailsForm:emailActionDialog">
                                     <f:setPropertyActionListener value="#{email}" target="#{emailArticleBean.selectedEmail}" />  
                                </p:commandButton>                  
                             </div> 
                             <br/>
                             <br/>
                             <div style="margin-top:10px;">        
                                <h:inputTextarea id="accordian1" value="#{email.tempAction}" cols="90" rows="3" />                          
                            </div>
                            <h6 style="font-weight:bold;">Due Date</h6>  
                            <p:calendar value="#{email.tempDate}" id="popupCal" pattern="dd MMM, yyyy"/> 
                            <p:commandButton actionListener="#{emailArticleBean.updateAction}" value="Add Action" 
                                 style="margin-left:5px;">
                                <f:setPropertyActionListener value="#{email}" target="#{emailArticleBean.selectedEmail}" />
                            </p:commandButton> 

                    </p:tab>
                </p:accordionPanel>
            </p:column>
        </p:dataTable>
        </div>

    </div> 

    <p:dialog id="emailActionDialog" header="Email Actions" widgetVar="dlg2" modal="true" height="100">  
        <h3>Email Actions</h3>  
        <p:dataList value="#{emailArticleBean.selectedEmail.actions}" var="action" itemType="disc">  
            #{action.action} --- #{action.username}  
        </p:dataList>  
    </p:dialog> 



    </h:form>   



</html>

In my bean I have the following getter/setter

public EmailArticle getSelectedEmail() {
        return selectedEmail;
    }

    public void setSelectedEmail(EmailArticle selectedEmail) {

        this.selectedEmail = selectedEmail;

    }

public void updateAction(ActionEvent ae) {
        selectedEmail.getActions().add(new EmailActions(emailAction, "testUser", actionDueDate));
        selectedEmail.merge();

    }
user1107753
  • 1,566
  • 4
  • 24
  • 36

1 Answers1

2

The main problem here is the order of the invocation, first your actionListener is called then f:setPropertyActionListener.

To solve this, change your actionListener to action, and update your action method to return a String.

Button

   <p:commandButton action="#{emailArticleBean.updateAction}" 
                    value="Add Action"  style="margin-left:5px;">
           <f:setPropertyActionListener value="#{email}" 
              target="#  {emailArticleBean.selectedEmail}" />
   </p:commandButton> 

updateAction

public String updateAction() {
    selectedEmail.getActions().add(new EmailActions(emailAction, "testUser", actionDueDate));
    selectedEmail.merge();
    return null;
}

OR

you can keep using your actionListener and pass the selectedEmail to it (without using f:setPropertyActionListener).

Button

<p:commandButton actionListener="#{emailArticleBean.updateAction(email)}" 
                 value="Add Action"  style="margin-left:5px;">
</p:commandButton>

updateAction

public void updateAction(EmailArticle selectedEmail) {
    selectedEmail.getActions().add(new EmailActions(emailAction, "testUser", actionDueDate));
    selectedEmail.merge();
}

To understand more the order of the invocation

See:

Community
  • 1
  • 1
Hatem Alimam
  • 9,968
  • 4
  • 44
  • 56
  • No setting the property to @this did not work. – user1107753 Dec 19 '13 at 13:51
  • what do you do when action is forbidden, cause you build ajax UI and only use f:ajax listener? – mondjunge Dec 01 '16 at 11:52
  • @mondjunge what do you mean "action is forbidden" ? – Hatem Alimam Dec 01 '16 at 13:26
  • @HatemAlimam action normally trigger transitions in webflows what leads to reloading of the page. Also you cannot pass parameters to beans you have not written. You need to write them especially for the ajax call. Using action for ajax calls is therefore quite a hassle and not very practicable in huge projects. – mondjunge Dec 05 '16 at 08:50