Search code examples
javajspstrutsjsp-tagsstruts-1

Struts Calling Action on page load


I am new to STRUTS and to this mailing list, so please be patient with me.

What I am trying, is to call an action on jsp load. I guess I need a javasript function which calls an action and 'onload' in my body.

function callAction{

... calling myAction.do

}

<body onload="callAction();" ... >

Can somebody help me with this issue?

thank you


Solution

  • You need to call your action like this if you want to navigate to another page,

    function callAction() {
        window.location = "navigation.do?parameter=showNavigation";
    }
    

    else if you want to call the same page (without loading another page), you need to use ajax like this. Here i used jQuery ajax

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
                                                                        </script>
    <script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            url : "navigation.do?parameter=showNavigation",
            type : "POST",
            success : function(data) {
                alert(data);
            }
        });
    });
    </script>
    

    Hope this helps.