user3860841
user3860841

Reputation: 19

How to pass parameters with action name dynamically in Struts 2

I want to call the Action class with some parameters, but I am not able to do this. Please, suggest me how to pass parameters with mapping struts.xml file in application.

I am using Struts2 framework in my application. My Struts file action mapping is below:

<action name="createSHBehaviour/*" class="sadagi.pro.action.CreateSHBehaviourAction" >  
<interceptor-ref name="accessRequired"/>
<interceptor-ref name="scope" />
<param name="param1">{1}</param>
<result name="success">views/createSHBehaviourView.jsp</result>            
<result name="login" type="redirect">/</result>  
</action>

and URL that used to call action class:

http://localhost:8080/pro/createSHBehaviour/123

Here createSHBehaviour is a method of CreateSHBehaviourAction class:

public class CreateSHBehaviourAction extends ActionSupport implements
    SessionAware,Parameterizable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private SessionMap<String, Object> sessionMap = null;
private Map<String, String> params = new HashMap<String, String>();

public String execute() {
    System.out.println("param1 = " + this.params.get("param1"));
    sessionMap = (SessionMap<String, Object>) ActionContext.getContext()
            .getSession();
    sessionMap.put("idSoftHumanS", "hhhh");
    sessionMap.put("fix_behaviour_mode", true);
    return Action.SUCCESS;
}

public void setSession(Map<String, Object> map) {
    sessionMap = (SessionMap<String, Object>) map;
}

@Override
public void addParam(String key, String value) {
    // TODO Auto-generated method stub
    this.params.put(key, value);
}

@Override
public Map<String, String> getParams() {
    // TODO Auto-generated method stub
    return this.params;
}

@Override
public void setParams(Map<String, String> params) {
    // TODO Auto-generated method stub
    this.params = params;
  }
   }

Please, give me a suggestion how to solve this.

Upvotes: 1

Views: 4746

Answers (1)

Roman C
Roman C

Reputation: 1

Use ParameterAware instead of Parametrizable or get parameters directly from the action context.

Map<String, Object[]> parameters =  ActionContext.getContext().getParameters();

Upvotes: 1

Related Questions