File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts 2: Custom display of error messages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2: Custom display of error messages" Watch "Struts 2: Custom display of error messages" New topic
Author

Struts 2: Custom display of error messages

Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
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

Joined: Sep 29, 2008
Posts: 12617

<s:actionerror/> and <s:fielderror/>.

There's already validation and workflow interceptors--are you using your own?
Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jul 02, 2008
Posts: 436
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jul 02, 2008
Posts: 436
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

Joined: Sep 29, 2008
Posts: 12617

<s:if test="hasActionErrors()">...</...>

The API docs might be helpful to you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 2: Custom display of error messages