Adrian Totolici
Adrian Totolici

Reputation: 233

how to start an action after the webpage is loaded using struts2

Is there a way to perform an submit action on a page after the page is loaded, whit out using a button or a submit form.

I need this to populate my web page whit data from database and using a Java class. For the moment i can do this, but i am using a form to submit and my data appears on table only after i hit the button.

I just want to appear when the page is loaded. Can someone give some suggestion ?

struts.xml

    <action name="afisusers" class="eu.memshare.payments.action.ListaUsers">
        <result name="succes">users.jsp</result>
    </action>

page.jsp

    <s:action="afisusers"/>

Upvotes: 0

Views: 986

Answers (1)

Arvind Sridharan
Arvind Sridharan

Reputation: 4045

you can initate an ajax request on load of the page or use jQuery's $.ajax function (I would recommend this).

To include jQuery you can use the script hosted on Google CDN. put this in the head tag of your page.

<head>
<!-- .... other stuff ....  -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    submitForm();
});

function submitForm(){
   $.ajax({
      url : '/afisusers.action',
      data : $("#form").serialize(),
      success : function(data){
          //do what you want here.
          alert("form submitted");
      }
   });
}
 </script>
<!-- .... other stuff ....  -->
</head>

replace "#form" with # + your form id.

specific to struts, you can return the response as a json object or a jsp. the JSON object data will contain whatever you return from the action.

If you are using a browser like chrome, right click and hit inspect element. you will see the javascript errors that you are getting.

Upvotes: 1

Related Questions