0

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.

7
  • can you be a bit clear what you trying to do? and what is not working Commented Aug 30, 2012 at 1:45
  • I am trying to display the value of param_ID in the action class.I am getting it as null.Is the code right for redirecting the jsp values to the action class? Commented Aug 30, 2012 at 1:57
  • please help.Trying to search for a solution since a long time. Commented Aug 30, 2012 at 2:07
  • 2
    and what this param_id? it should be hidden field to save value and make sure to define a property in your action class with name param_ID Commented Aug 30, 2012 at 2:13
  • 1
    Why do you insist on violating java naming conventions? Commented Aug 30, 2012 at 11:14

4 Answers 4

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

That's what the beauty of question,its letting all of us guessing ;)
0

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

Comments

0

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%>" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.