Tuesday, October 29, 2013

Pass additional values to Listener using SetPropertyListener

Scenario: User enters bonus amount. If Salary+Bonus is greater than 1,50,000 show alert message.

Step1:Create VO with Salary and Bonus attributes. Create a table in JSPX page.

Step2: Set Bonus input text field properties.. add validate method to ValueChangeListener and set AutoSubmit to true.

Step3: Add setPropertyListener on Bonus inputtext field.
    From: Source value(Value to be saved in scope for later use) Ex: #{row.bindings.Salary.inputValue}
    To:  Variable Name(Scope and Variable to hold the source value) Ex; #{pageFlowScope.CurrRowSalaryVar}
    Type: Listener Type Ex: valueChange

Step4: Add code in bean.
    public void validateTotalSalary(ValueChangeEvent ve){
        BigDecimal bonus = (BigDecimal)ve.getNewValue();
        BigDecimal salary = (BigDecimal)AdfFacesContext.getCurrentInstance().getPageFlowScope().get("CurrRowSalaryVar");
        int ibonus = bonus.intValue();
        int isalary= salary.intValue();
        
        if(bonus!=null && salary!=null && (ibonus+isalary)>150000){
            FacesMessage msg = new FacesMessage("Total Salary exceeded 1,50,000.");
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            FacesContext fc = FacesContext.getCurrentInstance();
            fc.addMessage(null, msg);
        }
    }

Step5: Ouput


Related Posts Plugin for WordPress, Blogger...