1

This is my struts 2 flow where i am using action chaining

JSP--->Action1--->Action2--->ResultJsp

With action chaining , my understanding is that request is forwarded from action1 to action2.So if i pass some parameter from action1 to action 2 it should be set in new action instance variable(/value stack created for new action).But its not happening

Below is my code in action1

@Result(name = "displayEmployee",type = "chain",
        params = {
            "namespace", "/employee",
            "actionName", "Employee-lookup!search",
            "empMode", "true"

        })


@Action("display-employee!displayEmployee")
  public String displayEmployee() {
    return "displayEmployee";
  }

Now in Action 2 i.e display-employee , i have boolean property with name empMode. But i get the value as false though i should get it true because i am passing it as attribute in result annotation. As my understanding in action chaining, all request paramaters are forwarded from action1 to action2. Basically new value stack is created for action2 which contains the variables which were present in action1. So why value true is not set for empMode property in action 2?

M Sach
  • 33,416
  • 76
  • 221
  • 314
  • did u configured anywhere interceptors – PSR Mar 01 '13 at 14:20
  • try with default stack once.Just for testing only – PSR Mar 01 '13 at 14:42
  • i think .Do like that – PSR Mar 01 '13 at 14:57
  • I don't know but this will get it working then you can determine after the fact why: Set empMode as a property of both actions, and manually set it to true in the initiating action, before forwarding to the second. Change the stack to the receiving action to the default stack. – Quaternion Mar 01 '13 at 17:16
  • @Quaternion Yes i already tried manually setting it to true in initiating action.It works. But can't i pass the parameters except only two i.e nameSpace and actionName ( like i passed empMode.)? Struts 2 Documents does not mention anything like this. It just say you can pass any number of key value pair. – M Sach Mar 01 '13 at 17:27
  • Really doesn't matter how do you pass parameters by one or by two in action or after. – Roman C Mar 01 '13 at 18:33

2 Answers2

2

I don't think you can do this, there is no mention of being able to pass additional parameters using the chain result type: http://struts.apache.org/release/2.3.x/docs/action-chaining.html

The result is of type chain... so it will need to interpret and handle your parameters, because after that a new action is started. But chain does not have this facility (look at the source): http://grepcode.com/file/repo1.maven.org/maven2/org.apache.struts.xwork/xwork-core/2.3.1.1/com/opensymphony/xwork2/ActionChainResult.java

If this was a normal redirect, they you could add these parameters into the request, as you are doing.

In other words, add the required properties to the action and chain will do what you need it to, because the chain result has no facilities to handle properties other then "namespace" and "action".

Quaternion
  • 10,380
  • 6
  • 51
  • 102
  • +1... and I would add that, according to the documentation, Action chaining is discouraged – Andrea Ligios Mar 01 '13 at 22:13
  • I don't remember the documentation being so direct, it is good that it now is. I talked about this earlier here: http://stackoverflow.com/questions/4956603/action-redirect-in-struts-xml personally I think chaining indicates actions which are too thick. If you really want to reuse action logic it should be moved to the service layer where it probably belongs. Chaining and that struts2-jquery plugin I think are among the two worst things new struts2 users fall into using. – Quaternion Mar 01 '13 at 22:58
  • For the records, it is from http://struts.apache.org/release/2.0.x/docs/action-chaining.html , the green, *Don't Try This at Home* box: As a rule, Action Chaining is not recommended. First explore other options, such as the Redirect After Post technique. – Andrea Ligios Mar 02 '13 at 14:01
0

if you have no other option but to use action chaining and need to pass parameters from one to the other, try either of the following methods

  1. have corresponding getter/setter methods in both actions. e.g: if you want to pass 'empMode' parameter from Action1 to Action2. have getEmpMode()/setEmpMode() methods in both actions.

  2. add the parameters to you want to pass to parameter map in the action context. You can access the parameter map using ActionContext.getContext().getParameters(). Add the new parameters you want to pass to Action2 to this map from Action1. Provided Action2 has corresponding getter/setters for the new parameters and given that 'params' interceptor is configured in the stack Action2 will get correctly populated.

Dev Blanked
  • 8,555
  • 3
  • 26
  • 32