<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> </list> </property> </bean>
EmpAction.java
package cn.buaa.javaee.springmvc.app3; import java.util.ArrayList; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value="/emp") public class EmpAction { /** * @ResponseBody Emp 表示让springmvc将Emp对象转成json文本 */ @RequestMapping(value="/bean2json") public @ResponseBody Emp bean2json() throws Exception{ //创建Emp对象 Emp emp = new Emp(); emp.setId(1); emp.setUsername("老四"); emp.setSalary(7000D); emp.setHiredate(new Date()); return emp; } @RequestMapping(value="/listbean2json") public @ResponseBody List<Emp> listbean2json() throws Exception{ //创建List对象 List<Emp> empList = new ArrayList<Emp>(); //向List对象中添加三个Emp对象 empList.add(new Emp(1,"哈哈",7000D,new Date())); empList.add(new Emp(2,"嘿嘿",5000D,new Date())); empList.add(new Emp(3,"呵呵",8000D,new Date())); return empList; } @RequestMapping(value="/mapbean2json") public @ResponseBody Map<String,Object> maplistbean2json() throws Exception{ //创建List对象 List<Emp> empList = new ArrayList<Emp>(); //向List对象中添加三个Emp对象 empList.add(new Emp(1,"哈哈",7000D,new Date())); empList.add(new Emp(2,"嘿嘿",5000D,new Date())); empList.add(new Emp(3,"呵呵",8000D,new Date())); Map <String,Object> map = new LinkedHashMap<String, Object>(); map.put("total", empList.size()); map.put("rows", empList); return map; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>bean2json</title> <script type="text/javascript" src="js/jquery-1.8.2.js"></script> </head> <body> <input type="button" value="Emp转json"/><p> <input type="button" value="List<Emp>转json"/><p> <input type="button" value="Map<String,Object>转json" /><p> <!-- Map<String,Object>转json --> <script type="text/javascript"> $(":button:last").click(function(){ var url = "${pageContext.request.contextPath}/emp/mapbean2json.action"; var sendData = null; $.post(url,sendData,function(backData,textStaut,ajax){ alert(ajax.responseText); }); }); </script> <!-- List<Emp>转json --> <script type="text/javascript"> $(":button:eq(1)").click(function(){ var url = "${pageContext.request.contextPath}/emp/listbean2json.action"; var sendData = null; $.post(url,sendData,function(backData,textStaut,ajax){ alert(ajax.responseText); }); }); </script> <!-- Emp转json --> <script type="text/javascript"> $(":button:first").click(function(){ var url = "${pageContext.request.contextPath}/emp/bean2json.action"; var sendData = null; $.post(url,sendData,function(backData,textStaut,ajax){ //alert(ajax.responseText); var hiredate = backData.hiredate; var date = new Date(hiredate); alert(date.getFullYear() + "年" + (date.getMonth()+1) + "月" + (date.getDate()) + "日"); }); }); </script> </body> </html>Emp.java
package cn.buaa.javaee.springmvc.app3; import java.util.Date; public class Emp { private Integer id; private String username; private Double salary; private Date hiredate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Emp(Integer id, String username, Double salary, Date hiredate) { this.id = id; this.username = username; this.salary = salary; this.hiredate = hiredate; } public Emp() { super(); } }