• 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

indexed buttons

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey there!
I've got a simple address database.
I've also got a jsp-page where all addresses are listed.
Each address record has a delete-button and an edit-button.
When I click one, how will Struts (or I) know, which row the user pressed?
All the delete buttons have the same name of course, because they are catched within the Action-Class and must be specified in the Form-Class.
Is there some way I can give them an index?
I know that I can say indexed="true" in the button-taglib.
But how can I catch that index in the Action Class? How do I have to construct my form class?
Thanks for any help!
 
author
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Meyer,
There was another question regarding this in this forum a while back, which I had answered. Here is the link:
https://coderanch.com/t/47611/Struts/forms-submit-collection
You can use the same technique with some modification.
However, while you can certainly use indexed properties to achieve what you want, I suggest that you use one form per row since all you do with each row is just edit/delete. Then it is really simple: Each row is a form and on submission the Action class gets the primary key of the row and deletes it or brings up the edit page.
Hope this helps
Srikanth Shenoy
Author: Struts Survival Guide - Basics to Best Practices
 
Meyer Florian
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Shenoy!
Thanx for help, I think you're the one that can solve my problem...
This is my Form class:
public class OverviewForm extends ActionForm
{
private List edit;
private List delete;
public OverviewForm()
{
edit= new ArrayList();
delete= new ArrayList();
}
public List getEdit()
{
return edit;
}
public ImageButtonBean getEdit(int index)
{
while (edit.size() + 1 < index)
{
edit.add(new ImageButtonBean());
}
return (ImageButtonBean) edit.get(index);
}
public List getDelete()
{
return delete;
}
public ImageButtonBean getDelete(int index)
{
while (delete.size() + 1 < index)
{
delete.add(new ImageButtonBean());
}
return (ImageButtonBean) delete.get(index);
}
public void setEdit(List edit)
{
this.edit= edit;
}
public void setLoeschen(List delete)
{
this.delete= delete;
}
public void reset(ActionMapping arg0, HttpServletRequest arg1)
{
bearbeiten = new ArrayList();
loeschen = new ArrayList();
}
public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1)
{
return new ActionErrors();
}
}
As you see, I'm using image-buttons (<bean:image...>), their properties are "edit" and "delete" and they are indexed, so the exactly name may be "delete[51]" or "edit[28]" etc...
When I open the JSP and click on an edit or delete button, I get the following error message in the browser: Error 500: BeanUtils.populate
and these in my console:
X Servlet Error: BeanUtils.populate: java.lang.reflect.InvocationTargetException: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
Do you know what my problem could be?
Thanx in advance!
Florian
 
Meyer Florian
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, of course the method name is "setDelete" and not "setLoeschen" (i translated the code for this post...)
 
Meyer Florian
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
doesn't anybody know any solution for my problem?
 
Srikanth Shenoy
author
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is flaw in your logic. Look at your method below:

public ImageButtonBean getEdit(int index)
{
while (edit.size() + 1 < index)
{
edit.add(new ImageButtonBean());
}
...
}
When this method is invoked with index = 0, the while condition evaluates to (0 > 1) which is false and your Image button is never added to the List thus throwing a ArrayIndexOutofBoundException.
Change it to:
public ImageButtonBean getEdit(int index)
{
while (edit.size() <= index)
{
edit.add(new ImageButtonBean());
}
...
}
That should work
Srikanth Shenoy
Author: Struts Survival Guide - Basics to Best Practices
 
Srikanth Shenoy
author
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or even this will do
public ImageButtonBean getEdit(int index)
{
while (edit.size() < index + 1)
{
edit.add(new ImageButtonBean());
}
...
}
HTH,
Srikanth Shenoy
Author: Struts Survival Guide - Basics to Best Practices
 
Srikanth Shenoy
author
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While the above solutions I suggested should work without a hitch, the better design is going with one form per row.
SrikanthAuthor: Struts Survival Guide - Basics to Best Practices
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic