aspose file tools*
The moose likes Struts and the fly likes Generate Radio Buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Generate Radio Buttons" Watch "Generate Radio Buttons" New topic
Author

Generate Radio Buttons

Amy May
Greenhorn

Joined: Jun 21, 2006
Posts: 5
I've looked and looked online, tried many suggestions I came across on the web, but I just DONT GET it... This is what I am trying to do:

I'm generating rows of data on a JSP page. On each row, there needs to be 3-4 radio buttons (i.e.: new, update, delete -the particular entry). I can't figure out how to do that via struts. What do I put in the form bean?? the jsp Page?? the action class after submitting the form?? Any ideas would be very helpful.

Thanks.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
There are two different things you need to understand thoroughly in order to perform this task:

1- You need to understand how indexed properties work in Struts. This link explains the basics of indexed properties. I'd suggest you create a few test pages trying out some of the techniques offered in this link.

2- You need to understand how radio buttons work. The main thing you need to understand is that when you have more than one radio button with the same property name, they all update the same property with different values depending on which one is checked. So, in your example, you will need three <html:radio> tags all with the same property attribute, but with different value attributes.


Here's a simple example that you can extrapolate to match what you're doing:

ActionForm
public class ItemForm extends ActionForm {
private List items;
public List getItems() { return items; }
public void setItems(List items) {this.items = items; }
public Item getItem(int index) {return (Item)items.get(index);}
public void setItem(int index, Item item) { items.set(index, item);}
}

Item class

public class Item {
private String name;
private String number;
private String color;
// getters and setters
}


JSP
<table>
<logic:iterate id="item" name="itemForm" property="items">
<tr>
<td>Number:<html:text name=item" property="number" indexed="true"/></td>
<td>Name:<html:text name=item" property="name" indexed="true"/></td>
<td>color:<html:radio name=item" property="color" value="red" indexed="true"/>red<html:radio name=item" property="color" value="blue" indexed="true"/>blue<html:radio name=item" property="color" value="green" indexed="true"/>green</td>
</tr>
</logic:iterate>
</table>


In order to make this work, I'd suggest you put your form bean in session scope rather than request scope. It's easier to get it working and see results this way. If you really don't want it to be in session scope, then you will have to change your indexed getters to use a "lazy initialization" technique to make sure the list is populated before trying to get or set one of it's values.


Merrill
Consultant, Sima Solutions
Amy May
Greenhorn

Joined: Jun 21, 2006
Posts: 5
Thanks for that... I'm reading up on how the indexed properties work though the link you send, and I'm trying to make the second option work. However, I keep getting the "No getter method for property items of bean <nameOfMyFormBean>" error... any idea WHY it does that??

I've populated my form with some item objects put the form in the session. When I'm debugging it, I can see the information, however when it gets to the <logic:iterate ...> it gives me the error listed above. I have also check to make sure to check my form for capitalization in the getter/setters and the property in the JSP... Any other suggestion?

Thanks for it!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
This error message would normally indicate that there is no getItems() method in your bean. Are you sure there is such a method, and that it's defined as public? Also, make sure the name attribute of the <logic:iterate> tag references the name of the ActionForm bean you defined in struts-config.xml.

If you've already checked these things and are still getting the error, post your code, and we'll try and help you figure it out.
Amy May
Greenhorn

Joined: Jun 21, 2006
Posts: 5
Alright, I check, and thanks for the help. I guess my computer just needed a reboot. So, now I am able to display the Item Objects in the manner I needed... wonderful. Just another quick question. I'm hitting the submit button to send the information back, but I'm getting a BeanUtils.populate error: (Im guessing the stuff on the screen isn't populating the form when sending the information back? )


Caused by: org.apache.jetspeed.portlet.PortletException: BeanUtils.populate
at java.lang.Throwable.<init>(Throwable.java)
at java.lang.Throwable.<init>(Throwable.java)
at javax.servlet.ServletException.<init>(ServletException.java:132)
at org.apache.jetspeed.portlet.PortletException.<init>(PortletException.java:53)
at com.ibm.wps.portlets.struts.WpsStrutsPortlet.actionPerformed(WpsStrutsPortlet.java:1641)
at com.ibm.wps.pe.pc.legacy.SPIPortletInterceptorImpl.handleEvents(SPIPortletInterceptorImpl.java:341)
at com.ibm.wps.pe.pc.legacy.invoker.impl.PortletDelegateImpl._dispatch(PortletDelegateImpl.java:408)

.
.
.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Did you specify "session" as the scope for your action? If not, that's the problem.
Amy May
Greenhorn

Joined: Jun 21, 2006
Posts: 5
Yes, it is in the session... I think the jsp isn't able to convert the Html data (the text fields and radio buttons) back into Item objects and put it in the ArrayList to send the stuff BACK to the action class.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Since your ActionForm is in session scope, the most likely cause of this error is a misspelling of one or more properties. Check the spelling very carefully. The properties much match the getters and setters in your ActionForm.

If you're still not able to figure it out, please post:

The code for your ActionForm class
The code for your JSP

and we'll help you find the problem.
Amy May
Greenhorn

Joined: Jun 21, 2006
Posts: 5
I think something was messed up in my workspace. I re-created my enviornment and made sure all the getter/setters and scope were defined correctly according to the information you suggested above works and EVERYTHING works perfectly fine. Thank you SOOOOO MUCH for all your help.
 
 
subject: Generate Radio Buttons