• 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

Struts, Iterate/Index/Checkboxes

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've read the posts on logic:iterate (and all over google) and have a snag..

I'm iterating over a Vector that contains 'AppPermission' objects.
Each AppPermission object has a boolean property 'cb'.
I use this vector (which is in the session) to prepopulate the checkboxes:

<logic:iterate id="permBoxes" name="<%=AppConstants.UserAPVector%>" indexId="permIndex" type="util.AppPermission">

<html:checkbox indexed="true" name="permBoxes" property="cb" value="true" />
<!-- Struts False Submit -->
<html:hidden indexed="true" name="permBoxes" property="cb" value="false" />

So the checkboxes show up fine, checked as specified in the session's vector. Now, I want to grab any changes to the checks, and update the session vector. To do this, here's my ActionForm code:

// Permissions Checkboxes
private AppPermission [] permBoxes;


public AppPermission [] getPermBoxes() {
return this.permBoxes;
}

public void setPermBoxes(int index, boolean thePerm) {
try {
if (permBoxes == null) {
permBoxes = new AppPermission[100];
}
permBoxes[index].setCb(thePerm);
}
catch (Exception e) {
log("Exception in setPermBoxes: " + e);
}
}


And it doesn't work. The try/catch new AP[100] thing was a suggestion from an earlier post.. might not be needed. The error I'm getting when I try any kind of action from the checkbox page is:

java.lang.NullPointerException
at org.apache.commons.beanutils.PropertyUtils.getIndexedProperty(PropertyUtils.java:515)
at org.apache.commons.beanutils.PropertyUtils.getIndexedProperty(PropertyUtils.java:428)
at org.apache.commons.beanutils.PropertyUtils.getNestedProperty(PropertyUtils.java:770)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:801)


Will it work to use the vector of AppPermissions' cb values to prepopulate the boxes, and then be able to retrieve the array of checkbox values to update the Vector? For more info, here's a snippet of the generated HTML:

...
...
<input type="checkbox" name="permBoxes[2].cb" value="true">
<!-- Struts False Submit -->
<input type="hidden" name="permBoxes[2].cb" value="false">
...
...
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michael,

I was looking up the Struts documentation on Indexed Properties.

My first observation was that your form bean does not have an accessor method with an int argument.

First note the two methods in the StringBean class, "getStringIndexed()" and "setStringIndexed()". Note that the "get" method takes an "int" and the "set" method takes an "int" and "String". The Beanutils package and Struts recognizes this arrangement of signatures as an "indexed property", in this case with the property name "stringIndexed".


You need to have a method with the below signature.
public AppPermission getPermBoxes(int i) {
return this.permBoxes[i];
}

Check the section on "Nested Properties". It is applicable to your scenario.

If this does not resolve the issue, then you could possibly think along these lines.
What is the scope of your form bean?
- Session (no problem)
- Request (might get null pointers, if array of AppPermission is not initialized with AppPermission objects.)

Note, permBoxes = new AppPermission[100]; creates an array of nulls. In this case, permBoxes[index].setCb(thePerm); could throw a NullPointerException.

Hope this helps you debug the problem. Let us know what you find.

Sheldon
 
Sheldon Fernandes
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<html:checkbox indexed="true" name="permBoxes" property="cb" value="true" />
<!-- Struts False Submit -->
<html:hidden indexed="true" name="permBoxes" property="cb" value="false" />

The "Struts False Submit" idea is a good one.
 
Michael Enzo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heh well, the soln i'm using is ghetto..
I use a .js to iterate over the boxes, forming an int by <<'ing a 0x001 value and adding the value when the box is checked.
The resulting mask can be passed as a string and used to update the vector by iterating through the vector and doing a .get(i), .setCb(true/false), and putting the obj back (.set(i, Object)).

When you revisit the checked page (or just action back to the same page), the checks are updated since it always pulls from the vector (which got updated).

I guess you could argue that passing a string is better than an array anyways, but it would be nice to get the array of boxes back as an array of Objects.. if anyone figures that part out, please post!

Thx --
m
 
reply
    Bookmark Topic Watch Topic
  • New Topic