1

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

<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>价值的动态输入完全无能为力。请帮忙。

4

1 回答 1

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>

/* 支持 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;
    }


}
于 2013-05-23T14:32:26.620 回答