M
msa
Hello,
I have a collection of objects embedded in page scope named
"userList". Each object has a property called "id" that is of type
Integer and a property called "name" of type String. (Code snippets
below.)
In my JSP, I want to iterate through the collection and build a select
box/drop down box where the value of each option is the id and the
label is the name. The exception is if there is only one item in the
collection, in which case I want to output just the name, not in a
drop down box. However, in this latter case, I still need the id of
this single item in the page somewhere so that when the form is
submitted, I know what the id of the single item is.
I thought I could just use the html:hidden tag, and store the id under
form bean property of the same name as the select property ("userId").
But that didn't work because the html:hidden value attribute will
only accept Strings. Using Integer.parseInt to convert the value to a
string doesn't work either because it doesn't satisfy the form bean
definition, which defines userId as an Integer.
Can anyone give me some advice?
Thanks!
Marnie
(I apologize in advance for any small syntactical errors -- I'm trying
to remember my code from home.)
<%-- if collection size = 1, just output the name --%>
<logic:equals name="userListSize" value="1">
<logic:iterate name="userList">
<bean:define id="userId" name="userList" property="id"
type="java.lang.Integer" />
<%-- the line below doesn't work; it causes exception creating
bean since hidden tag only accepts strings for value attribute; using
Integer.parseInt doesn't satisfy the form bean definition, which
defines userId as an Integer --%>
<html:hidden property="userId" value="<%= userId %>" />
<bean:write name="userList" property="name" />
</logic:iterate>
</logic:equals>
<%-- else, if collection size > 1, create a drop down box --%>
<logic:greaterThan name="userListSize" value="1">
<html:select property="userId">
<htmlptions collection="userList" property="id"
labelProperty="name" />
</html:select>
</logic:greaterThan>
<form-beans>
<form-bean name="userSelectForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="userId" type="java.lang.Integer"/>
</form-bean>
</form-beans>
I have a collection of objects embedded in page scope named
"userList". Each object has a property called "id" that is of type
Integer and a property called "name" of type String. (Code snippets
below.)
In my JSP, I want to iterate through the collection and build a select
box/drop down box where the value of each option is the id and the
label is the name. The exception is if there is only one item in the
collection, in which case I want to output just the name, not in a
drop down box. However, in this latter case, I still need the id of
this single item in the page somewhere so that when the form is
submitted, I know what the id of the single item is.
I thought I could just use the html:hidden tag, and store the id under
form bean property of the same name as the select property ("userId").
But that didn't work because the html:hidden value attribute will
only accept Strings. Using Integer.parseInt to convert the value to a
string doesn't work either because it doesn't satisfy the form bean
definition, which defines userId as an Integer.
Can anyone give me some advice?
Thanks!
Marnie
(I apologize in advance for any small syntactical errors -- I'm trying
to remember my code from home.)
<%-- if collection size = 1, just output the name --%>
<logic:equals name="userListSize" value="1">
<logic:iterate name="userList">
<bean:define id="userId" name="userList" property="id"
type="java.lang.Integer" />
<%-- the line below doesn't work; it causes exception creating
bean since hidden tag only accepts strings for value attribute; using
Integer.parseInt doesn't satisfy the form bean definition, which
defines userId as an Integer --%>
<html:hidden property="userId" value="<%= userId %>" />
<bean:write name="userList" property="name" />
</logic:iterate>
</logic:equals>
<%-- else, if collection size > 1, create a drop down box --%>
<logic:greaterThan name="userListSize" value="1">
<html:select property="userId">
<htmlptions collection="userList" property="id"
labelProperty="name" />
</html:select>
</logic:greaterThan>
<form-beans>
<form-bean name="userSelectForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="userId" type="java.lang.Integer"/>
</form-bean>
</form-beans>