作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 JSF-Compnents 构建了一个短页面,它显示并递增来自 @ConversationScoped Bean 的值。此页面能够结束对话,并在结束旧对话后获得新的 Bean。这是它的样子:
<!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:p="http://primefaces.org/ui">
<h:head />
<h:body>
<h:form>
<h:outputText id="i" value="#{test.i}" />
<h:commandButton value="increment"
actionListener="#{test.increment}"
update="i cid" />
<h:outputText id="cid"
value="#{javax.enterprise.context.conversation.id}" />
<h:commandButton value="end"
actionListener="#{test.endConversation}"
update="i cid" />
</h:form>
</h:body>
</html>
Bean 的代码非常简单:
package de.burghard.britzke.test.beans;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
@Named
@ConversationScoped
public class Test implements Serializable {
@Inject Conversation conversation;
private int i;
@PostConstruct
public void init() {
conversation.begin();
System.out.println("init conversation"+conversation.getId());
}
public int getI() { return i; }
public void setI(int i) { this.i = i; }
public void increment() { i++;System.out.println(i); }
public void endConversation() {
System.out.println("ending conversation "+conversation.getId());
conversation.end();
}
}
当使用标准的h:commandButton 组件时一切正常。但是使用 Primefaces 组件 p:commandButton 然后每次点击“增量”按钮都会抓取一个新的 Bean 实例,因为 cid 参数没有传递给服务器。已经声明这不是 primefaces 问题。但为什么它使用标准组件而不是 primefaces 组件呢?我已经能够通过将 f:param 组件显式嵌入 commandButton 组件来传递 cid-Parameter,但是在破坏 Conversation 之后,发送的参数没有任何值,从而产生错误。但它甚至可以在没有 f:param 组件的情况下工作。
是否有关于如何使用 Primefaces 和 Conversation 作用域 bean(无需明确传递 cid 参数)的简短教程?
最佳答案
f:param
在对话结束时的问题是,不应为参数 cid
传递无效值(null)的参数。因此,使用以下代码更正此问题效果很好:
<!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:p="http://primefaces.org/ui">
<h:head />
<h:body>
<h:form id="form">
<h:outputText value="#{test.i}" />
<p:commandButton value="increment"
actionListener="#{test.increment}"
update="@parent">
<f:param
name="#{not empty javax.enterprise.context.conversation.id?'cid':''}"
value="#{javax.enterprise.context.conversation.id}" />
</p:commandButton>
<h:outputText
value="#{javax.enterprise.context.conversation.id}" />
<p:commandButton value="end" actionListener="#{test.endConversation}"
update="@parent">
<f:param name="cid" value="#{javax.enterprise.context.conversation.id}" />
</p:commandButton>
</h:form>
</h:body>
</html>
但我希望 primefaces 组件能够进行对话处理,而无需页面程序员显式传递 f:param
。它们的行为应该像标准 JSF (MyFaces) 组件那样。
关于jsf - 如何让 Primefaces 与 @ConversationScoped Beans 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11702297/
我是一名优秀的程序员,十分优秀!