Skip to main content

Post 1 :JSF: valueChangeListener Attribute

JSF 2.2 valueChangeListener Attribute Example

Usually Components in Web application are dependent on each other. To keep dependent component in sync, it is possible to change the dependent component on change of independent component.
Example: Say we have input form where we have two inputs Select Country select menu and State text box. Now if selected country is say USA, State label is ok but if country is JAPAN we should change label to Prefecture (Prefecture corresponds to State in Japan). So to perform such kind of changes we can use valueChangeListener.
valueChangeListener can be implemented in two ways
  • valueChangeListener attribute (this example explains this way)
  • <f:valueChangeListener> tag
Note! valueChangeListener attribute and <f:valueChangeListener> tag has following subtle differences
  • valueChangeListener attribute can be bound to managed bean method, while <f:valueChangeListener> tag can be bound to class that implements ValueChangeListener interface
  • valueChangeListener attribute allows to attach only single managed bean method to component, while <f:valueChangeListener> tags can be used to attach multiple listeners to component

Code Snippet of valueChangeListener

To implement valueChangeListener, in xhtml file
  • add valueChangeListener=”#{bean.method}” attribute to input HTML element
  • add onchange=”submit()” attribute to input HTML element. This invokes javascript submit function, when selected menu item is changed.
1
2
3
4
5
6
<h:selectOneMenu id="countryList"
         value="#{indexBean.selectedCountry}"
         onchange="submit()"
         valueChangeListener="#{indexBean.onCountrySelect}">
<f:selectItems value="#{indexBean.countries}" />
</h:selectOneMenu>
In managed bean, method bound to valueChangeListener needs to be implements. This method takes ValueChangeEvent as argument.

JSF 2.2 valueChangeListener attribute Example

In this example, form containing Select Country select menu and State text box is displayed. The label for State text box is changed based on Country selected. i.e. State is displayed for USA and Prefecture is displayed for Japan.

JSF 2.2 JAR Dependency

  • javax.faces-2.2.4.jar
  • jsf-api-2.2.4.jar

Development Environment

  • JDK 1.7.0_07
  • Tomcat v7.0
  • JSF 2.2
  • Eclipse 4.2

index.xhtml

Displays input form with Select Country select one menu and State input text box. Based on country selected, state label is changed by use of valueChangeListener

IndexBean.java

Managed Bean class with onCountrySelect() method that is bound to valueChangeListener. onCountrySelect() sets the state label based on selected state.

web.xml

Project Layout valueChangeListener attribute example project layout

Example Output

Input Page input page
As you can in following Output Page, when Japan is selected as country, valueChangeListener method is invoked, which sets State label to Prefecture and hence Prefecture is displayed
Output Page output page
Logoutput is as follows
1
2
3
4
---- initialize ----
----- onCountrySelect method -----
***** oldValue: null
***** newValue: JAPAN


 

Comments

Popular posts from this blog

TIME TABLE: VTU BE/B.Tech June/July 2016 Exam Time table Draft Layout

Semester-wise June-July 2016 Exam time table (Draft Layout) Are You Using updated Kwikstudy App?

Walkins Bangalore: 01st-Oct-2016

1. UTC Aerospace : GET – S&IS-Software Team hiring Electronics & CSE / IT Freshers ! Walkin on 1st OCT | BE/BTECH - 2015 & 2016 both batch can try Selection Process : Written Test Group Discussion Technical & HR Interviews How To Apply : It is a walk-in drive for 2016 batch eligible freshers on 1 Oct 2016 at Bangalore location. Interested and eligible candidates can walk-in to the following venue on 1 Oct 2016 Date of Drive : 01 Oct 2016 Reporting Time : 8:00 AM to 11:30 AM Venue : UTC Aerospace Systems – Site 1, Sy. Nos. 14/1 & 15/1, Maruthi Industrial State, Phase 2, Hoody Village, Whitefield Road, K R Puram Hobli, Bangalore – 560048 ______________________________________ 2. HP : Software Eng/ Software Tester (Call based ,less chance of getting entry , try at your own risk) Event Date : 1-October-2016 Event Time : 8 : 00 am Job Location : Bangalore / Chennai Job Description: Software Engineer /

How to get VTU Provisional Degree Certificate (PDC) ?

Here is the procedure to get the Provisional Degree Certificate from the VTU by passed out candidates. You can get the certificate through post by sending the required documents to VTU. Documents and things required: 1. Provisional Degree Certificate (PDC) application form 2. DD of specified fees amount or print copy of receipt if you paid the fees online 3. A letter to the VTU registrar 4. A4 size envelope Step 1: Fill the PDC application You can download the PDC application form from the VTU website. Click below link for application download. PDC application form download     After downloading the application take a print of it and properly fill the form. Step 2: Make a DD of specified amount as the fees of PDC: Go to the bank and make the DD of prescribed fee amount for the PDC. The fee amount is specified in the above link from where you downloaded the application. Make the DD in favor of " Finance Officer, VTU Belgaum ".   Wr