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

有帮助吗?

解决方案

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.

其他提示

@user3333294: You can also do in the following way:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
    <head>
        <script type="text/javascript">
            function submitPage(){
                document.forms[0].action = "navigation.do?parameter=showNavigation";
                document.forms[0].submit();
            }
        </script>
    </head>
    <body id="mainBody">
        <form method="post" onload="submitPage()">
            ...
        </form>
    </body>
</html>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top