'Pass value from javascript in jsp to action class in struts2

how can i pass value from javascript to a java class in struts2? This is the link similar to what I want.However it does not work for me. I have tried something like this. Struts tag for hidden in jsp is

    function redirect(id)
    {
    window.alert("Hello"+id);
    document.getElementById('param_ID').value=id;
    document.forms["./ListAlgorithmAction"].submit();
    }

In my action class I have declared getter and setter for the param_ID.But it returns null.

Kindly help. Thank you.



Solution 1:[1]

You should use like this

<s:hidden id = "param_ID" name="xyz" />

Now you should have a getter and setter for xyz property in your struts action.

You should always have name attribute in tags mapped to attributes in Struts action.

Hope this works.

Solution 2:[2]

If the page is not submitting your hidden field, maybe the hidden field is not inside your form element. You can try pass it as a parameter to your url. Try this

function redirect(id)
{
     var form = document.forms[0];
     var url = './ListAlgorithmAction?param_ID=' + id;
     form.action = url;
     form.submit();
}

Let me know if this works.

Solution 3:[3]

If you want to call an action and return data you should use struts2-json plugin JQuery script for calling action

    var abc = $("#abc").val();
            $.getJSON('youraction.action', {
                variable -name action class : abc,
                            value to be passed:value,

        //and so on
            }, function(data) {


        //your processing code
                }
            });
            return false;
        });

If you want to just call action you may want to use $.ajax in place of $.getJson

If you want to use struts 2-json then you would also need to change your struts.xml i.e

<result name="success" type="json"></result>

It's worth a go

Solution 4:[4]

I had a similar issue and resolved it by appending the Action class from JavaScript function and appending the URL parameters as shown below:

<a href="(<%actionClassName%>.action?<%URLParms%>" />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Deepak
Solution 2 Uchenna Nwanyanwu
Solution 3 Ronald James
Solution 4 matthias_h