user1107753
user1107753

Reputation: 1636

JSF setter called multiple time overwriting values

Hi I am using PrimeFaces to create a data table which has 1 column within it and each row of that datatable holds a accordian. i want to update a specific row within the data table with the values from some input boxes associated with each row.

When I submit the form per row firstly the set property action listener is never called and secondly the setters for the fields I am updating are called multiple times. In fact they are called once for every row in the data table.

The problem being that when debugging I can see that no matter which ever row I am editing the setters are called for all the rows in the datatable. So unless i am editing the last row the values I am setting always get overwritten. Any one know whey this is happening as i thought it would only have called the setter once per row edit.

UPDATE: Worst case i can get around the setters being called by putting a null check in. however I do not know which row I am working with since the set property action listener is not called

JSF page

<?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>

    <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="#{emailArticleBean.emailAction}" cols="90" rows="3" />                          
                            </div>
                            <h6 style="font-weight:bold;">Due Date</h6>  
                            <p:calendar value="#{emailArticleBean.actionDueDate}" 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>

My backing bean

@ManagedBean
@Configurable
@ViewScoped
public class EmailArticleBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    EmailArticleService emailArticleService;

    private List<EmailArticle> emailArticles;
    private String searchText;
    private String emailAction;
    private EmailArticle selectedEmail;
    private Date actionDueDate;

    /**
     * Using the search term entered will
     * query the database and return a list of articles 
     * that match that parameter
     * @param ae
     */
    public void search(ActionEvent ae) {
        emailArticles = emailArticleService.findEmailArticles(searchText);
    }   

    /**
     * add the inputted action to the list of existing actions
     * @param ae
     */
    public void updateAction(ActionEvent ae) {
        selectedEmail.getActions().add(new EmailActions(emailAction, "testUser", actionDueDate));
        selectedEmail.merge();
        emailAction = "";
        actionDueDate = null;
    }

    public List<EmailArticle> getEmailArticles() {
        return emailArticles;
    }

    public void setEmailArticles(List<EmailArticle> emailArticles) {
        this.emailArticles = emailArticles;
    }

    public String getSearchText() {
        return searchText;
    }

    public void setSearchText(String searchText) {
        this.searchText = searchText;
    }

    public String getEmailAction() {
        return emailAction;
    }

    public void setEmailAction(String emailAction) {
        //putting this check in because it the jsf lifecycle
        //seems to be calling the setter once for every row overwriting the new value set by the user
        if(StringUtils.isEmpty(this.emailAction)) {
            this.emailAction = emailAction;
        }       
    }   

    public EmailArticle getSelectedEmail() {
        return selectedEmail;
    }

    public void setSelectedEmail(EmailArticle selectedEmail) {
        if(selectedEmail == null) {
            this.selectedEmail = selectedEmail;
        }       
    }

    public Date getActionDueDate() {
        return actionDueDate;
    }

    public void setActionDueDate(Date actionDueDate) {
        if(this.actionDueDate == null) {
            this.actionDueDate = actionDueDate;
        }       
    }


}

Upvotes: 0

Views: 1855

Answers (1)

BalusC
BalusC

Reputation: 1108577

You're indeed binding the value of the input field of all rows to one and same bean property.

<p:dataTable var="email" value="#{emailArticleBean.emailArticles}" ...>
    ...
    <h:inputTextarea value="#{emailArticleBean.emailAction}" ... /> 

Logically, this makes no sense. The symptoms which you described is exactly the expected consequence. You should be binding the value of the input field to the currently iterated row.

<p:dataTable var="email" value="#{emailArticleBean.emailArticles}" ...>
    ...
    <h:inputTextarea value="#{email.emailAction}" ... /> 

Upvotes: 4

Related Questions