anichhangani anichhangani - 1 year ago 124
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 Source

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) {

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download