anichhangani anichhangani - 1 month ago 19
Java Question

Passing parameter from view to action in Struts 2

I am trying to pass a value in JavaScript variable (i.e an access token from Facebook) from the view section of the framework to action where the corresponding Java object variable is stored.

What are the possible ways to do this?

Answer

Create a hidden field and save the value in it. Access the action via submitting a form like in this answer. The example of saving a value to a hidden field using jQuery

<s:hidden name="myHiddenField"/>
<script type="text/javascript">
  function saveValue(value) {
    $('input:hidden[name="myHiddenField"]').val(value);
  }
</script> 

In the action class you should have a property for myHiddenField

public class MyAction extends ActionSupport {
  private String myHiddenField;
  //getters and setters here 
  ...
}

now you configure the action to execute using defaultStack of interceptors and when you submit the form the field will be populated to the action object.