The method of timing refresh by jQuery, Ajax and struts2

Time:2021-3-3

Step 1: import jQuery plug-in

Copy codeThe code is as follows:
<script type=”text/javascript” src=”js/easyui/jquery-1.8.0.min.js”></script>

Step 2: add information display place in < body > < / body >

Copy codeThe code is as follows:
<label></label>

Step 3: add JS code to the page

Copy codeThe code is as follows:
<script type=”text/javascript”>
    setInterval(“Push()”,1000*60);
    function Push() {
           $.ajax({
           type: “POST”,
           url: encodeURI(“crmMaintenance/crmAirpolicyAction-takeAllMonitor.action”),
           success: function(data) {
               $(‘#context’).text(data);
           }
     });}
  </script>

Step 4: in struts 2, process the request

Copy codeThe code is as follows:
public void takeAllMonitor() {
        try {
            HttpServletResponse response = ServletActionContext.getResponse();
            PrintWriter out;
            out = response.getWriter();
            response.setCharacterEncoding(“utf-8”);
/ / / insert business logic
                out.print(“Synchronous…” );
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

At this point, you can achieve the effect of regular refresh page, but there is still a problem, that is out.print When there is Chinese in (“), there is a problem of garbled code. If any great Xia knows how to deal with it, I would like to give some advice.