This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Struts and the fly likes Struts: Displaying Field Messages with Indexed Fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts: Displaying Field Messages with Indexed Fields" Watch "Struts: Displaying Field Messages with Indexed Fields" New topic

Struts: Displaying Field Messages with Indexed Fields

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Hi everyone,

I've run into a situation in which I just can't figure out how to accomplish what I'm after. Let me try to lay out the basic concept I'm trying to deal with.

I have a page (part of a J2EE web app) that displays a number of exception records. I'm using indexed fields so that I can display multiple exceptions and allow the user to edit them. It's possible that the user could enter some invalid information and then submit the form (such as the date 2/30/2004). I'd like to be able to print a catch block for the user so that they can easily fix the problem but, with the indexed fields, I'm not sure of how to get the message next to the proper field.

I have a form bean that contains this:

The Collection "exceptionRecords" contains a number of ExceptionRecord objects. Those objects are just JavaBeans that contain the data I want to render on the screen. Since I'm rendering a Collection, I've used nested:iterate tags, like this:

That works great for displaying my data to the user and for getting the modified data back. The resulting HTML ends up looking something like this:

Now, if I wanted to get an error message to appear beside the Date of Birth field for the 5th exception on the page, how would I do that? Hopefully, I've explained this well.


SCJP Tipline, etc.
s penumudi
Ranch Hand

Joined: Nov 17, 2004
Posts: 113
If I understand your problem correctly, then all you want to do is to display the error message beside some field in your form.

To achieve this you need to give the name for that location while adding error to ActionErrors and specify same in html:errors property attribute


in your form you would have something like this beside fifth exception.

To get the fifth element, may be u have to have some if conditions to check for 5th element.

Hope this helps

Thank you
[ December 08, 2004: Message edited by: s penumudi ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by s penumudi:

in your form you would have something like this beside fifth exception.

Thanks, but therein lies the problem. Since I'm displaying multiple exceptions using an "iterate" tag, I don't have a "5th" exception in my JSP. I just have one set of tags that is automatically expanded for every exception record I have. So, regardless of how many exceptions I have, I only have one set of tags in my JSP.

So, with that in mind, how do I differentiate the error message for different exception records?
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6280

The first parameter of ActionErrors.add() is the property name. The "property" attribute of the html:errors tag takes a runtime expression. All you need to do then is to make these two match. So, you'd probably do the following:

// in Java:
errors.add("exception" + index, new ActionError(...));

// in JSP:
<nested:iterate ... indexId="idx">
<html:errors property='<%= "exception" + idx %>' />

Junilu - [How to Ask Questions] [How to Answer Questions]
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Struts: Displaying Field Messages with Indexed Fields
It's not a secret anymore!