Jsp
First create the index.jsp page
<Scripttype= "Text/javascript"> $(function () { $("#username"). Click (function() {$.ajax ({URL:"List",// request address type:"POST", DataType:"JSON", Success:function(data) {//Datais the default, receiving the information returned by the foreground //alert ("Success"); $.each (data,function(index,element) {// loop $ ("#tr"). Append ("<th>"+Element.userid+"</th>"); $("#tr"). Append ("<th>"+Element.username+"</th>"); $("#tr"). Append ("<th>"+Element.password+"</th>"); }) } }); });</Script></Head><Body> <TableAlign= "Left"Border= "1"cellpadding= "Ten"cellspacing= "0"ID= "form"> <TR> <th>Id</th> <th>User name</th> <th>Password</th> </TR> <TRID= "TR"> </TR> </Table></Body>
Controller
The above index.jsp sends a list request, followed by the controller that handles the request
@Controller Public classUsercontroller {Private Static FinalString NONE =NULL; //Inject Productservice@AutowiredPrivateUsermapper Usermapper; /*** Query All users *@return */@RequestMapping ("/list") @ResponseBody// return data in JSON format PublicList<user>list () {list<User> list =usermapper.list (); returnlist; }}
Mapper.xml
< mapper namespace = "Com.ssm.mapper.UserMapper" > <!-- query all users --> < select id = "list" Resulttype = "Com.ssm.entity.User" > select * FROM user </ select > </ mapper >
User
Public class User { private Integer userid; Private String username; Private String password;}
SPRINGMVC returning JSON-formatted data via AJAX asynchronous request