用ajax JSF2复合组件链接 [英] JSF2 Composite component link using ajax

查看:121
本文介绍了用ajax JSF2复合组件链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的(简体)问题:
我有一个使用我的2复合材料部件的页面:
  - CCSelection
  - CCDisplay

在CCSelection,我有值的列表,每个人都有了啊:commandLink上
当点击一个链接,使用所选择的值的CCDiaplay组分是刷新。
要做到这一点,CCSelection公开直接链接在每个小时的方法属性:commandLink。该值是用F中给出的方法:属性
在该页面的支持豆,我有一个方法(这是考虑到CCSelection作为一个属性),即设置一个成员。
CCDisplay得到这个值,虽然一个抄送:属性通过网页的会员的获取
它的工作原理!
现在,我想ajaxize这种行为。

我trye​​d把一个f:ajax的每个H:commandLink在CCSelection ...但如果​​我把@form或@All在渲染属性,没有什么渲染(但setter方法​​被调用)。如果我把(页面)的UIComponent的id来渲染,我得到一个NullPointerException异常说,一个属性没有在CCDisplay定义NamingContainer。很奇怪,因为我并没有改变内部CCDisplay!
什么
我认为,解决的办法是把F:AJAX里面没有CCSelection但在页。
所以有可能是2种方法来实现这一目标:
  - CCSelection引发一个事件F:AJAX可以管理......但如何
?   - 使用CC:clientBehaviour为CCSelection。但它是有可能的目标更是1
成分(我有很多H:commandLink,但我只想要1个事件)
。   - 其他方式


这里是一个伪code

Here is my (simplified) issue :
I've got a page that is using 2 composite components of mine :
- CCSelection
- CCDisplay

In CCSelection, I have a list of values, each one has got a h:commandLink onto.
When clicking on a link, the CCDiaplay component is refreshed using the selected value.
To do this, CCSelection exposes a method attribute that is directly linked on each h:commandLink. The value is given to the method using f:attribute.
In the page backing bean, I've got a method (that is given to CCSelection as an attribute), that sets a member.
CCDisplay gets this value though an cc:attribute via the pages's member's getter.
It works !
Now, I want to ajaxize this behaviour.

I tryed to put an f:ajax for each h:commandLink in CCSelection... but if I put @form or @all in the render attribute, nothing is rendered (but the setter methods are called). If I put the id of the UIComponent (of the Page) to render, I get a nullpointerexception saying that a property is not defined for NamingContainer in CCDisplay. Quite strange because I didn't change anything inside CCDisplay !

I think the solution is to put the f:ajax not inside CCSelection but in Page.
So there may be 2 ways to achieve this :
- CCSelection raises an event f:ajax can manage... but how ?
- Using cc:clientBehaviour for CCSelection. But is it possible to target more that 1
component (I've got many h:commandLink, but I want only 1 event).
- Other ways ?


Here is a pseudo code

page.xhtml

page.xhtml

<myComp:ccSelection actionMethod="#{pageBean.select}"
                    render="#{clientIDHelper.clientId['display']}" />
<h:panelGroup id="diplay" binding="#{clientIDHelper.bindings['display']}">
  <myComp:ccDisplay value="#{pageBean.value}" />
</h:panelGroup>

要回收含有ccDiaplay复合组件面板的全部客户端ID,我用描述的clientIDMap工艺<一href="https://www.xing.com/net/jsf/newsletters-archive-362954/group-newsletter-the-map-hack-26501345/"相对=nofollow>这里。

To recover the full clientid of the panel containing the ccDiaplay composite component, I use a clientIDMap technic described here.

PageBean.java

PageBean.java

private String _value;

public String getValue() { return _value; }
public void setValue(String value) [ _value = value; }

public void select(String value) {
  setValue(value);
}

ccSelection.xhtml

ccSelection.xhtml

<cc:interface>
  <cc:attribute method-signature="void selectAction(String)"
                name="actionMethod" />
  <cc:attribute name="render" />
</cc:interface>
<cc:implementation>
  <t:dataTable value="#{cc.values}"
               var="val"
               ...
               >
    <h:column>
       <t:commandLink actionListener="#{cc.selectionValueListener}"
         <f:ajax render="#{cc.attrs.render}" />
         <f:attribute name="value"
                      value="#{val}" />
       </t:commandLink>
    </h:column>
  </t:dataTable>
</cc:implementation>

ccSelection.java

ccSelection.java

public void selectionValueListener() {
  // recover the attribute value
  String value = event.getComponent().getAttributes().get("value");
  // call the callback method of the page
  FacesContext context = FacesContext.getCurrentInstance();
  MethodExpression method = (MethodExpression) this.getAttributes().get("actionMethod");
  if (method != null)
    method.invoke(context.getELContext(), new Object[] {value});
}

我不认为ccDisplay是interressting。

所以,如果我不把F:AJAX标签,它的工作原理。 当我把F:Ajax和渲染指着参数通过客户端ID,我得到一个错误,而加载页面。 如果我改变渲染的@form或@All,该pageBean.select方法被调用,但ccDisplay不会刷新。

I don't think ccDisplay is interressting.

So, if I don't put the f:ajax tag, it works. When I put the f:ajax with the render pointing to the clientId passed in param, I get an error while loading the page. If I change the render for @form or @all, the pageBean.select method is called, but ccDisplay is not refreshed.

推荐答案

确定。它解决了......但我不很喜欢。
你会认为我是一个傻瓜:我通过删除的&lt解决了这个问题; [CDATA包围我的脚本
! 我已经发现了一些问题,使用CDATA。我不知道这是否是一个MyFaces的错误或东西,我做了错误的方式就像把许多H:outputScript块与CDATA复合成分,但与CDATA,我得到的错误或不工作。只是删除它,它的作品!

OK. It is solved... but I don't like it very much.
You'll think I'm a fool : I solved the problem by removing the <![CDATA surrounding my scripts !
I've already found some issue using CDATA. I don't know if this is a MyFaces bug or something I do the wrong way like putting many h:outputScript blocks with CDATA in composite components but with CDATA, I get errors or not working. Just removing it, it works !

这篇关于用ajax JSF2复合组件链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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