3

I am not getting action error, when I am using dispatcher result in Struts 2.

In action class, the below code is used to add error message.

addActionError("Error");
return "Failure";

In Struts config:

...
<result name="Failure" type="dispatcher">/ShowError.do</result>
...
<action name="ShowError">
    <result>/jsp/ShowActionErrror.jsp</result>
</action>

In ShowActionErrror.jsp:

<div class="error"><s:actionerror /></div>

But, I am not getting action error message in ShowActionErrror.jsp?

Roman C
  • 49,761
  • 33
  • 66
  • 176
john
  • 859
  • 2
  • 12
  • 25
  • To use the dispatcher with actions you need to configure your container to process forwards through your S2 filter. In general I recommend against it, though. – Dave Newton Sep 04 '13 at 13:05

2 Answers2

4

Dispatcher is the default Struts2 Result Type.

It is used to execute the standard behavior, from Action to JSP.

Special results are needed to perform other operations like Action to Action to JSP, like RedirectAction Result, Chain Result (discouraged), etc. Please note that you will lose the Value Stack objects (hence the ActionErrors and ActionMessages) during this kind of routing.

In your case, you should simply use the default Dispatcher Result Type:

<result name="Failure" type="dispatcher">/jsp/ShowActionErrror.jsp</result>

or simply

<result name="Failure">/jsp/ShowActionErrror.jsp</result>

Read more on Result Configuration.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
2

Use dispatcher result type with JSPs

<result name="Failure" type="dispatcher">/jsp/ShowActionErrror.jsp</result>
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Yes, I already tried with JSP It's working fine. But why it is not working with action ? – john Sep 03 '13 at 11:25
  • 1
    Because dispatcher isn't intended to forward to another action. – Steven Benitez Sep 04 '13 at 02:53
  • 1
    @john Struts2 discourages the action chaining in any way, and you even didn't configure the action you talking about. Looking at the code in your question non-Struts1 user will mean that you used the `dispatcher` result with the location `/ShowError.do`, and Struts2 will not serve it because it doesn't accept forwarded requests. – Roman C Sep 04 '13 at 08:47