我正在使用 Java EE 6 编写应用程序并使用 Primefaces 3.4.1 作为用户界面。
我有类似的东西。
genericPage.xhtml
<ui:composition template="mainApplicationTemplate.xhtml" [...]>
<p:tabView id="tabView" dynamic="false"cache="false">
<p:tab id="tab1" title="Tab 1">
<h:form id="form1">
...
</h:form>
</p:tab>
<p:tab id="tab1" title="Tab 1">
<h:form id="form2">
...
</h:form>
</p:tab>
</p:tabView>
<ui:composition >
child1.xml
<ui:composition template="genericPage.xhtml" ...>
<ui:param name="actionBean" value="#{actionBeanA}"/>
</ui:compisition>
child2.xml
<ui:composition template="genericPage.xhtml" ...>
<ui:param name="actionBean" value="#{actionBeanB}"/>
</ui:compisition>
背后的想法是 child1.xhtml 和 child2.xhtml 共享相同的 jsf 代码,都包含在 genericPage.xhtml 中,但它们有不同的后端 bean(由“actionBean”参数化)
到目前为止,效果非常好。当我将 ui 参数放在 <p:ajax/>
中时,它变得复杂了元素。
在后端 bean 中,我需要以编程方式更新 Activity 选项卡,而其他选项卡保持不变。为此,我需要将 Activity 选项卡存储在操作 bean 中,当某些外部事件发生时,操作 bean 会更新 Activity 选项卡。
请注意,由于一些其他因素:
- 我无法设置
dynamic="true"
在tabView
- 我不能在
tabView
周围有一个全局表单因此不能使用“activeIndex”属性(我在应用程序的其他部分这样做)来管理 Activity 选项卡。
我想做什么
要解决这个问题,我想使用 tabChange
tabView
事件元素:
<p:tabView id="tabView" dynamic="false"cache="false">
<p:ajax event="tabChange" listener="#{actionBean.listen}"
<p:tab id="tab1" title="Tab 1">
<h:form id="form1">
...
</h:form>
</p:tab>
<p:tab id="tab1" title="Tab 1">
<h:form id="form2">
...
</h:form>
</p:tab>
</p:tabView>
Action bean
@Named
@WindowScoped
public class ActionBeanA implements Serializable{
public void listen(TabChangeEvent event){
...
}
}
什么不起作用
当我这样做时,我得到了错误
Target Unreachable, identifier 'actionBean' resolved to null: javax.el.PropertyNotFoundException: Target Unreachable, identifier 'actionBean' resolved to null
这似乎表明 <p:ajax>
元素尚未传递给操作 bean,因此不知道是什么 actionBean
是。
但是,如果我像那样更改监听器方法的签名
<p:ajax event="tabChange" listener="#{actionBean.listen(anything)}"
并将后端 bean 更改为:
public void listen(TabChangeEvent event){
System.out.println(event.toString());
}
这样做,我没有得到目标无法到达的错误,而是在 listen 方法中出现空指针异常(因为我没有为“任何东西”赋值)。这表明在这种情况下,<p:ajax/>
元素知道什么actionBean
是并设法调用 bean 中的方法。
问题
我怎样才能解决这个问题?我希望能够在选项卡更改事件中将新的 Activity 选项卡发送到我的后端 bean。
最佳答案
我也遇到了这个问题,并在 Primefaces 问题跟踪器中找到了解决方案:Solution found in the post #20
总而言之,这是 Primefaces 中的一个已知问题。半修复在版本 3.4.2 中找到,需要对代码进行一些更改。目前你有:
public void listen(TabChangeEvent event){
System.out.println(event.toString());
}
这是行不通的。您应该将代码更改为:
public void listen(AjaxBehaviorEvent event){
System.out.println(event.toString());
}
如果您需要使用 TabChangeEvent
的特定方法,那么您需要进行转换:(TabChangeEvent)event
。
它在问题跟踪器上的状态为已修复,因此可能需要将此作为永久解决方案。
关于java - JSF/Primefaces-用户界面 :param in p:ajax element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14460648/