Ok, the way you're using Struts makes me remember my first project using it too :p
In fact, there's a simple way of handling form initialization in Struts (surely not the only one, but that's the one that I find the best for now);
- You must have a Struts action that initialiaze the view (like a ViewMyForm.do)
- You must have a Struts action that handle the submit (like a DoMyForm.do or SubmitMyForm.do)
- On that submit action, you must have a Struts ActionForm (like MyForm) linked (which will contains all the parameters of the form)
The goal is to generate the actionForm in the view action so that it will contains all the "to be selected" values in the form to be displayed and eventually all the collections that are used in the forms (in tags like html:select).
When you've that, here's how you can do it;
* in MyForm;
private String userType;
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public void reset(... /* forgot the parameters required here :p */) {
userType = null;
}
* in ViewMyForm's execute method;
String userType = ...;
MyForm myForm= new MyForm();
myForm.setUserType(userTyp
request.setAttribute("MyFo
// Eventually, let's also generate the collection here; it'll look better than in the JSP page;
Collection options = new ArrayList();
for (int i = 0;i < Constants.USER_TYPE.length
String value = Constants.USER_TYPE[i];
/* A label value bean is the Struts key-value object, which is used to represent, by example,
the value and the label of a option in a select */
LabelValueBean option = new LabelValueBean(value, value);
options.add(option);
}
request.setAttribute("list
* In the JSP page;
<html:select property="userType">
<html:optionsCollection name="listOfValuesUserType
</html:select>
* In the SubmitMyForm's execute;
MyForm myForm = (MyForm) form;
String userType = myForm.getUserType();
So the advantages here are multiple;
- You've a clean separation between form/page initialization & display
- You've a nice JSP code without any Java code
- You can use the Struts framework to make most of the work through actions, forms and tags.
I know the example may not be very cool, but it's a simple description on how you could do it (and also, I did not really test it)...
If you have any question about it, just ask.
Main Topics
Browse All Topics





by: LeanMoreTryMorePosted on 2007-04-29 at 16:49:47ID: 18998799
I tried the below but it still doesn't work
;j++) {
<select name="userType">
<% for (int j = 0;j < Constants.USER_TYPE.length
String value = Constants.USER_TYPE[j];
%>
<option value="<%=value%>" <%= value.equals(<html:write property="userType" />) ?
"selected=\"true\"" : "" %> ><%=value%></option>
<% } %>
</select>