ORGL23
ORGL23

Reputation: 454

How can I use multiple value in Input text field JSF

In my xhtml there are 3 input field which calculate remaining day of two <p:calendar> dates. In next step I want to store calculated remaining day to 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}"/>

I used value="#{dayDiffBean.diff} to get remaining day and now I also want to use my vacationbean to store remaingday to my db using like this : value="#{vacationBean.vacation.balanceDay}"

But I cant use 2 value in inputtext field like this:

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

How can i solve this problem?

This is my vacation bean code:

@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;
    }

}

And daydiffbean code :

      @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;
}



}

Upvotes: 1

Views: 4748

Answers (1)

b0nyb0y
b0nyb0y

Reputation: 1446

From the code, one way to add balanceDay to your vacationBean is by passing the diff string as a parameter to addVac() method (notice action in the second line):

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

Then, for your 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);
}

Upvotes: 1

Related Questions