Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts 2: Custom display of error messages

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • 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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<s:if test="hasActionErrors()">...</...>

The API docs might be helpful to you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic