Struts

Name:
Location: Bangalore, India

Monday, November 26, 2007

Parameter Checking in JSP Page using struts tags

In 2 of my screens I am invoking a pop-up window and the same pop-up window in a 3rd screen

-------------------------------------------------------------------------------------

I am passing the request parameter here

<a href="#" onClick="window.open('searchContactName.do?param=contact','searchContact','resizable=1,width=710,
height=625,status=1,scrollbars=1');" >?</a>

-------------------------------------------------------------------------------------

I am NOT passing the request parameter here

<input type="button" value="LookUp" onClick="window.open('searchContactName.do',
'searchContact','resizable=1,width=710,height=625,
status=1,scrollbars=1');" />

------------------------------------------------------------------------------------

Here is the pop-up window JSP code

<input type="button" onClick="window.close()" value="cancel" />
<logic:present parameter="param" >
<logic:equal parameter="param" value="contact">
<input type="button" onClick="setContactInformation('abc','contactTable');" value="ok"/>
</logic:equal>
</logic:present>
<logic:notPresent parameter="param" >
<input type="button" onClick="setContactName('abc');" value="ok"/>
</logic:notPresent>

Saturday, November 17, 2007

When to use logic:iterate and when to use html:options.

When we want to iterate a list of bean objects which inturn contains a bunch of attributes (or) properties that needs to be displayed to the user in a table we will go for a logic:iterate.

When we need to iterate a bunch of bean objects which inturn has 2 properties. 1 holds the key and the other holds the corresponding display name. When user selects a name from the drop-down the corresponding key should be stored in the html:select tags property value we will go for a html:options.

For both the cases we can find code samples in this blog.

Thursday, November 15, 2007

How to set a value into a bean:write in the parent window from a pop-up window Javascript function.

Parent JSP Page:

<td class="rmt-readOnlyData" valign="top">
<bean:message key="request.label.countryOfRisk" bundle="abc">
</td>
<td align="left" valign="top" id="countryRisk">
<bean:write name="RequestForm" property="countryOfRisk">
</td>

Pop-up window Java script:

function setParentWindowProperty() {
var countryOfRisk = window.opener.document.getElementById("countryRisk");
var countryOfRiskName = window.opener.document.countryOfRisk;
countryOfRiskName.value = cel2.childNodes[0].data; // ==> Here is where we set the value to the property countryOfRisk.

// This step is not needed if the property is a text box.
countryOfRisk.innerText = countryOfRiskName.value; // ==> Here is where we set the value to the UI screen.

}

Tuesday, November 06, 2007

Populating a drop-down box

In our example we are iterating listRequestTypes which is an arrayList holding objects of type RequestTxnTypesBean. It is a bean class which has (1) requestTxnTypeKey and (2) display property. The display property contains the names that will come in the drop down and requestTxnTypeKey holds the corresponding key.

If the user selects one of the values from the drop-down the selected value’s key from requestTxnTypeKey property gets stored in requestTransactionTypeKey.

<html:select size="1" property="requestTransactionTypeKey" name="RequestForm">
<html:option value="">Select Request Type: </html:option>
<logic:present name="RequestForm" property="listRequestTypes">
<bean:define id="row" name="RequestForm" property="listRequestTypes" />
<html:options collection="row" property="requestTxnTypeKey" labelProperty="display" />
</logic:present>
</html:select>

Sunday, November 04, 2007

Displaying Yes/No in place of Y/N

To display the "Yes" and "No" values on screen. Helps to convert "Y" to "Yes" and "N" to "No". We have a drop down with
1. Select a value
2. Y
3. N

logic:present helps us to find if the property is not a null value in the form.
logic:notEqual helps us to check whether the property is not equal to the value given in the value attribute of this tag.
An example for this would be a drop-down where we have (1) select a value (2) Yes (3) No.
Here option 1 is the default value and if user selects this value then the property (yesnovalue) would be set to nothing which is obviously a empty string. To check for this condition we use
logic:notEqual

<logic:present name="abcForm" property="yesnovalue">
<logic:notEqual name="abcForm" property="yesnovalue" value="">
<bean:message key='common.label.${abcForm.yesnovalue}' bundle="abc"/>
</logic:notEqual>

${abcForm.yesnovalue} ==> This is nothing but the Expression Language (EL) to send the value to the (Yes/No) dynamically to the ApplicationResources.properties file.

Define these values in application resources bundle

common.label.Y=Yes
common.label.N=No

If Expression Language (EL) is not allowed we can directly use struts tags

<logic:present name="RequestForm" property="priority">
<logic:notEmpty name="RequestForm" property="priority">
<logic:equal name="RequestForm" property="priority" value="N">
<bean:message key="request.label.priorityNormal" bundle="gtem" />
</logic:equal>
<logic:equal name="RequestForm" property="priority" value="Y">
<bean:message key="request.label.priorityUrgent" bundle="gtem" />
</logic:equal>
</logic:notEmpty>
</logic:present>

Formatting Date

This is how we set date in US standard format.

<bean:write name="row" bundle="abc" formatKey="common.format.date" property='myDate' />

we specify this value in application resource bundle as
common.format.date=MM/dd/yyyy

OUTPUT: 10/26/2050


Tutorial Link: For Internationalization - I18N functionality

Formatting Numbers

This is how we set numbers in US currency format.

<bean:write name="row" property='automaticLimitAmount' bundle="abc" formatKey="common.currency.format"/>

we specify this value in application resource bundle
common.currency.format=###,###,###,###.##

The dot 00 in output (the decimal values) is restricted by those 2 hash symbols after the dot in the formatkey that we had specified above.

OUTPUT: 1,000,000.00

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.