如何获取动态日历值输入

时间:2013-05-23 13:27:11

标签: jsf-2

我想编写一个代码来获取主面日历输入值的输入值。

<div style="padding-left: 300px;">
    <p:calendar value="#{calendarBean.fromDate}" id="fromButton" showOn="button" pattern="dd-MMMM-yyyy" 
                                    required="true" requiredMessage="Insert From Date!">
    </p:calendar>
    <p:calendar value="#{calendarBean.toDate}" id="toButton" showOn="button" pattern="dd-MMMM-yyyy"
                                    style="padding-left: 100px;padding-right: 30px" required="true" requiredMessage="Insert To Date!">
    </p:calendar>
    <p:commandButton actionListener="#{userLeaveBean.addAppliedLeave}" styleClass="apply_button" value="Create Leave">
    <f:attribute name="userId" value="#{employee.name}"/>
    </p:commandButton>
</div>

我想通过方法调用传递三个参数值(userId,fromDate,toDate)

<p:commandButton actionListener="#{userLeaveBean.addAppliedLeave}" styleClass="apply_button" value="Create Leave"> 

使用方法addAppliedLeave()。 我可以使用:

发送userId
<f:attribute name="userId" value="#{employee.name}"/> but how can I pass the values for calender input at run time?

我也可以在方法addAppliedLeave()

中获取userId的值
public void addAppliedLeave(ActionEvent event){

    String userId = (String)event.getComponent().getAttributes().get("userId");
    System.out.println(userId);
    String fromDate = (String)event.getComponent().getAttributes().get("fromDate");
    System.out.println(fromDate);

    }

但我对<p:calendar>值的动态输入完全无能为力。 请帮忙。

1 个答案:

答案 0 :(得分:0)

您可以使用p:calender在运行时轻松获取p:ajax值,如此处所示

/* view.xhtml  */

<p:calendar value="#{yourBackingBean.user.fromDate}" mode="inline" id="inlineCal">  
        <p:ajax event="dateSelect" listener="#{yourBackingBean.handleDateSelect}" update="growl" />  
    </p:calendar>

/ * Backing bean * /

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class YourBackingBean implements Serializable {

    private static final long serialVersionUID = 1L;

     private User user;



    public void handleDateSelect(DateSelectEvent event) {

    System.out.println("dynamic date selected is "+user.getFromDate());

    // so here you will get everytime your changed date.
    //As this method will get called everytime whenever you will change your date from p:calender component.



    }


      public User getUser() {

     if(user==null){

     user=new User();

     }

     return user;
    }


}

/ * javaBean * /

public class User implements Serializable {

    private static final long serialVersionUID = 1L;


     private Date fromDate;
     private Date toDate;

    public Date getFromDate() {
        return fromDate;
    }

    public void setFromDate(Date fromDate) {
        this.fromDate = fromDate;
    }

    public Date getToDate() {
        return toDate;
    }

    public void setToDate(Date toDate) {
        this.toDate = toDate;
    }


}