Pass a parameter with struts 2 action

jose picture jose · Oct 18, 2012 · Viewed 8k times · Source

How can I pass a parameter with struts 2 action?.

Here is my code.

<s:form>

    <s:select name="menuItem" list="menuItems" listKey="menuItemID"
        listValue="menuItemName" headerKey="" headerValue="--MenuItems--"
        cssClass="selectbox_bg2" id="select"
        onchange="handleChange(this.value)" />

    <s:textfield name="select_value" id="select_value" />

</s:form>

<script type="text/javascript">

function handleChange(value) {

    window.location = "callMyAction?ValueToSubmit=" + value;

}

</script>

My Question is How can I get this parameter(value) in my action class. and passing a parameter to return jsp page.

Thanks..

Answer

Vasily Komarov picture Vasily Komarov · Oct 18, 2012

1 Just create "valueToSubmit" variable in your action class with public getter and setter

public MyAction extends ActionSupport {

  private BigDecimal valueToSubmit;

  public String execute{
    ... some code.....
  }

  public BigDecimal getValueTOoubmit(){
    return valueToSubmit;
  }

  public void setValueToSubmit(BigDecimal valueToSubmit){
    this.valueToSubmit = valueToSubmit;
  }
}

Struts2 ParametersInterceptor will get the parameter value from request and set it to action parameer automaticly.

2 To read this parameter in the action result jsp page just use some struts tags

  <s:property value="valueToSubmit"/>, 
  <s:textfield name="valueToSubmit"/>, 
  etc..