简体   繁体   English

如何从ManagedBean访问小部件

[英]How to access widgets from ManagedBean

Using Primefaces 3.1.1. 使用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">

A managed bean is called on the click of a submit button. 单击提交按钮将调用托管bean。

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

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'? 我的问题:如何从托管bean(.java)文件中读取两个“ ID”或“ WIdgetVars”以将它们相加并在以后减去,然后将值存储回“ SUM”和“ DIFFERENCE”?

Thank you in advance! 先感谢您!

-V -V

You could bind your Calendar to the Backing Bean identified as timePickingBean. 您可以将Calendar绑定到标识为timePickingBean的Backing Bean。

Something like this: 像这样:

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

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

Don't forget to control the Nullvalue and the getter and setter. 不要忘记控制Nullvalue以及getter和setter。 But that is only useful if you need these calendars only in your backing bean and not somewhere else. 但这仅在您仅在备用bean中而不在其他位置需要这些日历的情况下才有用。

A different approach is to trigger the calculation with the datechange event from PrimeFaces. 另一种方法是使用PrimeFaces中的datechange事件触发计算。 In this case the backing bean is called when changing the value of calendar 2 (and you could include the same for calendar 1): 在这种情况下,在更改日历2的值时会调用支持bean(并且您可以在日历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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM