ActionForm – Reset multiple list <html:select multiple=”true”>
Struts uses ActionForm to both populate a HTML form, and then to capture the parameters when a form is submitted.
When a request is submitted, the ActionServlet matches the URI for the request against the path of an action-mapping. If there is a form-bean for that action-mapping, it looks for an ActionForm under the form-bean name. If it doesn’t find one, a new one is created. The ActionServlet then calls reset() on the new or pre-existing ActionForm and populates it from the request. If the action-mapping has validate set to true, the ActionServlet calls the validate method. If this returns any error messages, the ActionServlet forwards to the URI indicated by the action-mappings error property. This is often a JSP, but it can also be another action-mapping. The latter being needed when there are drop-down controls to populate and so forth. If there are no errors, the ActionServlet passes the (populated and validated) ActionForm to the Action indicated by the action-mapping element.
If you have a form with say a multiple select list, when you unselect items from the list and submit, the ActionForm class would not empty the collection. The easy way to fix it is by writing your own reset() method in which you simply initialize or empty the collection.
Displaying a wait page for a struts action
I was searching google to find a way to show a wait page when a struts form is submitted and the response is loaded. Sometimes it is required when the page has to process a lot of information or get huge amount of data from the database.
I found a really clean way to do so at http://wiki.apache.org/struts/StrutsPleaseWait .
Using The Struts Multibox Tag With LabelValueBeans
The Jakarta Struts <html:multibox> tag can be used to display a group of checkboxes on an HTML form. The tag sets the state of the checkboxes according to the contents of an associated array. The LabelValueBean is a simple class that associates a label with a value. This can be used in combination with the <html:multibox> tag to create checkboxes that return different strings to the ones displayed in the user interface.
The code given below displays four checkboxes with the labels “Alpha”, “Beta”, “Charlie” and “Delta” and the checkboxes return the values “A”, “B”, “C” and “D” respectively.
The JSP:
<logic:iterate name=”myActionForm” id=”item” property=”possibleOptions”>
<html:multibox property=”selectedOptions”>
<bean:write name=”item” property=”value” />
</html:multibox>
<bean:write name=”item” property=”label” /><br />
</logic:iterate>
The Struts Form class:
import org.apache.struts.util.LabelValueBean;
public class MyActionForm extends ActionForm {
private LabelValueBean[] possibleOptions;
private String[] selectedOptions;
public MyActionForm() {
// Initialise the LabelValueBeans in the possibleOptions array.
LabelValueBean[] lvBeans = new LabelValueBean[4];
lvBeans[0] = new LabelValueBean(“Alpha”, “A”);
lvBeans[1] = new LabelValueBean(“Beta”, “B”);
lvBeans[2] = new LabelValueBean(“Charlie”, “C”);
lvBeans[3] = new LabelValueBean(“Delta”, “D”);
this.possibleOptions = lvBeans;
}
public LabelValueBean[] getPossibleOptions() {
return possibleOptions;
}
public String[] getSelectedOptions() {
return selectedOptions;
}
public void setSelectedOptions(String[] selectedOptions) {
this.selectedOptions = selectedOptions;
}
}
