如何在输入文本字段JSF中使用多个值

时间:2014-07-02 02:16:18

标签: jsf primefaces

在我的xhtml中有3个输入字段,用于计算两个<p:calendar>日期的剩余日期。在下一步中,我想将计算的剩余日期存储到MY DB。

<p:dataTable styleClass="vtable" editable="true" var="user"
editMode="cell" value="#{userBean.employeeList}">
<p:column styleClass="columntd" headerText="#{text['user.startedDate']}">
    <p:calendar widgetVar="fromCal" value="#{vacationBean.vacation.beginDate}">
 <p:ajax event="dateSelect" listener="#{dayDiffBean.fromSelected}"
        update="diff" />
  </p:calendar>
</p:column>
<p:column styleClass="columntd"
headerText="#{text['user.finishedDate']}">
 <p:calendar widgetVar="toCal" value="#{vacationBean.vacation.endDate}">
    <p:ajax event="dateSelect" listener="#{dayDiffBean.toSelected}"
        update="diff" />
  </p:calendar>
  </p:column>
  <p:column styleClass="columntd"
headerText="#{text['employee.remainingdays']}">

<p:inputText id="diff" styleClass="daysNumber"
    value="#{dayDiffBean.diff}" />
        </p:column>
</p:dataTable>
   <h:commandButton styleClass="sndbutton1"
value="#{text['employee.send']}" action="#{vacationBean.addVac}"/>

我使用value="#{dayDiffBean.diff}来获取剩余的一天,现在我还想使用我的vacationbean将remaingday存储到我的数据库中,使用如下:value="#{vacationBean.vacation.balanceDay}"

但我不能在inputtext字段中使用2值,如下所示:

<p:inputText value="dayDiffBean.diff" value1="vacationBean.vacation.balanceDay">

我该如何解决这个问题?

这是我的假期bean代码:

@ManagedBean(name="vacationBean")
@ViewScoped
public class VacationBean implements Serializable{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private Date vEndDate;
    private boolean selected;
    private Date vStartDate;
    private Date createdDate;
    private String isNobody;
    Requestrelax vacation;
    Employee e;
    Calendar javaCalendar = null;
    private short balanceDay;
    @EJB
    VacationLocal vacations;
    @ManagedProperty(value="#{loginBean.userId}")
    Integer userId;

    @EJB
    EmployeesLocal employees;
    @PostConstruct
    public void init(){
        System.out.println("0");
        //System.out.println("STATrtsg >> . "+ diff.getDiff());
        vacation=new Requestrelax();
        e=employees.getEmployee(userId);
        vacation.setEmployee(e);
        System.out.println("balanday is:"+balanceDay);
    }
    public void addVac(){
        System.out.println("1");
        javaCalendar = Calendar.getInstance();
        Date currenDate=Calendar.getInstance().getTime();
        vacation.setCreatedDate(currenDate);
        vacation.setBalanceDay(balanceDay);
        vacations.addEmployeeVacation(vacation);
    }
    public Integer getUserId() {
        return userId;
    }
    public void setUserId(Integer userId) {
        this.userId = userId;
    }
    public Employee getE() {
        return e;
    }
    public void setE(Employee e) {
        this.e = e;
    }
    public Requestrelax getVacation() {
        return vacation;
    }
    public void setVacation(Requestrelax vacation) {
        this.vacation = vacation;
    }
    public Date getvEndDate() {
        return vEndDate;
    }
    public void setvEndDate(Date vEndDate) {
        this.vEndDate = vEndDate;
    }
    public Date getvStartDate() {
        return vStartDate;
    }
    public void setvStartDate(Date vStartDate) {
        this.vStartDate = vStartDate;
    }
    public short getBalanceDay() {
        return balanceDay;
    }
    public void setBalanceDay(short balanceDay) {
        this.balanceDay = balanceDay;
    }
    public Date getCreatedDate() {
        return createdDate;
    }
    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public String getIsNobody() {
        return isNobody;
    }

    public void setIsNobody(String isNobody) {
        this.isNobody = isNobody;
    }

}

和daydiffbean代码:

      @ManagedBean(name="dayDiffBean")

    @SessionScoped
     public class DayDiffBean implements Serializable {

/**
 * 
 */



private static final long serialVersionUID = 1L;
private Date from;

private Date to;
private String diff="";
private final long oneDay=1000*60*60*24;



public void fromSelected(SelectEvent event){
    from=(Date) event.getObject();
    calDiff();

}

public void toSelected(SelectEvent event){
    to=(Date) event.getObject();
    calDiff();

}


public void calDiff(){
    if(from==null||to==null){
        diff="N/A";
        return;
    }
    diff=(to.getTime()-from.getTime())/oneDay+"";
} 
public String getDiff() {
    return diff;
}
public void setDiff(String diff) {
    this.diff = diff;
}
public void setFrom(Date from) {
    this.from = from;
}
public Date getFrom() {
    return from;
}
public Date getTo() {
    return to;
}
public void setTo(Date to) {
    this.to = to;
}



}

1 个答案:

答案 0 :(得分:1)

从代码中,将balanceDay添加到vacationBean的一种方法是将diff字符串作为参数传递给addVac()方法(在第二行注意action ):

<h:commandButton styleClass="sndbutton1" value="#{text['employee.send']}" 
action="#{vacationBean.addVac(dayDiffBean.diff)}"/>

然后,为您的VacationBean.addVac()

// 'diff' is now being passed in as a parameter
public void addVac(String diff) {
    System.out.println("1");
    javaCalendar = Calendar.getInstance();
    Date currenDate=Calendar.getInstance().getTime();
    vacation.setCreatedDate(currenDate);
    vacation.setBalanceDay(balanceDay);

    // UPDATED
    // so now you can set balanceDay
    setBalanceDay(Short.parseShort(diff));

    vacations.addEmployeeVacation(vacation);
}