• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Struts 2: Custom display of error messages

 
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm migrating my Struts 1 app to Struts 2. In my Struts 2 app, I have an interceptor class that is meant to validate my error fields. It has this method to add action errors ...

private void addActionError(ActionInvocation invocation, String message) {
Object action = invocation.getAction();
if(action instanceof ValidationAware) {
((ValidationAware) action).addActionError(message);
}
}

How do I display these on my JSP? On my target JSP, I have this Struts 1 code (which is obviously invalid in Struts 2):

<logic:messagesPresent>
<div id="errorBlock" style="display: block;" class="module error hidden">

The following error has occurred:


    <html:errors></html:errors>

<div class="clear"></div>
</div>
</logic:messagesPresent>


Thanks, - Dave
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<s:actionerror/> and <s:fielderror/>.

There's already validation and workflow interceptors--are you using your own?
 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I created my own because I have to validate the field using a custom algorithm. Here is what I have in my struts.xml file, but certainly let me know the correct way to do this if this is not it ...

<interceptors>
<interceptor name="AccountsInterceptor" class="com.myco.regui.struts.accounts.AccountsInterceptor"></interceptor>
<interceptor-stack name="AccountsInterceptorStack">
<interceptor-ref name="prepare"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="exception"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="params"/>
<interceptor-ref name="AccountsInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="AccountsInterceptorStack"></default-interceptor-ref>

Thanks, - Dave
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That'll work fine.

Really, though, there's no reason to do that via a custom interceptor--you can write your own validators and plug them in to the existing system or use the validation logic in an action's validate() method where it'll work along with the existing framework validators. I don't know your needs, of course, but I have yet to encounter a situation where I had to write an interceptor just to do custom validation.
 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I want to try the way you suggested, but was curious ... do you mean adding a "validate" method to the action class with a String return type?

Thanks, - Dave
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I mean adding a void validate() method to the action class, which is the signature expected by the interface.

If it's application-wide validation it'd be cleaner to create a validator.
 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To put some closure to this thread, Im using s:actionerror, per David's suggestion ...



Thing is, I only want to display the above if there are action errors. If not, I would prefer that none of the above HTML be visible. How do I do this?

Thanks, - Dave
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<s:if test="hasActionErrors()">...</...>

The API docs might be helpful to you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic