【问题标题】:two rich:ajax conflict problems (in 3 rich:select controls)两个 rich:ajax 冲突问题(在 3 rich:select 控件中)
【发布时间】:2012-03-11 23:14:07
【问题描述】:

我有 3 个 Rich:select 控件,id 分别为 select1、select2、select3。我想单击 select1,将呈现控件“select2”,当我单击控件 select2 时,将呈现 select3,如下所示。我通过 netbeans 7.0.1 制作了我的应用程序,并使用了框架 jsf 2.0 和richFaces 4.2.0。

我的解决方案是创建两个具有 id ajax1 和 ajax2 的 a4j:ajax,根据 select1 和 select2 selectItem 事件分配 select2 和 select3 的渲染。我已将 ajax1 和 ajax2 安排在一个 id 为 queue1 的队列中。

这是页面的代码:

<h:form>
    <a4j:queue name="queue1"  onerror="window.alert('alert in queue');" onrequestdequeue="window.alert('queue1 dequeue');" onrequestqueue="window.alert('queue1 enqueue');" requestDelay="1000" ignoreDupResponses="true"/>
     <h3 style="text-align: center">Test case 1: 3 selection controls problem</h3>

     <br/><br/>
    <h:outputLink value="../index.xhtml">View Index </h:outputLink>
    <br/><br/>
     Problem: <h:outputText escape="false" value="#{carBean.testDescription}"/>
    <br/><br/>
    <table>
        <tr>
            <td>Car Firm (selection1)</td>
            <td>Car type (selection2)</td>
            <td>Production (selection3)</td>
        </tr>
        <tr>
            <td>
        <rich:select id="select1" value="#{carBean.companyName}" maxListHeight="100"  enableManualInput="true"  defaultLabel="Type here">      
            <a4j:ajax id="ajax1" queueId="queue1" execute="@form" render="select2, errorText" event="selectitem"  listener="#{carBean.changeCompanyEvent()}">

            </a4j:ajax>                                
            <f:selectItems value="#{carBean.lstCompany}"></f:selectItems>
        </rich:select>
        </td>
        <td>
        <rich:select id="select2" value="#{carBean.typeName}"  maxListHeight="100"  enableManualInput="true" defaultLabel="Type here"  >      
            <a4j:ajax id="ajax2" queueId="queue1"  immediate="true"  execute="@form" render="select3, errorText" event="selectitem"  listener="#{carBean.changeCarTypeEvent()}">

            </a4j:ajax>                                
            <f:selectItems value="#{carBean.lstCarType}"></f:selectItems>
        </rich:select>
        </td>
        <td>
        <rich:select id="select3" value="#{carBean.productionName}" 
                     maxListHeight="100"  enableManualInput="true" defaultLabel="Type here"  >                                              
            <f:selectItems  value="#{carBean.lstCarProduction}"></f:selectItems>
        </rich:select>
        </td>
        </tr>
    </table>
    <br/><br/>
    <h:outputText id="errorText" escape="false" value="#{carBean.alertError}"/>

</h:form>

遇到的问题:我有2个问题还没找到原因:

  • select2 必须设置 immediate = true,否则 ajax2 中定义的事件不会完成。

我不知道这是什么原因。

  • 当我为 select2 设置 immediate = true 时,调用了 ajax2 事件。但是作为select2控件赋值的typeName变量仍然没有赋值,所以select3还没有赋值。

这是错误图片:http://i970.photobucket.com/albums/ae190/swenteiger7/richFaces%20error-%20110312/testcase1Error.png

应用程序:我还发送了我的应用程序项目(由 net beans IDE 打开)来解决这个问题。你可以专注于3个项目: - Web/Web-INF 文件夹下的 faces-config.xml 文件,定义了 CarBean 管理的 Bean - web 文件夹中的 testcase1 文件夹(其中包含三个SelectExample.xhtml)。 - beans 包和 utils 包保存托管 bean(在 testcase1 中我们关注 CarBean 托管 Bean)。

您可以在我的附件中下载我的存档项目。请添加 JSF2.0 和richFaces 框架来运行应用程序(https://community.jboss.org/wiki/HowToAddRichFaces4xToProjectsNotBasedOnMaven)

这是我的申请:http://www.mediafire.com/?fnab3824b8vwd93

如果下载有问题,请联系我。谢谢。

【问题讨论】:

    标签: jsf jsf-2 richfaces


    【解决方案1】:

    rich:select 也有类似的问题。为了解决这个问题,我们必须更改为 h:selectOneMenu 并为 onchange javascript 函数添加 ajax 功能。另外,当您使用 ajax 方法时,您应该在 h:selectOneMenu 中使用 valueChangeListener 来设置所选值,而不是 value 属性。

    我会给你一个样品

    <h:selectOneMenu id="select1" maxListHeight="100" 
        enableManualInput="true" defaultLabel="Type here"
        valueChangeListener="#{carBean.changeCompanyName}">
        <f:selectItems value="#{carBean.lstCompany}"></f:selectItems>
        <a4j:ajax id="ajax1" queueId="queue1" execute="@form"
            render="select2, errorText" event="selectitem"
            listener="#{carBean.changeCompanyEvent()}">
    </h:selectOneMenu>
    

    在您的 ManagedBean 中,您应该有这样的代码:

    @ManagedBean(name="carBean")
    @ViewScoped
    public class CarBean implements Serializable {
        private String companyName;
        //getters and setter...
        //constructor and other functions
        public void valueChangeMethod(ValueChangeEvent e){
            companyName = (String)e.getNewValue();
        }
    }
    

    RichFaces 论坛中有一个帖子about this issue。此外,还有一篇关于 populating child menu's 的 BalusC(JSF 专家)博客文章。

    【讨论】:

    • 非常感谢您的精彩回答。我会检查它:)
    • 你好路易吉。我在 3 天前尝试了你的建议。我仍然不能使用 2 ajax 事件。当我使用 2 个 时出现问题,不幸的是同时使用
    猜你喜欢
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2019-04-19
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多