0

我正在开发一些 Java JSF 和 Richfaces 4 应用程序,我对所有这些技术(包括 Java)都是新手,所以你可以告诉我的一切,甚至是基础知识,都可以帮助我 :)。

我有

  1. 一个 a4j:commandButton 通过其 actionListener 属性触发一个 bean 方法
  2. 这个 bean 方法发布一个名为“updateGUI”的主题的 id 列表
  3. 回到视图文件,有一个 a4j:push,它监听这个“updateGUI”主题,有一个子节点 a4j:ajax,事件“dataavailable”

这3点有效。我唯一的问题是我希望 a4j:ajax 呈现包含在 javascript event.rf.data 中的 id,但我没有成功。如果我手动编写 id,而不是使用 event.rf.data,它们会被渲染。

这是xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich" template="/templates/template.xhtml">
  <ui:define name="title">RichFaces Sample</ui:define>
  <ui:define name="body">
    <a4j:push address="updateGUI">
      <!-- here, instead of render="main butt0" (the added button and the parent node),
           I'd like to use event.rf.data, a javascript variable containing the ids
           I don't want to call a bean's getter, since the bean could have created/
           updated/deleted other ids, and we could miss old updated ids, and also
           getters are called multiple times (so can't make a FIFO of updated ids) -->
      <a4j:ajax event="dataavailable" limitRender="true" render="main butt0" />
    </a4j:push>
    <h:form id="main">
      <h:inputText value="#{pushtest.text}">
    <a4j:ajax event="change" />
      </h:inputText>
      <a4j:commandButton value="create a new button!" actionListener="#{pushtest.makemodifthread}" />
      <a4j:outputPanel id="status">
        last updated ids: #{pushtest.logupdated_ids}
      </a4j:outputPanel>
      <br />
    </h:form>
  </ui:define>
</ui:composition>

及其模板/模板.xhtml:

<!DOCTYPE html>
<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:ui="http://java.sun.com/jsf/facelets" xmlns:a4j="http://richfaces.org/a4j">

<h:head>
    <title>RichFaces Application</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </h:head>

<h:body>
        <ui:insert name="body">Default content</ui:insert>
</h:body>
</html>

这是豆子:

package unit1;

import java.util.Date;
import java.lang.Thread;
import java.lang.Runnable;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;

import javax.faces.bean.ViewScoped;

import org.richfaces.application.push.MessageException;
import org.richfaces.application.push.TopicKey;
import org.richfaces.application.push.TopicsContext;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.richfaces.component.UICommandButton;
import javax.faces.event.ActionEvent;

@ManagedBean
@ViewScoped
public class pushtest {
    // from http://illegalargumentexception.blogspot.fr/2009/02/jsf-working-with-component-ids.html
    private UIComponent findComponent(UIComponent c, String id) {
    if (id.equals(c.getId())) {
    return c;
    }
    java.util.Iterator<UIComponent> kids = c.getFacetsAndChildren();
    while (kids.hasNext()) {
    UIComponent found = findComponent(kids.next(), id);
    if (found != null) {
        return found;
    }
    }
    return null;
    }

    public void makemodifthread(ActionEvent event) throws MessageException
    {
    UICommandButton butt = new UICommandButton();
    updated_ids = "butt"+ count;
    butt.setId("butt" + count);
    count++;
    butt.setValue(this.text);
    UIComponent parent = findComponent(FacesContext.getCurrentInstance().getViewRoot(), "main");
    parent.getChildren().add(butt);
    updated_ids += " " + parent.getId();
        TopicKey topicKey = new TopicKey("updateGUI");
        TopicsContext topicsContext = TopicsContext.lookup();
        topicsContext.publish(topicKey, updated_ids);
    }

    public String getLogupdated_ids()
    {
    System.out.println("LOG updated ids: " + log_updated_ids);
    return (log_updated_ids);
    }

    public String getUpdated_ids()
    {
    System.out.println("updated ids: " + updated_ids);
    if (!updated_ids.isEmpty())
    {
        log_updated_ids = updated_ids;
        updated_ids = "";
        return (log_updated_ids);
    }
    return (updated_ids);
    }

    public void setText(String text)
    {
    this.text = text;
    }

    public String getText()
    {
    return (text);  
    }

    private String text = "button name";
    private String updated_ids = "";
    private String log_updated_ids = "";
    private int count = 0;
}

再见

4

1 回答 1

0

实际上,我以更简单的方式解决了这个问题,这要归功于:http: //www.coderanch.com/t/562602/JSF/java/assign-JSF-Ajax-Render-attribute#post_text_2554972

FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add(target.getClientId());

在 bean 中,我现在可以添加/更新 jsf 节点,而不必担心 websocket、id getter 或其他奇怪的东西。

编辑:实际上,这个解决方案并不完美,我需要将它与 a4j:push 结合起来。下周有更多信息!

于 2013-07-05T12:13:43.080 回答