如何从ManagedBean访问小部件 [英] How to access widgets from ManagedBean

查看:53
本文介绍了如何从ManagedBean访问小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Primefaces 3.1.1.

Using Primefaces 3.1.1.

我想添加两个日期值,然后减去两个日期值.

I would like to add two date values and subtract two date values.

<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.

A managed bean is called on the click of a submit button.

<h:commandButton value="Save" action=" #{timePickingBean.submitMethod}" >
<f:ajax execute="@form" render="SUM DIFFERENCE" />
</h:commandButton>

我的问题:如何从托管bean(.java)文件中读取两个"ID"或"WIdgetVars"以将它们相加并在以后减去,然后将值存储回"SUM"和"DIFFERENCE"中?

My question: How does one read the two 'IDs' or 'WIdgetVars' from the managedbean (.java) file to add them and subtract them later and store the values back into 'SUM' and 'DIFFERENCE'?

提前谢谢!

-V

推荐答案

您可以将Calendar绑定到标识为timePickingBean的Backing Bean.

You could bind your Calendar to the Backing Bean identified as timePickingBean.

类似这样的东西:

<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>

在您的支持bean中:

And in your backing bean:

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. 但这仅在您仅在备用bean中而不在其他位置需要这些日历的情况下有用.

Don't forget to control the Nullvalue and the getter and setter. But that is only useful if you need these calendars only in your backing bean and not somewhere else.

另一种方法是使用PrimeFaces中的datechange事件触发计算.在这种情况下,更改日历2的值时会调用支持bean(并且您可以在日历1中包括它):

A different approach is to trigger the calculation with the datechange event from PrimeFaces. In this case the backing bean is called when changing the value of calendar 2 (and you could include the same for calendar 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>

这篇关于如何从ManagedBean访问小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆