首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActionListener不会在复合组件中触发

ActionListener不会在复合组件中触发
EN

Stack Overflow用户
提问于 2013-02-25 13:55:07
回答 1查看 1.2K关注 0票数 1

我有一个关于将ActionListener委托给复合组件(jsf)的具体问题。我创建了一个复合组件来显示(浏览)和编辑任何个人用户数据(属性为" user ")。

代码语言:javascript
复制
    <body>
        <co:interface>
            <co:attribute name="editAction"     required="false"  targets="edit"   method-signature="java.lang.String f()"/>
            <co:attribute name="saveAction"     required="false"  targets="save"   method-signature="java.lang.String f()"/>
            <co:attribute name="cancelAction"   required="false"  targets="cancel" method-signature="java.lang.String f()"/>
            <co:attribute name="isBrowseModus"  required="true"/>
            <co:attribute name="user"           required="true"/>
            <co:attribute name="isDeveloper"    required="true"/>
            <co:actionSource name="edit"/>
            <co:actionSource name="save"/>
            <co:actionSource name="cancel"/>
        </co:interface>
............
        <p:inplace id="inpLand" editor="true" 
            toggleable="#{not cc.attrs.isBrowseModus}" 
            style="#{cc.attrs.isBrowseModus ? 'color: black' : 'color: blue'}">  
                <p:inputText value="#{cc.attrs.user.land}" label="text"/>  
        </p:inplace>  
...........
       <c:if test="#{cc.attrs.isBrowseModus}">
         <h:commandButton id="edit" value="Edit"/>
       </c:if>
........

实际上,这个复合组件是通过调用myData.xhtml的视图调用的。

代码语言:javascript
复制
        <ui:define name="center">
            <h3>MyData Developer </h3>
            <h4>Welcomne</h4>
            <h:form id="dataForm">  
                <mc:UserData user="#{dataBean.user}" isDeveloper="#{dataBean.developer}" isBrowseModus="#{dataBean.browseModus}" cancelAction="#{dataBean.cancel}" 
saveAction="#{dataBean.save}" editAction="#{dataBean.edit}">
                    <f:actionListener for="edit" binding="#{dataBean.editActionListener}"/>
                </mc:UserData>   
            </h:form>  
        </ui:define>
    </ui:composition>

在复合组件中有一个(id = actionListener ),交付的myData(参见f:actionListener在myData中)应该绑定到这个commandButton (id =commandButton)。这是我在阅读不同论坛的讨论和几份文件后的理解。

不幸的是,在单击Edit之后,editActionListener方法没有被触发,我不知道为什么。当然,这个editActionListener方法存在于managedBean "dataBean“(myData.java)中:

代码语言:javascript
复制
public void editActionListener(ActionEvent ae) {
    browseModus = false;
    FacesContext.getCurrentInstance().renderResponse();
}    

我的目标是:在这个复合组件中,用户可以通过单击“编辑”按钮来更改任何用户数据。我更喜欢使用PRIMEFACES的内置标签。单击“编辑”按钮后,所有可更改的字段都应以蓝色显示。

我的问题:

  1. 有人能解释一下为什么没有触发有界的editActionListener方法吗?
  2. 通常,这是设置"isBrowseModus“属性(单击编辑按钮后为false )的正确方式吗?还是有更方便的方法来实现这一结果?
  3. 我正在使用primefaces,首先我尝试使用p:commandButton,但不幸的是它无法工作。在使用p:commandButton时,单击“编辑”按钮后会出现异常。JSF无法在我的editActionListener ( managedBean (PropertyNotFoundException)?!!?)中找到该方法。

任何帮助都会很感激的。在此之前,非常感谢您。你好,博多

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-25 17:18:24

根据标签文档<f:actionListener binding>必须引用实现ActionListener接口的具体实例,而不是方法表达式。

总之,您的问题可以通过通过getter提供ActionListener接口的具体实现来解决,而getter反过来又调用所需的动作侦听器方法。

代码语言:javascript
复制
private ActionListener editActionListener = new ActionListener() {
    @Override
    public void processAction(ActionEvent event) throws AbortProcessingException {
        DataBean.this.editActionListener(event);
    }
};

public ActionListener getEditActionListener() {
    return editActionListener;
}

public void editActionListener(ActionEvent event) {
    browseModus = false;
    FacesContext.getCurrentInstance().renderResponse();
}

然而,这是笨拙的,这表明您在寻找可能是错误的解决方案。一个更简单的方法是使用<cc:attribute targets="edit">

代码语言:javascript
复制
<cc:interface>
    ...
    <cc:attribute name="actionListener" targets="edit" />
</cc:interface>

它被用作

代码语言:javascript
复制
<mc:UserData ... actionListener="#{dataBean.editActionListener}" />
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15068602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档