博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springmvc-json小案例
阅读量:7077 次
发布时间:2019-06-28

本文共 4429 字,大约阅读时间需要 14 分钟。

hot3.png

controller类-----------------------------

@RequestMapping(value="/emp")
public class EmpAction {
 /*
  * 返回json数据
  *   1.导入jar包:jackson-core-asl.jar   jackson-mapper-asl.jar
  *   2.springmvc配置文件
  *   3.
  */
 @RequestMapping(value="/bean2json")
 public  @ResponseBody Emps bean2json(){
  Emps emps=new Emps();
  emps.setId(1);
  emps.setName("李永");
  emps.setSex("男");
  emps.setDate(new Date());
  return emps;
 }
 @RequestMapping(value="/list2json")
 public  @ResponseBody List<Emps> list2json(){
  List<Emps> empsList=new ArrayList<Emps>();
  Emps emps1=new Emps();
  emps1.setId(1);
  emps1.setName("李永");
  emps1.setSex("男");
  emps1.setDate(new Date());
  Emps emps2=new Emps();
  emps2.setId(2);
  emps2.setName("李永2");
  emps2.setSex("男2");
  emps2.setDate(new Date());
  Emps emps3=new Emps();
  emps3.setId(3);
  emps3.setName("李永3");
  emps3.setSex("男3");
  emps3.setDate(new Date());
  empsList.add(emps1);
  empsList.add(emps2);
  empsList.add(emps3);
  return empsList;
 }
 @RequestMapping(value="/map2json")
 public  @ResponseBody Map<String,Emps> map2json(){
  Map<String,Emps> empsMap=new LinkedHashMap<String,Emps>();
  Emps emps1=new Emps();
  emps1.setId(1);
  emps1.setName("李永");
  emps1.setSex("男");
  emps1.setDate(new Date());
  Emps emps2=new Emps();
  emps2.setId(2);
  emps2.setName("李永2");
  emps2.setSex("男2");
  emps2.setDate(new Date());
  
  empsMap.put("one", emps1);
  empsMap.put("two", emps2);
  
  return empsMap;
 }
}

--------------------------------springmvc配置文件-----------------------------------

<context:component-scan base-package="cn.gdpe.spring"></context:component-scan>

     
      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
         <list>
          <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
         </list>
        </property>
      </bean>

--------------------------------页面---------------------------------------------------------

<html>

  <head>
    <title>My JSP 'success.jsp' starting page</title>
 <script type="text/javascript" src="jquery-1.8.2.js"></script>
  </head>
 
  <body>
    <input type="button" value="Emp转json" />
    <input type="button" value="List&lt;Emp&gt;转json" />
    <input type="button" value="Map&lt;String,Object&gt;转json" />
  </body>
  <script type="text/javascript">
   /* function createXmlHttp(){
         var xmlhttp;
         if (window.XMLHttpRequest){
               xmlhttp=new XMLHttpRequest();
          }else{
               xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
           }
         return xmlhttp;
      }
     var xmlHttp;
     function ajaxObject(){
         xmlHttp=createXmlHttp();
         var url="${pageContext.request.contextPath}/emp/map2json.action";
         xmlHttp.open("get", url, true); 
         xmlHttp.onreadystatechange = updatePage; 
      // xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 
         xmlHttp.send(null); 
     }
    
     function updatePage() { 
         if (xmlHttp.readyState == 4) { 
          if (xmlHttp.status == 200) { 
           var response = xmlHttp.responseText; 
           var jsons=eval("(" + response + ")");
           alert(response);
          } else{
             alert("status is " + xmlHttp.status);
            } 
      } 
     } 
   $(function(){
    $("input:last").click(function(){
     ajaxObject();
    });
   }) */
   $(function(){
    $("input:first").click(function(){
     $.ajax({
      type:"POST",
      url:"${pageContext.request.contextPath}/emp/bean2json.action",
      //1,dataType:"json",
      dataType:"text",
      success:function(data){
       /*1,alert(dates.id);*/
       var json=$.parseJSON(data);
       var hiredate=json.date;
       var date=new Date(hiredate);
       var now=date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日";
       alert(now);
      }
     });
    });
    $("input:eq(1)").click(function(){
     $.ajax({
      type:"POST",
      url:"${pageContext.request.contextPath}/emp/list2json.action",
      dataType:"json",
      success:function(data){
       for(var i=0;i<data.length;i++){
        alert(data[i].id+data[i].name+data[i].sex);
       }
      }
     });
    });
    $("input:last").click(function(){
     $.ajax({
      type:"POST",
      url:"${pageContext.request.contextPath}/emp/map2json.action",
      dataType:"json",
      success:function(data){
       alert(data["one"].name);
      }
     });
    });
   })
  </script>
</html>

---------------------------------实体类-------------------------------------------------------

public class Emps {

 private int id;
 private String name;
 private String sex;
 private Date date;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 public Date getDate() {
  return date;
 }
 public void setDate(Date date) {
  this.date = date;
 }
 
}

转载于:https://my.oschina.net/chenliyong/blog/677771

你可能感兴趣的文章
PHP递归遍历文件夹
查看>>
用户系列之五:用户SID查看之终结版
查看>>
ubuntu 11.10下载和编译Android源码
查看>>
千兆级LTE的一小步,5G之路的一大步
查看>>
跟我一起写 Makefile(一)
查看>>
管理日志-原创理论工具--技能方格图
查看>>
MPLS TE第一步:创建基本TE隧道
查看>>
windows中禁止U盘写入
查看>>
Bash技巧总结
查看>>
在窗体中添加标签Label、Icon图标
查看>>
Perl脚本学习笔记(一)
查看>>
基于BIND实现DNS的解析、主从、子域、请求转发、访问控制
查看>>
Oracle Number用法
查看>>
nat
查看>>
基于Cisco技术的MPLS原理以及应用实现[一]
查看>>
iPhone/Mac Objective-C内存管理原理
查看>>
极速理解设计模式系列:14.轻量级模式(Flyweight Pattern)
查看>>
Resin HTTPS 安装指南
查看>>
无法加入域
查看>>
在RHEL5下构建LAMP网站服务平台之架设Discuz!论坛
查看>>