• 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

How can I have global and specific html:errors?

 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 415
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 415
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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> ?
 
author
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the way I have it:

Logic:


JSP:
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic