阿贾克斯监听器有条件地渲染元素中不工作 [英] ajax listener inside conditionally rendered element not working

查看:113
本文介绍了阿贾克斯监听器有条件地渲染元素中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个测试用例我的问题:

Here is a test case of my problem:

@ManagedBean
@ViewScoped
public class TestBean implements Serializable {
    private static final long serialVersionUID = -2329929006490721388L;

    private List<TestObject> testObjects;
    private int selectedId;

    public TestBean(){
        List<TestObject> to = new ArrayList<TestObject>();
        for(int i=0; i<10; i++){
            TestObject o = new TestObject(i, "object-"+i);
            to.add(o);
        }
        this.setTestObjects(to);
    }

    public void testAjaxListener(int id){
        System.out.println("testAjaxListener("+id+")");
        this.setSelectedId(id);
    }

    //+getters/setters
}

的TestObject

public class TestObject {
    private int id;
    private String name;

    public TestObject(int id, String name){
        this.setId(id);
        this.setName(name);
    }

    //+getters/setters
}

Test.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head></h:head>
<h:body>
    <h:form id="testForm">
        <h:panelGroup rendered="#{param['view'] eq 'test'}">
            <h2>DataTable</h2>
            <h:dataTable var="o" value="#{testBean.testObjects}">
                <h:column>
                    <h:commandLink value="#{o.name}" actionListener="#{testBean.testAjaxListener(o.id)}">
                        <f:ajax
                            render=":testForm:outputTest"
                        />
                    </h:commandLink>
                </h:column>
            </h:dataTable>
            <h2>output</h2>
            <h:outputText id="outputTest" value="#{testBean.selectedId}" />
        </h:panelGroup>
    </h:form>
</h:body>
</html>

现在的问题是,是的ActionListener不会解雇(我检查与System.out.print,你可以看到)。它工作正常,当我删除条件从panelGroup中呈现,所以我觉得这是问题 - 但我该如何解决? 我已经readed这些议题: <一href="http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked/2120183#2120183">h:commandLink / H:的commandButton没有被调用, <一href="http://stackoverflow.com/questions/18782503/fajax-inside-conditionally-rendered-custom-tag-backing-bean-method-not-invoke">f:ajax有条件地呈现自定义标签里面 - 后台bean的方法不会被调用 更多的人,但它并没有解决我的问题:(

The problem is, that actionListener won't firing (i'm checking that with System.out.print as you can see). It works fine when i remove conditional render from panelGroup, so i think that is the issue - but how can i fix it? I have readed those topics: h:commandLink / h:commandButton is not being invoked, f:ajax inside conditionally rendered custom tag - backing bean method not invoked and many more, but it didn't solve my problem :(

请帮忙

推荐答案

这是因为#{参数['观点'] EQ'测试'} 没有评估而JSF是忙于处理AJAX提交。然后,它还将征询渲染禁用只读再次属性为保障反对黑客攻击请求。 JSF即不包括在&LT请求参数;形式作用&gt; 由所生成的URL &LT; H:形式GT; 。这符合了<点5 href="http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked/2120183#2120183">h:commandLink / H:的commandButton没有被调用。

It's because the #{param['view'] eq 'test'} didn't evaluate true while JSF is busy processing the ajax submit. It will then also consult the rendered, disabled and readonly attributes once again as safeguard against hacked requests. JSF namely doesn't include the request parameters in the <form action> URL as generated by <h:form>. This matches point 5 of h:commandLink / h:commandButton is not being invoked.

有几种方法来解决这个问题。

There are several ways to get around this.

  1. 将其设置为视图的属性通过作用域的bean &LT; F:。viewParam&GT;

<f:metadata>
    <f:viewParam name="view" value="#{testBean.view}" />
</f:metadata>
...
<h:panelGroup rendered="#{testBean.view eq 'test'}">

  • 手动通过保留参数&LT; F:参数&GT; (!你需要把它在每一个提交操作):

  • Manually retain the param via <f:param> (you need to put it in every submit action!):

    <h:commandLink ...>
        <f:param name="view" value="#{param.view}" />
        ...
    </h:commandLink>
    

  • 替换&LT; H:形式GT; 通过 OmniFaces &LT;○:形式GT; 这能告诉JSF的,它必须提交的表格,包括请求参数的URL:

  • Replace <h:form> by OmniFaces <o:form> which is capable of telling JSF that it must submit the form to an URL including the request parameters:

    <o:form includeRequestParams="true">
        ...
    </o:form>
    

    • <一个href="http://stackoverflow.com/questions/17734230/retaining-get-request-query-string-parameters-on-jsf-form-submit/">Retaining获得JSF表单请求的查询字符串参数提交

    这篇关于阿贾克斯监听器有条件地渲染元素中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆