Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Hiding integer form value with Struts html:hidden (http://www.velocityreviews.com/forums/t128731-hiding-integer-form-value-with-struts-html-hidden.html)

msa 11-16-2003 05:11 AM

Hiding integer form value with Struts html:hidden
 
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:options collection="userList" property="id"
labelProperty="name" />
</html:select>

</logic:greaterThan>


<form-beans>
<form-bean name="userSelectForm"
type="org.apache.struts.validator.DynaValidatorFor m">
<form-property name="userId" type="java.lang.Integer"/>
</form-bean>
</form-beans>

news.verizon.net 11-17-2003 01:15 AM

Re: Hiding integer form value with Struts html:hidden
 
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

"msa" <marniesandrews@yahoo.com> wrote in message
news:ad8e6692.0311152111.576d4183@posting.google.c om...
> 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:options collection="userList" property="id"
> labelProperty="name" />
> </html:select>
>
> </logic:greaterThan>
>
>
> <form-beans>
> <form-bean name="userSelectForm"
> type="org.apache.struts.validator.DynaValidatorFor m">
> <form-property name="userId" type="java.lang.Integer"/>
> </form-bean>
> </form-beans>




msa 11-18-2003 05:00 PM

Re: Hiding integer form value with Struts html:hidden
 
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:options, 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:options collection="userList" property="id"
labelProperty="name" />
</html:select>

</logic:greaterThan>

<form-beans>
<form-bean name="userSelectForm"
type="org.apache.struts.validator.DynaValidatorFor m">
<form-property name="userId" type="java.lang.String"/>
</form-bean>
</form-beans>

-Marnie

"news.verizon.net" <vparekh@verizon.net> wrote in message news:<IiVtb.64055$p9.5641@nwrddc02.gnilink.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
>
> "msa" <marniesandrews@yahoo.com> wrote in message
> news:ad8e6692.0311152111.576d4183@posting.google.c om...
> > 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:options collection="userList" property="id"
> > labelProperty="name" />
> > </html:select>
> >
> > </logic:greaterThan>
> >
> >
> > <form-beans>
> > <form-bean name="userSelectForm"
> > type="org.apache.struts.validator.DynaValidatorFor m">
> > <form-property name="userId" type="java.lang.Integer"/>
> > </form-bean>
> > </form-beans>



All times are GMT. The time now is 05:51 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.