Working with Multiple JSF Input Fields

By Jim White (Director of Training and Java Instructor) ? jwhite@intertech.com

Just before the holiday break, a former student of mine (thanks Don) sent me an email asking if an array of Strings property on a managed bean can be bound to a group of input fields in a JSF form? Can the String array be used to both populate and collect data from the input fields?

Binding Input Fields to Properties

If you have started to explore JSF, you are aware that a managed bean?s property can be bound to an input field in a JSP. For example, a VoteBean with a name property (and associated getter and setter methods) as shown can be bound to a JSF form?s input field as shown in the JSP also shown here.

Managed bean code

public class VoteBean {
    private String name = "Enter your name";

    public String getName() { 
        return name;
    }

    public void setName(String name) { 
        this.name = name;
    }

   ?

}

JSP code

<f:view>
    <h:form>
Your name:
        <h:inputText value="#{vote.name }" />
        <br />
        <h:commandButton value="Submit" action="#{vote.collectFavorites}" />
    </h:form>
</f:view>

Faces config

<managed-bean>
    <managed-bean-name>vote</managed-bean-name>
    <managed-bean-class>com.intertech.beans.VoteBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

As the JSP is initially displayed, the getter method on the name field is called and the data from the property (initially ?Enter your name?) is used to populate the associated input field in the form. Conversely, when the form is submitted, the data from the form input field is used to call the setter method to update the data back into the managed bean?s name property.

image

Binding Input Fields to String Arrays/Collections

So what if a JSP needs to collect several strings in a group of input fields? How can the data in the group of input fields be captured? Indeed, you can bind a String array or java.util collection property to the JSF form?s input fields.

It?s the New Year and I was passed this year?s list of ?national days? by a friend.  January 23rd is National Pie Day.  No ? I?m not joking (check it out at American Pie Council).  So what if you wanted to build a Web application (JSF driven of course) to collect people?s choice for favorite pies in honor of National Pie Day?  In the managed bean, define a property called favorites that is either a String array or collection (like a List<String>). Create getters and setters for the property. Importantly, make sure the array or collection object is initialized and populated (when necessary) before the form that uses the data is displayed (the static initialization block does that work here).

Managed Bean Code Using an Array

    private String[] favorites;

    {
        favorites = new String[5];
    }
    public String[] getFavorites() {
        return favorites;
    }

    public void setFavorites(String[] favorites) {
        this.favorites = favorites;
    }

Managed Bean Code Using an ArrayList

    private List<String> favorites;

    {
         favorites = new ArrayList<String>();
         for(int i = 1; i<=5; i++)
             favorites.add("");
    }

    public List<String> getFavorites() {
        return favorites;
    }

    public void setFavorites(List<String> favorites) {
        this.favorites = favorites;
    }

Now, using JSF?s tag library, define input fields in the JSP to collect the favorite pie votes. The input fields value attribute should use Unified Expression Language to reference each of the array or collection elements using square bracket [ ] notation.

JSP Code

<f:view>
    <h:form>
Your name:
        <h:inputText value="#{vote.name }" />
        <br />
Your age:
        <h:inputText value="#{vote.age }" />
        <br />
Your favorite types of pie:<br />
        <h:inputText value="#{vote.favorites[0] }" />
        <br />
        <h:inputText value="#{vote.favorites[1] }" />
        <br />
        <h:inputText value="#{vote.favorites[2] }" />
        <br />
        <h:inputText value="#{vote.favorites[3] }" />
        <br />
        <h:inputText value="#{vote.favorites[4] }" />
        <br />
        <h:commandButton value="Submit" action="#{vote.collectFavorites}" />
    </h:form>
</f:view>

  image image

image Click here for a complete (Eclipse 3/Tomcat 6) working example of the code shown in this blog entry.

Have a wonderful year, and if you would like to join me to learn more about JSF in the New Year, you can sign up and learn more about Intertech?s Complete JavaServer Faces class here.


Posted by: Jim White
Posted on: 1/13/2010 at 10:03 AM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Subscribe to this BlogRSS comment feed

Add comment




biuquote
  • Comment
  • Preview
Loading


Contact Us 651-994-8558 1-800-866-9884
Home | Training | Curriculum | Course Finder | Schedule | Enroll | Twin Cities Java User Group | Consulting | Foundation | Jobs | About Us | Our Story | Press Room | Instructors | President | Map & Directions | Sitemap

Java Training | JSF / Struts / Spring / Hibernate Training | Java Power Tools Training | .NET 4.0 & Visual Studio 2010 Training | .NET 3.5 and Visual Studio 2008 Training | .NET 2.0 and Visual Studio 2003 Training | Prism / MVVM / MEF Training | Microsoft Web Development Training | Cloud Computing Training | Ajax / Web Services / XML Training | Groovy and Grails Training | SQL Server 2008 Training | SQL Server 2005 Training | Mobile Development Training | SharePoint 2010 Training | SharePoint 2007 Training | Agile, Process, Analysis & Design Training | Arch/Design Patterns Training | Microsoft Official Curriculum Training | Web Development Training | Ruby Training | Rational Application Developer (RAD) Training | WebSphere Application Server Training | WebSphere Portal Training | WebLogic Training | Boot Camp Training | Project Management Training | C++ Training | Metro / WinRT / Windows 8 Development Training | Retired

Intertech delivers training on-site and virtually serving cities including Phoenix, AZ | San Francisco, CA | Los Angeles, CA | San Diego, CA | San Jose, CA | Washington, DC | Chicago, IL | Orlando, FL | Boston, MA | Duluth, MN | Minneapolis St. Paul, MN | Rochester, MN | Raleigh-Durham, NC | New York, NY | Philadelphia, PA | Austin, TX | Dallas, TX | Houston, TX | Seattle, WA.