Вопрос

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