简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM