PatYuen
PatYuen

Reputation: 11

(Struts2) How to add new parameters into Action instance from an interceptor?

I want to make an interceptor to pre-process XML request. In this interceptor, it parse the XML and put the processed info into ValueStack so that it can map to the instance in the action. However, I have tried so many methods but none of them is work. Can anyone help me and give me advises? Thanks!

In Interceptor

//Get ValueStack
ActionContext ctx = invocation.getInvocationContext();
Map ctxMap = ctx.getValueStack().getContext();

//Have tried the following but none of the following is work
ctxMap.get("com.opensymphony.xwork2.ActionContext.parameters")).put("data1", "1");
ctxMap.get("parameters")).put("data1", "1");
ctxMap.get("request")).put("data1", "1");
ctx.getParameters().put("data1",new String[]{"1"});

//Sample Action Class
public class TestAction extends ActionSupport {
     private String data1;
     public String execute() {  
         System.out.println("data1 value: " + data1);
         return SUCCESS;
     }
//Get Setter is omitted here
}

Upvotes: 1

Views: 3022

Answers (1)

Bibin
Bibin

Reputation: 434

You should add the processed value into value stack inside interceptor

ActionContext.getContext().getValueStack().setValue("data1",yourdata);

If your action instance has been created at the time of interceptor invocation, it should add the value. If its not working, there should be something wrong with order of your interceptor stack.Try to put your interceptor at the bottom of stack, so that we can guarantee that rest of processing is over and binding to your action property is working as desired.

Upvotes: 3

Related Questions