【问题标题】:JSF setpropertyactionlistener not being called未调用 JSF setpropertyactionlistener
【发布时间】:2013-12-18 15:23:56
【问题描述】:

我在数据表中有一个 f:setPropertyActionListener,单击命令按钮时未调用 setpropertyaction 侦听器。有人知道我哪里出错了吗?

谢谢

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

在我的 bean 中,我有以下 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();

    }

【问题讨论】:

    标签: jsf jsf-2 primefaces


    【解决方案1】:

    这里的主要问题是调用的顺序,首先你的actionListener 被调用然后 f:setPropertyActionListener.

    要解决此问题,请将您的 actionListener 更改为 action,并更新您的操作方法以返回一个字符串。

    按钮

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

    更新操作

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

    或

    您可以继续使用您的 actionListener 并将 selectedEmail 传递给它(不使用 f:setPropertyActionListener)。

    按钮

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

    更新操作

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

    了解更多调用顺序

    见:

    【讨论】:

    • 没有将属性设置为@this 不起作用。
    • 当行为被禁止时你会怎么做,导致你构建 ajax UI 并且只使用 f:ajax 监听器?
    • @mondjunge “禁止行动”是什么意思?
    • @HatemAlimam 操作通常会触发 webflow 中的转换,从而导致页面重新加载。您也不能将参数传递给您尚未编写的 bean。您需要特别为 ajax 调用编写它们。因此,对 ajax 调用使用 action 非常麻烦,而且在大型项目中不太实用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2012-12-19
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    相关资源
    最近更新 更多