• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

html:radio..

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!
i have a simple question..is there any way to have one value pre-selected in html:radio buttons?something equivalent to checked="checked" in html?
thanks in advance!
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Natasa p,

We're glad you dropped by the Java Ranch. There aren't many rules here, but we do ask that you let us know who you are by giving us your actual first and last names (Not just initials). Please make sure the "publicly displayed name" in your profile meets the JavaRanch naming policy.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Natasa,

Thanks for taking the time to comply with the naming policy.

Regarding your question:

Since the <html:radio> tag is tied to the property of the ActionForm bean you specify in the "property" attribute, all you have to do is set the bean property sometime before the tag is displayed. The recommended place to do it is in the Action class that forwards to the JSP. Here's an example:

in your action class:

myActionForm.setColor("red");

in your JSP:

select color:
<html:radio property="color" value="blue" /> blue
<html:radio property="color" value="red" /> red
<html:radio property="color" value="green" /> green

In this case, the "red" button will be selected.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this a year late response, I am trying to use Merrill's suggestion, but the form is null before you render the jsp page right?

So how can you pre-populate the form then?

Thanks!
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jazzy Sanchez:
the form is null before you render the jsp page right?


No, not necessariy. If you want to pre-populate an ActionForm property, create an Action class that uses that ActionForm and then have it forward to your JSP. When Struts calls the execute method in your Action it will instantiate the ActionForm for you and put it in the specified scope (either request or session). You are then free to set one or more of its properties. When the JSP is rendered, the ActionForm will have already been instantiated, and the property will have been set to the value you set in your Action class.

In this case, rather than using the JSP directly in a URL, you'll want to use the preparatory Action. For example: myAction.do instead of myPage.jsp.
 
Jazzy Sanchez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Please bear with me, this is what I did.

This is my action class that will be called prior to rendering the JSP page.



I tried this but didn't work. Thanks in advance!
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change

to

You must use the ActionForm object passed as a parameter to the execute method. This is the one that will be used by the JSP.

Incidentally, the statement:

is completely useless. Just remove it.
[ July 17, 2007: Message edited by: Merrill Higginson ]
 
Jazzy Sanchez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merrill. I actually did just that before i even had my other code from previous post and it is throwing null pointer exception.

Since "form" is null, "settingsForm" will also be null even after casting and it will throw an exception as I set value to its property.

Thanks!
 
Jazzy Sanchez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SOLVED!

You're right. I just missed to define a form to use in my struts-config.

Thanks!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merrill
I have similar problem.By default one of my radio button is selected(Male and Female radio button)i.e. Male.Both radio buttons are html:radio and is a part of c:forEach loop.So there will be multiple Gender fields when JSP page is displayed.But when i try to select Female radio button,its not getting selected.I can't toggled between the radio buttons.
I really appreciate for any solution?

<html:radio styleId='genderMale${counter.count}' indexed="true" name="guests" property="gender" value="<%=Gender.MALE.toString()%>" styleClass="text ${genderErrorStyleClass}"/> <fmt:message key="gender.male"/>

<html:radio styleId='genderFemale${counter.count}' indexed="true" name="guests" property="gender" value="<%=Gender.FEMALE.toString()%>" styleClass="text ${genderErrorStyleClass}"/> <fmt:message key="gender.female"/>

Thanks
Sachin
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't given us enough information to help you with this. We would need to see your ActionForm class as well as the class you're using to populate the "guests" collection. Also, which scope are you using for this action.. request or session?
 
Sachin Chaudhari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have sent you UserForm.java as well as GuestInfoForm.java by an email.
Here is the struts configuration for above action class.Its a DynaActionForm.

<form-bean name="guestsForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="guests" type="com.ncl.web.ui.form.GuestInfoForm[]"/>
</form-bean>

</form-beans>

Action class we are using is GuestAction class which is in session scope.

<action path="/secure/cbooking/setupGuests"
type="com.ncl.web.ui.action.booking.GuestAction"
name="guestsForm" scope="session"
parameter="setupGuests">
<forward name="guestsPage" path=".view.guests.information"/>
</action>

Thanks
Sachin
 
Sachin Chaudhari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sachin Chaudhari:
I have sent you UserForm.java as well as GuestInfoForm.java by an email.
Here is the struts configuration for above action class.Its a DynaActionForm.

<form-bean name="guestsForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="guests" type="com.ncl.web.ui.form.GuestInfoForm[]"/>
</form-bean>

</form-beans>

Action class we are using is GuestAction class which is in session scope.

<action path="/secure/cbooking/setupGuests"
type="com.ncl.web.ui.action.booking.GuestAction"
name="guestsForm" scope="session"
parameter="setupGuests">
<forward name="guestsPage" path=".view.guests.information"/>
</action>

Thanks
Sachin

 
Sachin Chaudhari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Meriil
Any light on this issue? This one is bothering us a lot.Your help is really appreciated.
Thanks
Sachin
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the <logic:iterate> tags surrounding the fragment of the JSP you've shown us.
 
You didn't tell me he was so big. Unlike this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic