Hiding integer form value with Struts html:hidden

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">
<html:eek:ptions 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>
 
N

news.verizon.net

I didn't quite understand what you are trying to do ....if you can be more
clear i might be able to help.. i have worked
with struts a lot lately

casting string to Integer object can be done easily as.
Integer.valueOf("string') ..........

Vishal
 
M

msa

Thanks for responding, Vishal. Sorry if I wasn't very clear.

The problem ended up being that I had my <form-property> definition
for the userId property set to java.lang.Integer. It turns out that
although the property of the collection I was iterating through to
output the select box was type Integer, when you use html:eek:ptions, the
values are converted to Strings automatically.

So, to make things work, I just had to change the <form-property>
value to java.lang.String. I guess I'll use Integer.parseInt() on the
value once I get into my Action class, which is fine.

Here is the correct code:

<%-- 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.String" />

<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">
<html:eek:ptions 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.String"/>
</form-bean>
</form-beans>

-Marnie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top