This is a small fragment of a system (endowment insurance Management) that I have written about, and today it suddenly occurred to me how to change the request data to JSON. (Spring+springmvc+mybatis+mysql) as follows
1. Front-end JSP page
<Divclass= "Tab_tip">Please enter [ID number or name]<inputtype= "text"class= "Tab_gettext"ID= "Tab1_gettext"> <inputtype= "button"class= "Tab_selectbutton"ID= "Tab1_selectbutton"value= "Query"></Div><!--Omit Code -<Tableclass= "Table"ID= "Table1"cellspacing= "0"cellpadding= "0"> <TR> <th>Personal number</th> <th>ID number</th> <th>Name</th> <th>Gender</th> <th>National</th> <th>Date of birth</th> <th>Participation in working hours</th> <th>Contribution Base</th> <th>Unit number</th> <th>Unit abbreviation</th> <th>Personnel status</th> </TR></Table>
2. JavaScript processing code
$(function(){ $("#tab1_selectButton"). Unbind (' click '). Click (function(){ varTab1_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 (Timeout); } } }); }Else{alert ("Please enter your personal number or name!"); } }); });
3.Controller Chinese Law (note: staffallselectdto: Same as the table field in the front-end JSP page, because the code is too long, it is no longer posted)
@RequestMapping ("/getstaffallselect/{sname}/{start}") publicint 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. Methods in the Services class
@Override Public int int limit) { // TODO auto-generated method stub list<staff> stafflist = staffdao.getstaffsbyname (sname, start, limit); List<StaffAllSelectDTO> staffallselectdtolist = Stafffactory.staffandstaffpaymenttostaffallselectdto (stafflist); return staffallselectdtolist; }
5. Methods in the DAO class
@Override Public int int limit) { // TODO auto-generated method stub 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: Same as table field in front-end JSP page, because the code is too long, no longer post
7. Mybatis
<SelectID= "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<ifTest= "Sname! = null and!"". Equals (sname) ">and s.sname like CONCAT ('% ', #{sname,jdbctype=varchar}, '% ')</if> <offTest= "Start!=null and Limit!=null">Limit #{start},#{limit}</if> </Select>
Spring Annotations handle AJAX Requests-json format [System architecture: Spring+springmvc+mybatis+mysql]