Hiding integer form value with Struts html:hidden

Discussion in 'Java' started by msa, Nov 16, 2003.

  1. msa

    msa Guest

    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>
     
    msa, Nov 16, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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>
     
    news.verizon.net, Nov 17, 2003
    #2
    1. Advertising

  3. msa

    msa Guest

    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

    "news.verizon.net" <> wrote in message news:<IiVtb.64055$>...
    > 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" <> wrote in message
    > news:...
    > > 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>
     
    msa, Nov 18, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chanchal
    Replies:
    1
    Views:
    1,727
    Manish Pandit
    Oct 9, 2006
  2. Hole
    Replies:
    0
    Views:
    1,077
  3. Rick
    Replies:
    3
    Views:
    8,587
    Alexey Smirnov
    Apr 13, 2010
  4. GavMc
    Replies:
    4
    Views:
    378
    Evertjan.
    Sep 22, 2005
  5. Matt Herson
    Replies:
    8
    Views:
    392
    Matt Herson
    Sep 30, 2003
Loading...

Share This Page