aspose file tools*
The moose likes Struts and the fly likes html:radio.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "html:radio.." Watch "html:radio.." New topic
Author

html:radio..

natasa jones
Ranch Hand

Joined: Aug 20, 2006
Posts: 45
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!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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
Consultant, Sima Solutions
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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.
Jazzy Sanchez
Ranch Hand

Joined: Apr 02, 2006
Posts: 35
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

Joined: Feb 15, 2005
Posts: 4864
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

Joined: Apr 02, 2006
Posts: 35
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

Joined: Feb 15, 2005
Posts: 4864
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

Joined: Apr 02, 2006
Posts: 35
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

Joined: Apr 02, 2006
Posts: 35
SOLVED!

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

Thanks!
Sachin Chaudhari
Greenhorn

Joined: Sep 08, 2008
Posts: 4
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

Joined: Feb 15, 2005
Posts: 4864
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

Joined: Sep 08, 2008
Posts: 4
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

Joined: Sep 08, 2008
Posts: 4
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

Joined: Sep 08, 2008
Posts: 4
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

Joined: Feb 15, 2005
Posts: 4864
Please post the <logic:iterate> tags surrounding the fragment of the JSP you've shown us.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: html:radio..