Name:
Location: Bangalore, India

Sunday, November 04, 2007

How to fetch a bunch of radio button values from database and present it to the user?

First we need to understand why we have this requirement. Let’s say in a secondary table one particular column’s (statusKey) value is being mapped to the radio button property in UI (JSP Page) and from the same table we are also fetching the corresponding description column (display) related to the statusKey column.

If we don’t fetch the values from database then we need to hardcode the values directly into the jsp page. This way of hard-coding the value is NOT a standard way of developing web based j2ee applications.

Implementation Description:
First we query the database table and get the values – the radio button property and the label property. There could be 10 values which are being retrieved. That means there will be 10 radio buttons with its corresponding description in UI. We fetch from database, iterate it and store the 10 statusKey/display properties in 10 bean objects and store all those 10 bean objects in an arraylist.

In the JSP page we iterate the list and map all the radio button statusKey fetched from database to the form property and write the display property fetched from database as well.

<logic:present name="ABCForm" property="ABCList"><logic:iterate id="row" name="ABCForm" property="ABCList">
-----------------------------------------------------------------------------
<html:radio name="ABCForm" property="systemStatusKey" value='<%=((com.abc.xyz.projectName.bean.MyBean)row).getStatusKey ().toString() %>' ">
or use Expression Language (EL)
<html:radio name="ABCForm" property="systemStatusKey" value='${row.statusKey}' >
------------------------------------------------------------------------------
<bean:write name="row" property="display"/>
</html:radio></logic:iterate></logic:present>

The above code iterates the list and produces 10 radio buttons along with its description. When the user selects a value from the list of radio buttons then that particular value (statusKey) gets stored in the parent table.

0 Comments:

Post a Comment

<< Home