Using data beans to populate values in a universal dialog

Data beans that are defined in the universal dialog definition XML file are instantiated when the universal dialog begins loading. If the data bean is also a WebSphere Commerce smartDataBean, it will also be activated at this time. A universal dialog is actually rendered as an HTML form.

If the user wants to update some properties of an item, then the bean should populate all properties in the universal dialog so the user can see the existing values. If the user wants to create an item, the bean should populate default values as well as selection options for objects such as radio buttons and check boxes.

For most data types, if the bean returns null, an empty string will be used in the final HTML form. For a date item, the current date is used.

Procedure

  • One setter input types

    For the following five element data types, which have only one input value, one simple setter method inside the data bean class is sufficient:

    • hidden
    • label
    • text
    • password
    • textarea
    • checkbox

    These bean properties can be set by using one setter method, for example, setName("my name").

  • Collections

    The following three element data types involves multiple selection options. The bean property should be defined as a subclass of Collection, so that it contains all options:

    • radio
    • checkboxgroup
    • select

    When setting these bean properties, every object inside the Collection should be an instance of the HTMLOption class, for example:

    
    Vector options = new Vector();
    options.add(new HTMLOption("1", "red", false));
    options.add(new HTMLOption("2", "blue", true));
    options.add(new HTMLOption("3", "green", false));
    setFavoriteColor(options);
    

    The HTMLOption class (com.ibm.commerce.tools.common.ui.HTMLOption) is used to represent a selection option. This class has two types of constructors: HTMLOption(String v, String t, Boolean s) and HTMLOption(String v, String t, Boolean s, Boolean key), where v is the value, t is the text, s is whether the option is selected, and key is whether this option text is a resource bundle key or actual text. The default is true). One thing to notice is that for checkboxgroup, since a check box does not have an HTML value, the value property in this case is used to represent the actual check box HTML name, for example:

    
    options = new Vector();
    options.add(new HTMLOption("cert_db2", "db2", true));
    options.add(new HTMLOption("cert_oracle", "oracle", false));
    options.add(new HTMLOption("cert_was", "was", true));
    options.add(new HTMLOption("cert_linux", "linux", false));
    setCertificates(options);
    

    In this example, four check boxes are generated in the output HTML page, and finally the controller command will receive four separate request parameters (ie. cert_db2, cert_oracle, cert_was and cert_linux) each with a "true" or "false" value.

  • Date

    For the date type, the bean property should be defined as a java.sql.Timestamp object type (this is currently the Commerce convention for the date object). A sample setter method is:

    
    setServiceDate(new Timestamp(System.currentTimeMillis())); // set today's date
    

    When the date type element is rendered in the page, three text input fields are generated as separate input of year, month and day. These fields are named as <elementName>_year, <elementName>_month, <elementName>_day. They will be submitted to the server as three request parameters. As well, controller commands should use this convention when parsing request properties.