我正在开发一些 Java JSF 和 Richfaces 4 应用程序,我对所有这些技术(包括 Java)都是新手,所以你可以告诉我的一切,甚至是基础知识,都可以帮助我 :)。
我有
- 一个 a4j:commandButton 通过其 actionListener 属性触发一个 bean 方法
- 这个 bean 方法发布一个名为“updateGUI”的主题的 id 列表
- 回到视图文件,有一个 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;
}
再见