Spring註解處理Ajax請求-JSON格式[系統架構:Spring+SpringMVC+MyBatis+MySql]

來源:互聯網
上載者:User

標籤:lin   系統   err   相同   str   method   list   size   架構   

這是自己曾寫的一個系統(養老保險管理)中的一個小片段,今天突然想到把請求資料改成json,該如何處理。(Spring+SpringMVC+MyBatis+MySql)如下

1.前端jsp頁面

<div class="tab_tip">     請輸入[社會安全號碼或姓名]     <input type="text" class="tab_getText" id="tab1_getText"> <input type="button" class="tab_selectButton" id="tab1_selectButton" value="查詢"></div><!-- 省略代碼 --><table class="table" id="table1" cellspacing="0" cellpadding="0">    <tr>        <th>個人編號</th>        <th>社會安全號碼</th>        <th>姓名</th>        <th>性別</th>        <th>民族</th>        <th>出生年月</th>        <th>參加工作時間</th>        <th>繳費基數</th>        <th>單位編號</th>        <th>單位簡稱</th>        <th>人員狀態</th>    </tr></table>    

2. JavaScript處理代碼

$(function(){            $("#tab1_selectButton").unbind(‘click‘).click(function(){                var tab1_getText = $.trim(document.getElementById("tab1_getText").value);                if(tab1_getText != ""){                    $.ajax({                        type:"POST",                        url:"getStaffAllSelect/"+tab1_getText+"/0",                        async:false,                        dataType:"json",                        success:function(data){                            $(".staffallinfotr").remove();                            for(i=0;i<data.length;i++){                                $("#table1").append(‘<tr class="staffallinfotr"><td>‘+data[i].sid+‘</td><td>‘+data[i].sino+‘</td><td>‘+data[i].sname+‘</td><td>‘+data[i].ssex+‘</td><td>‘+data[i].snation+‘</td><td>‘+data[i].sbirth+‘</td><td>‘+data[i].sdaj+‘</td><td>‘+data[i].pbase+‘</td><td>‘+data[i].cid+‘</td><td>‘+data[i].csn+‘</td><td>‘+data[i].sstate+‘</td></tr>‘);                            }                        },                        error:function(){                            alert("error");                        },                        complete : function(XMLHttpRequest,status){                               if(status==‘timeout‘){                                  ajaxTimeoutTest.abort();                                    alert("逾時");                                 }                          }                                              });                }else{                    alert("請輸入個人編號或姓名!");                }            });        });

3.Controller類中方法(註:StaffAllSelectDTO:和前端jsp頁面中的table欄位相同,因為代碼過長,就不再貼出)

@RequestMapping("/getStaffAllSelect/{sname}/{start}")    public @ResponseBody List<StaffAllSelectDTO> getStaffAllSelect(@PathVariable String sname, @PathVariable int start){        List<StaffAllSelectDTO> staffAllSelectDTOList = staffServices.getStaffAllSelectByName(sname, start, 10);        for(int i=0;i<staffAllSelectDTOList.size();i++){            System.out.println(staffAllSelectDTOList.get(i));        }        return staffAllSelectDTOList;    }

4. Services類中方法

@Override    public List<StaffAllSelectDTO> getStaffAllSelectByName(String sname, int start, int limit) {        // TODO Auto-generated method stub        List<Staff> staffList = staffDAO.getStaffsByName(sname, start, limit);        List<StaffAllSelectDTO> staffAllSelectDTOList = staffFactory.staffAndStaffPaymentToStaffAllSelectDTO(staffList);        return staffAllSelectDTOList;    }

5. DAO類中方法

@Override    public List<Staff> getStaffsByName(String sname, int start, int limit) {        // TODO Auto-generated method stub        Map<String, Object> map = new HashMap<String, Object>();        map.put("sname", sname);        map.put("start", start);        map.put("limit", limit);        return getSqlSession().selectList("com.staff.entity.StaffMapper.getStaffsByName", map);    }

6. DTO: StaffAllSelectDTO:和前端jsp頁面中的table欄位相同,因為代碼過長,就不再貼出)

7. Mybatis

<select id="getStaffsByName" parameterType="Map" resultMap="StaffResult">    select s.sid,s.sino,s.sname,s.ssex,s.snation,s.sbirth,s.sdaj,s.sstate,s.spbase,s.cid,c.csn,c.cname from staffinfo s, companyinfo c where s.cid = c.cid    <if test="sname != null and !&quot;&quot;.equals(sname)">and s.sname like CONCAT(‘%‘,#{sname,jdbcType=VARCHAR},‘%‘)</if>    <if test="start!=null and limit!=null">          limit #{start},#{limit}      </if>   </select>

 

Spring註解處理Ajax請求-JSON格式[系統架構:Spring+SpringMVC+MyBatis+MySql]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.