Meaningless Drivel is fun!*
The moose likes Struts and the fly likes Creating Radio Buttons using Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Creating Radio Buttons using Struts" Watch "Creating Radio Buttons using Struts" New topic
Author

Creating Radio Buttons using Struts

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I'm remarkably bad at creating HTML components using the various struts tag libraries. I understand the rest of Struts well but, when it comes to writing my JSP pages, I'm much better at using raw HTML than Struts tags. As I'm currently working on my first Struts app, I'm learning as I go along.

Anyway, a co-worker of mine came to me with a problem she's having (we're both new to Struts and she's quite new to Java, in general). Let me try to explain the situation.

In the form bean, she has a member variable which is, in itself, a collection of beans. So, she has a form bean which contains a member called status, which is a collection of beans that contain a label and a value - essentially it's a hash (she probably isn't yet comfortable enough with Java to use a hash but we pick our battles and I'll leave this one go for now). She wanted to print all of the values to her JSP so we got some code together that looks like this:



That little snippet gets us all of the status labels printed out in a nice row. That's fine, but it's the next part that gets me. We want to print out each of those labels as a radio button so that the user can select a new status. How do I do that? I've looked at the <html:radio> tag but I honestly can't make much sense of it. My goal would be to have HTML that resembles this:



Any ideas how I might accomplish that? Like I said, I'm pretty new to this whole Struts tag thing and, while I've been working on some back end stuff, my co-workers have been getting into this stuff. Obviously, I haven't had time to play with it myself so I'll simply concede to asking for help for the time being.

Thanks,
Corey


SCJP Tipline, etc.
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Each radio button element requires an assigned value that distinguishes it from the other radio buttons. When creating a static array of radio buttons, you need to indicate which one of these, if any, is checked. This does not need to be done when the radio buttons are being populated from dynamic data. The control can compare itself to the form bean's property and then check itself when appropriate.

Given a set of <html:radio> controls like this:

And that the expectedVia property on the form bean was already set to "UPS", then the HTML radio elements would be rendered like this:

If you need to create a dynamic set of radio buttons, or want to localize the values, you can create in an Action a collection of LabelValueBeans with the appropriate labels and values for each button. Here's an example:

Then, on the page, you can iterate through the collection

So long as one of the values matches the "expectedVia" property on our ActionForm, the radio tag will still automatically select the appropriate button.

In Struts 1.1, you can use the built-in LabelValue class instead [org.apache.struts.util.LabelValueBean].

Struts 1.1 (beta 2 or later) also provides an additional property to the RadioButton tag that eliminates the need to use a scriplet. With 1.1, we can interate through the same collection this way:

This tells the tag to look for a bean named row, and call its getValue method, which is what the scriplet did.
[ November 11, 2004: Message edited by: somkiat puisungnoen ]

SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Creating Radio Buttons using Struts
 
Similar Threads
checked radio button from an Enum value
Anchor tag calling Javascript function
h:selectOneRadio automatically creates Table
Changed values aren't set on the backing object.
IE doesn't return value of submit button?