使用Primefaces 3.1.1。
我想加两个日期值,减去两个日期。
<p:calendar widgetVar="Var1" id="ID1" value="#{Bean.Till}" pattern="HH:mm" timeOnly="true" mode="popup" showOn="button">
<p:calendar widgetVar="Var2" id="ID2" value="#{Bean.Late}" pattern="HH:mm" timeOnly="true" mode="popup" showOn="button">
在单击提交按钮时调用托管bean。
<h:commandButton value="Save" action=" #{timePickingBean.submitMethod}" >
<f:ajax execute="@form" render="SUM DIFFERENCE" />
</h:commandButton>
我的问题是:如何从managedbean(.java)文件中读取两个"ID"或"WIdgetVars",以便稍后将它们相加和相减,并将值存储回"SUM"one_answers"DIFFERENCE"?
提前谢谢!
-V
您可以将日历绑定到标识为timePickingBean的BackingBean。
类似这样的东西:
<p:calendar widgetVar="Var1" id="ID1" value="#{Bean.Till}" pattern="HH:mm" timeOnly="true" mode="popup" showOn="button" binding="#{timePickingBean.calendar1}"></p:calendar>
<p:calendar widgetVar="Var2" id="ID2" value="#{Bean.Late}" pattern="HH:mm" timeOnly="true" mode="popup" showOn="button" binding="#{timePickingBean.calendar2}"></p:calendar>
在你的支持豆中:
public class TimePickingBean {
private org.primefaces.component.calendar.Calendar calendar1;
private org.primefaces.component.calendar.Calendar calendar2;
public void submitMethod(AjaxBehaviorEvent event) {
Date cal1 = (Date)calendar1.getValue();
Date cal2 = (Date)calendar2.getValue();
do some stuff...
}
}
不要忘记控制Nullvalue以及getter和setter。但只有当你只需要在你的后台而不是其他地方使用这些日历时,这才有用。
另一种方法是使用PrimeFaces中的datechange事件触发计算。在这种情况下,在更改日历2的值时调用backingbean(您可以为日历1包含相同的值):
<p:calendar widgetVar="Var2" id="ID2" value="#{Bean.Late}" pattern="HH:mm" timeOnly="true" mode="popup" showOn="button" binding="#{timePickingBean.calendar2}">
<f:ajax event="dateSelect" execute="@form" render="SUM DIFFERENCE" action="#{timePickingBean.submitMethod}" ></f:ajax>
</p:calendar>