The first thing to do is to add three jar packages:
Second, in the controller:
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package Com.cgf.springmvc.handlers;import Java.util.collection;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.responsebody;import Com.cgf.springmvc.crud.dao.employeedao;import Com.cgf.springmvc.crud.entities.Employee, @RequestMapping (value= "/springmvc") @Controllerpublic class Myjson {@ Autowiredprivate EmployeeDAO employeedao;<span style= "color: #ff0000;" > @ResponseBody </span> @RequestMapping (value= "/testjson") public collection<employee> Testjson ( ) {return Employeedao.getall ();}} </span>
In the index.jsp:
<script type= "Text/javascript" src= "scripts/jquery-1.9.1.min.js" ></script><script type= "text/ JavaScript ">$ (function () {$ (" #test_json "). Click (function () {var url=this.href;var args={};$.post (Url,args, function (data) {for (Var i=0;i<data.length;i++) {var id=data[i].id;var lastname=data[i].lastname;alert (id+ "---" + LastName);}); return false;}) </script>
Learn Springmvc (20) return JSON