To alert struts error messages in JSP's
<script language="javascript">
var errors = '<html:errors/>';
if(errors != '')
alert(errors);
</script>
Showing posts with label Struts. Show all posts
Showing posts with label Struts. Show all posts
Monday, June 16, 2008
Dynamically display a struts radio button
To dynamically display a radio button with struts iteration, use:
<logic:iterate id="row" name="SHIPPERS">
<html:radio property="expectedVia" value="value" idName="row"/>
<bean:write name="row" property="label"/>
</logic:iterate
<logic:iterate id="row" name="SHIPPERS">
<html:radio property="expectedVia" value="value" idName="row"/>
<bean:write name="row" property="label"/>
</logic:iterate
Preselect struts radio buttons
To preselect a struts radio button"
You can make the radio button checked by declaring the attribute corresponding to radio button as String and providing the default value in FormBean.
For example,
declare and define radio button attribute in FormBean
private String gender = "female";
Now in JSP
<html:radio property="gender" value="female">Female</html:radio>
<html:radio property="gender" value="male">Male</html:radio>
since the value of property gender matches with that of attribute gender in FormBean, the radio button with value female gets checked.
You can make the radio button checked by declaring the attribute corresponding to radio button as String and providing the default value in FormBean.
For example,
declare and define radio button attribute in FormBean
private String gender = "female";
Now in JSP
<html:radio property="gender" value="female">Female</html:radio>
<html:radio property="gender" value="male">Male</html:radio>
since the value of property gender matches with that of attribute gender in FormBean, the radio button with value female gets checked.
Thursday, June 12, 2008
Nested Iterate tag
If you have a Collection in ActionForm and require to access them, we can use nested:iterate
<nested:iterate name="memberCustomerViewForm" property="addressList" >
<div>
<span class="leftBuffer"> </span>
<span class="generalInput">
<nested:text property="addressType" onblur="toUpper(this);" size="11" />
<nested:hidden property="addressId"/>
</span>
</nested:iterate>
Note
- do not use id=current in nested:iterate
- don't access like <nested:text name="current" property="addressType" />
<nested:iterate name="memberCustomerViewForm" property="addressList" >
<div>
<span class="leftBuffer"> </span>
<span class="generalInput">
<nested:text property="addressType" onblur="toUpper(this);" size="11" />
<nested:hidden property="addressId"/>
</span>
</nested:iterate>
Note
- do not use id=current in nested:iterate
- don't access like <nested:text name="current" property="addressType" />
Thursday, May 1, 2008
Working with collections
If we have to iterate a Collection in struts and were required to pass back the object values from the collection as hidden fields to be set in the form, we would get an ArrayIndexOutofBounds exception. This is because we cannot set the collection as is. We need a special handling to add the objects to the collection from the jsp to form like the example given below.
Create a class called CustomList as follows
public class CustomList extends ArrayList {
private Class elementType;
private int maximumGrowSize;
public CustomList(Class elementType, int maximumGrowSize) {
this.elementType = elementType;
this.maximumGrowSize = maximumGrowSize;
}
public Object get(int index) {
while(index >= size()) {
if (index > maximumGrowSize) {
throw new RuntimeException("Cannot allow list to automatically grow beyond " + maximumGrowSize);
}
try {
add(elementType.newInstance());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return super.get(index);
}
}
In struts Action Form, we add
private List records = new CustomList(Person.class, 1000);
where Person.java is our model object which is populated in the collection
Sometimes we may have to display checkboxes next to each record to give the user option to delete selected records.
In such a case we handle the code as below:
<logic:iterate property="records">
<tr class="<%=rowClass%>" style="text-align:center">
<td>
<logic:checkbox property="delete" />
</td>
<td>
<logic:hidden property="personName"/>
<logic:write property="personName"/>
</td>
<td>
<logic:hidden property="personAge"/>
<logic:write property="personAge"/>
</td>
</tr>
</logic:iterate>
In the form, we can add a wrapper class to our Person object as shown below
public class PersonForm extends ActionForm
{
private List records = new CustomList(PersonWrapper.class, 1000);
public List getRecords()
{
return records;
}
public void setRecords(List records)
{
this.records = records;
}
public static class PersonWrapper extends Person
{
private boolean delete;
public boolean isDelete()
{
return delete;
}
public void setDelete(boolean delete)
{
this.delete = delete;
}
}
//....
}
Then in our struts Action class, we can get
public class PersonAction extends Action
{
List myRecords = actionForm.getRecords(); //This would have the the delete checkbox value as well in each row of the collection
for (PersonForm.PersonWrapper record : myRecords)
{
if (record.isDelete())
{
// do delete from database....
}
}
//...
//How to populate this result collection for display is as shown below
List<Person> results = databaseHelper.getAllPersonRecords();
if (results.size() > 0)
{
for (int i = 0, n = results.size(); i < n; i++)
{
PersonForm.PersonWrapper rec = new PersonForm.PersonWrapper();
try
{
PropertyUtils.copyProperties(rec, results.get(i));
}
catch (Exception e)
{
logger.error("Error copying Person beans!", e);
}
results.set(i, rec);
}
}
Create a class called CustomList as follows
public class CustomList extends ArrayList {
private Class elementType;
private int maximumGrowSize;
public CustomList(Class elementType, int maximumGrowSize) {
this.elementType = elementType;
this.maximumGrowSize = maximumGrowSize;
}
public Object get(int index) {
while(index >= size()) {
if (index > maximumGrowSize) {
throw new RuntimeException("Cannot allow list to automatically grow beyond " + maximumGrowSize);
}
try {
add(elementType.newInstance());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return super.get(index);
}
}
In struts Action Form, we add
private List records = new CustomList(Person.class, 1000);
where Person.java is our model object which is populated in the collection
Sometimes we may have to display checkboxes next to each record to give the user option to delete selected records.
In such a case we handle the code as below:
<logic:iterate property="records">
<tr class="<%=rowClass%>" style="text-align:center">
<td>
<logic:checkbox property="delete" />
</td>
<td>
<logic:hidden property="personName"/>
<logic:write property="personName"/>
</td>
<td>
<logic:hidden property="personAge"/>
<logic:write property="personAge"/>
</td>
</tr>
</logic:iterate>
In the form, we can add a wrapper class to our Person object as shown below
public class PersonForm extends ActionForm
{
private List records = new CustomList(PersonWrapper.class, 1000);
public List getRecords()
{
return records;
}
public void setRecords(List records)
{
this.records = records;
}
public static class PersonWrapper extends Person
{
private boolean delete;
public boolean isDelete()
{
return delete;
}
public void setDelete(boolean delete)
{
this.delete = delete;
}
}
//....
}
Then in our struts Action class, we can get
public class PersonAction extends Action
{
List myRecords = actionForm.getRecords(); //This would have the the delete checkbox value as well in each row of the collection
for (PersonForm.PersonWrapper record : myRecords)
{
if (record.isDelete())
{
// do delete from database....
}
}
//...
//How to populate this result collection for display is as shown below
List<Person> results = databaseHelper.getAllPersonRecords();
if (results.size() > 0)
{
for (int i = 0, n = results.size(); i < n; i++)
{
PersonForm.PersonWrapper rec = new PersonForm.PersonWrapper();
try
{
PropertyUtils.copyProperties(rec, results.get(i));
}
catch (Exception e)
{
logger.error("Error copying Person beans!", e);
}
results.set(i, rec);
}
}
Tuesday, December 4, 2007
Struts Redirect does not forward errors
The struts framework stores the Action Errors in the request object and hence when we redirect an action in the struts-config.xml, the errors saved may disappear.
<forward name="close" redirect="true" path="/logoutAction.do" contextrelative="false" >
Solution
Put the errors in session scope as <html:errors/> reads ActionErrors from session context too
Example
private ActionErrors errors = new ActionErrors();
try
{
insertNewEmployee();
}
catch (SQLException e)
{
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("errors.database.unableToInsert"));
}
saveErrors(request.getSession(), errors);
<forward name="close" redirect="true" path="/logoutAction.do" contextrelative="false" >
Solution
Put the errors in session scope as <html:errors/> reads ActionErrors from session context too
Example
private ActionErrors errors = new ActionErrors();
try
{
insertNewEmployee();
}
catch (SQLException e)
{
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("errors.database.unableToInsert"));
}
saveErrors(request.getSession(), errors);
Friday, November 30, 2007
Struts StyleId attribute
In HTML, we can make references to an element by the "id" attribute.
Example: input type="text" name="carrier" id="carrierId"
We can reference this element in javascript as document.getElementById("carrierId")
In Struts, 1.1 we do not have this attribute "id". To make a reference we can instead use the styleId attribute
Example: <html:text property="carrier" styleId="carrierId" />
Example: input type="text" name="carrier" id="carrierId"
We can reference this element in javascript as document.getElementById("carrierId")
In Struts, 1.1 we do not have this attribute "id". To make a reference we can instead use the styleId attribute
Example: <html:text property="carrier" styleId="carrierId" />
Subscribe to:
Posts (Atom)