4

Possible Duplicate:
Can I propagate struts2 ActionErrors between different action classes?

in action.Test1

public class Test1 extends ActionSupport {

    public String execute() {
        addActionMessage("Hello in test2");
        return SUCCESS;
    } else {
      addActionError("Please try again");
        return INPUT;
    }
}

in struts.xml

<action name="test1" class="action.Test1">
        <result name="success" type="redirect">test2</result>
  </action>
<action name="test2" class="action.Test2">
        <result name="success">/test2.jsp</result>
</action>

I want to display ActionMessage and ActionError in test2.jsp

Community
  • 1
  • 1
xrcwrn
  • 5,339
  • 17
  • 68
  • 129
  • [Check this blog..](http://glindholm.wordpress.com/2008/07/02/preserving-messages-across-a-redirect-in-struts-2/). –  Sep 04 '12 at 07:21

1 Answers1

1

For redirect it means a new request, which will make the framework drop all data related to the first request and new object of request and response will be placed in the values stack and hence in your case your ActionMessage and ActionError data lost.

If you are doing it in same App context it's more advisable to use redirectAction if it's going to the other action as redirect result is used to redirect to another URL (web resource).

In order to save the data you have these options

  1. Use session to save values and retrieve values in other action.
  2. Make use of scope
Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204
  • 3
    It is not impossible to do such thing, there are some instances that you need to redirect and you need to pass Action Messages which is why this scenario still supported by struts2. The OP is asking for help and instead you gave an unnecessary lecture. – Macchiato Feb 01 '13 at 04:30
  • @Macchiato: you have not read question nor answer properly as i already gave the solution for doing this and its always good to give background how things works rather than giving 2 lines of code.This comment is not to object your opinion and i fully respect your view. – Umesh Awasthi Feb 03 '13 at 09:54
  • 1
    This guy has written an interceptor that should do the job for you http://glindholm.wordpress.com/2008/07/02/preserving-messages-across-a-redirect-in-struts-2/ – Kris Feb 12 '13 at 00:42
  • @Kris: this is not mentioned anywhere when and how interceptor is written and moreover this is redirect so you need to save those messages else they will be lost. – Umesh Awasthi Feb 12 '13 at 03:54