wood burning stoves 2.0*
The moose likes Struts and the fly likes How can I have global and specific html:errors? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How can I have global and specific html:errors?" Watch "How can I have global and specific html:errors?" New topic
Author

How can I have global and specific html:errors?

Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
Basically, when I have an error with some field, I usually put "must enter" or "must be at least 10 characters" next to the field itself. What I also want to do is to put "Field address has not been entered" or "Field address is invalid" at the top of the form.

How can I accomplish that?

I found this article: http://husted.com/struts/tips/017.html, but it doesn't help me at all. First of all, it's horribly written with a lot of mistakes in it, and second, it uses ActionErrors, which are deprecated.

Anybody has any clues? Thanks a lot in advance!
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
hi

i think <html:errors/> at the top of the form will display all the error messages .is this what u wanted ??
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
html:errors will display ALL error messages. So what will the user think if he sees

"must enter" at the top? It wouldn't make any sense.

I want to display the "global" error messages at the top, and field-specific next to the fields. That's what I don't know how to do (without hacking struts).
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
oh yah i got it what u need and ofcourse this can be done . so while adding the errors add all the global errors in the format

errors.add("Global",new ActionError(....));

and the other errors specific to the fields by errors.add("fieldname",....)

and finally in ur jsp at the top of the page use <html:errors property="Global"/>


i hope u got what i am saying
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
Yes I got what you're saying, but I have a feeling you have no idea what you're saying.

1) Read my original post. "it uses ActionErrors, which are deprecated." Isn't there another way, without using the deprecated stuff?

2) If I do what you tell me to do, I will only be able to use 1 error message. Which means I'd have to concat Strings, which is pretty annoying. Isn't there a better way to do it?
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
hello

Whatu want me to post the code itself r what ?? u should apply a little sense too .....u know that actionerrors are deprecated then use actionmessages(try to find out y actionmessages are introduced atleast) and no need to concat and all ....just try doing something instead of posting it in forums
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
If there's no need to concat, then HOW would you display multiple messages as "global" using your way? come up with 20 different message names and put them all as separate <html:errors> ?
Jonathan Lehr
author
Greenhorn

Joined: Oct 07, 2003
Posts: 20
Hi Ivan,

What I usually do is post ActionMessage instances under Globals.MESSAGES_KEY for page-level messages, and under Globals.ERROR_KEY for field-level messages. Then I use the <html:messages> tag to render the page-level messages, and use <html:errors> (depending on requirements) either at the top of the page to display all the field-level errors, or else in-line next to each field.

By the way, I recently checked some code into the StrutsLive open source project that automatically prefixes error messages with the associated field label from the JSP (provided that JSP uses <bean:message> tags to render the labels). This is helpful in cases where an application may be required to display all error messages at the top of the page.

I need to add a bit of functionality to make the prefixing behavior optional, but I'm hoping to include that in a new release (0.2.3) due out next week. Here's the project's URL:

http://strutslive.dev.java.net

Jonathan
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
I just figured out something: you can add multiple messages under the same property name - I thought property names were a hash key, but no, they're kept as a list.

So, at the top you can do <html:errors property="global"> and in code you can add like 10 different messages for "global". Wee.
rangababu chakravarthula
Greenhorn

Joined: Jan 02, 2004
Posts: 7
lets take an example of login error.

In your action class
ActionMessages messages = new ActionMessages();

if(invalid userid/pwd combination){
messages.add("logininvalid", new ActionMessage("invalid.user"));
saveMessages(request,messages);
return forward = mapping.findForward("error");
}

if(usernamenotpresent){
messages.add("missingusername", new ActionMessage("username.missing"));
saveMessages(request,messages);
return forward = mapping.findForward("error");
}

In your Applicationresources.properties file add this line
invalid.user=Invalid Username/Password combination
username.missing=Username is required

In your JSP

In this case the message is going to be displayed if present
<html:messages id="msg1" message="true" property="logininvalid">
<bean:write name="msg1"/><br>
</html:messages>

In this case you are not displaying the error message yet. just defining a bean to store the error message if present

<html:messages id="msg0" message="true" property="missingusername">
<bean:write name="msg0"/><br>
<bean efine id="usernameerror" name="msg0" />
</html:messages>

<logic resent name="usernameerror">
<font color="red">User Name:</font>
</logic resent>

<logic:notPresent name="usernameerror">
User Name:
</logic:notPresent>
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
Here's the way I have it:

Logic:


JSP:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I have global and specific html:errors?
 
Similar Threads
more than html:error
Mulitple Image submit buttons question (code needed)
html:submit tag with an image
how to error msg next to an html comonent ?
html:hidden, property & javascript