我不确定如何正确地提出这个问题,但我会尝试这样做: 问题是关于Primefaces,JSF2 Calendar in composite ..我想捕获一个在该日历被更改后调用的事件(并捕获其新的Date值)。
我的复合xhtml:
<composite:interface componentType="myComponent">
<composite:attribute name="value" required="true"/>
</composite:interface>
<composite:implementation>
<p:calendar
id="tempCalendar"
pattern="dd.MM.yyyy" value="#{cc.attrs.value}"
valueChangeListener="#{cc.valueChanged}"
validator="DateValidator" converter="MyDateConverter" showOn="button" showButtonPanel="true" navigator="true" >
<p:ajax event="dateSelect" update="@this" listener="#{cc.event1}"/>
</p:calendar>
</composite:implementation>
我的复合豆:
public void valueChanged(Object event) {
log("valueChanged");
}
public void event1(AjaxBehaviorEvent ab) {
log("Event1");
if (ab != null && ab.getSource() != null && ab.getSource() instanceof Calendar) {
//....
}
}
我正在使用复合材料的页面:
<cc:inputdate value="#{mainBean.aDate}" />
在上面的代码中,我试图在compotents bean中捕获新值,但是log看起来像这样:
的valueChanged
事件1
setADate
当我在valueChangedListener时,我仍然有旧的日历值。最后设置了新值。
所以,首先我希望在我的复合bean中有新的价值..但我的主要问题是:
如何在我的mainBean中实现一个事件,该事件会在更改时捕获该日历的新值?
编辑:我现在的复合材料:
<composite:interface componentType="myComponent">
<composite:attribute name="value" required="true"/>
<composite:attribute
name="myListener"
method-signature="void listener()" />
</composite:interface>
<composite:implementation>
<h:panelGroup id="container">
<p:calendar
value="#{cc.attrs.value}"
valueChangeListener="#{cc.valueChanged}"
<p:ajax event="dateSelect" update="@this,:buttonpanel" listener="#{cc.attrs.myListener}"/>
</p:calendar>
</h:panelGroup>
</composite:implementation>
这样我在主页面中调用它(与mainBean连接):
<cc:inputdate
value="#{mainBean.item.myDate}"
myListener="#{mainBean.event1}"/>
我想在mainBean.java改变之后抓住evet ......
答案 0 :(得分:2)
您可以从组件的本地值访问它。 即,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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:p="http://primefaces.org/ui">
<f:view>
<h:head />
<h:body>
<h:form>
<p:calendar pattern="dd.MM.yyyy">
<p:ajax event="dateSelect" process="@this"
listener="#{testBean.event1}" />
</p:calendar>
</h:form>
</h:body>
</f:view>
</html>
import org.primefaces.component.calendar.Calendar;
@ManagedBean(name="testBean")
@SessionScoped
public class TestBackingBean
{
public void event1(AjaxBehaviorEvent ab)
{
if (ab != null)
{
Calendar calendar = (Calendar) ab.getSource();
if(calendar != null)
{
System.out.println(String.format("Newly selected value: %s",
calendar.getLocalValue()));
}
}
}
}
答案 1 :(得分:2)
您已将其指定为复合的value
属性,因此它应该可以通过继承的UIComponent#getAttributes()
在任何支持组件的方法中使用,如下所示:
Object value = getAttributes().get("value");