05springmvc-json-ajax使用

spring mvc 框架内置json使用的是jackson组件
pom.xml 加入如下依赖
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>

会自动引入如下jar文件
Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0
Maven: com.fasterxml.jackson.core:jackson-core:2.8.6
Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6
Maven: org.codehaus.jackson:jackson-core-asl:1.9.13
Maven: org.codehaus.jackson:jackson-mapper-asl:1.9.13


@RequestMapping("/view") @ResponseBody
public String[] view(){
String[] addr = new String[]{"郑州","洛阳","开封","北京"};
return addr;
}

["郑州","洛阳","开封","北京"]

@RequestMapping("/show") @ResponseBody
public List<String> show(){
List<String> addr = new ArrayList<String>();
addr.add("郑州1");
addr.add("郑州2");
addr.add("郑州3");
addr.add("郑州4");
return addr;
}

["郑州1","郑州2","郑州3","郑州4"]


@RequestMapping("/book") @ResponseBody
public Book showById(){
return new Book(100,"java项目开发",80d);
}

{"id":100,"name":"java项目开发","price":80.0}


如果项目中不想使用springmvc内置的json组件,我们可以使用fastjson组件
pom.xml 加入fastjson依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.33</version>
</dependency>

编写代码
@RequestMapping("/bk") @ResponseBody
public String showbk(){
Book book = new Book(100,"java项目开发",80d);
return JSON.toJSONString(book);
}

@RequestMapping("/sw") @ResponseBody
public String shows(){
List<String> addr = new ArrayList<String>();
addr.add("郑州1");
addr.add("郑州2");
addr.add("郑州3");
addr.add("郑州4");
return JSON.toJSONString(addr);
}

 


ajax 使用
index.jsp
------------------------------------------------------------------
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" pageEncoding="utf-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="js/j.js"></script>
<script>
function tt(){
//$('#ttt').load("timer.action");
$.ajax({
url:'timer.action',
type:'GET',
dataType:'text',
cache:false,
success:function(d){
$('#ttt').html(d);
}
});
}
$(function(){
tt();
setInterval(tt,1000);
});
</script>
</head>
<body>
<a href="user">user-ok</a>
<h3>springmvc - jquery - ajax - 时钟效果</h3>
<h3 id="ttt"></h3>
</body>
</html>


package com.fz.controller;
import com.alibaba.fastjson.JSON;
import com.fz.entity.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Created by webrx on 2017-06-27.
*/
@Controller
public class User {

//http://localhost/save
@RequestMapping(value = "save", method = RequestMethod.GET)
@ResponseBody
public void save() {
System.out.println("hello-save()");
}

//http://localhost/aa
//http://localhost/bb
//http://localhost/cc
@RequestMapping(value = {"aa", "bb", "cc"}, method = RequestMethod.GET)
public void show(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset-utf-8");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"utf-8\">");
out.println("<title>玉灵 QQ:7031633 Email:webrx@126.com</title>");
out.println("<meta name=\"keywords\" content=\"关键字\">");
out.println("<meta name=\"description\" content=\"简介\">");
out.println("</head>");
out.println("<body>");
out.println("<h3>Hello World 中文效果</h3>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}


//http://localhost/user
@RequestMapping("/user")
public String user(Model m) {
m.addAttribute("name", "张三丰");
m.addAttribute("age", 18);
m.addAttribute("address", new String[]{"郑州", "上海", "北京", "天津"});
return "user"; // /WEB-INF/template/user.jsp 视图渲染
}


//RESTFul 风格参数
//http://localhost/add/300/李四.action
//http://localhost/add/300/李四
@RequestMapping("/add/{id}/{name}")
@ResponseBody
public void add(HttpServletResponse resp, @PathVariable String name, @PathVariable int id) throws IOException {
//System.out.println(name);
//System.out.println(id);
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset-utf-8");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"utf-8\">");
out.println("<title>玉灵 QQ:7031633 Email:webrx@126.com</title>");
out.println("<meta name=\"keywords\" content=\"关键字\">");
out.println("<meta name=\"description\" content=\"简介\">");
out.println("</head>");
out.println("<body>");
out.println("<h3>" + name + "</h3>");
out.println("<h3>" + id + "</h3>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}

//http://localhost/insert.action?nn=jack
//http://localhost/insert

//http://localhost/insert.action?aa=38&nn=李四四
@RequestMapping(value="/insert",method = RequestMethod.GET)
public void insert(@RequestParam(value = "nn", required = false, defaultValue = "李四") String name, @RequestParam(value = "aa",required = false,defaultValue = "18") int age,HttpServletResponse resp) throws IOException {
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset-utf-8");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"utf-8\">");
out.println("<title>玉灵 QQ:7031633 Email:webrx@126.com</title>");
out.println("<meta name=\"keywords\" content=\"关键字\">");
out.println("<meta name=\"description\" content=\"简介\">");
out.println("</head>");
out.println("<body>");
out.println("<h3>"+ name +"</h3>");
out.println("<h3>"+ age +"</h3>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}


@RequestMapping("/view") @ResponseBody
public String[] view(){
String[] addr = new String[]{"郑州","洛阳","开封","北京"};
return addr;
}

@RequestMapping("/show") @ResponseBody
public List<String> show(){
List<String> addr = new ArrayList<String>();
addr.add("郑州1");
addr.add("郑州2");
addr.add("郑州3");
addr.add("郑州4");
return addr;
}


@RequestMapping("/book") @ResponseBody
public Book showById(){
return new Book(100,"java项目开发",80d);
}

@RequestMapping("/bk") @ResponseBody
public String showbk(){
Book book = new Book(100,"java项目开发",80d);
return JSON.toJSONString(book);
}

@RequestMapping("/sw") @ResponseBody
public String shows(){
List<String> addr = new ArrayList<String>();
addr.add("郑州1");
addr.add("郑州2");
addr.add("郑州3");
addr.add("郑州4");
return JSON.toJSONString(addr);
}

@RequestMapping("/timer.action") @ResponseBody
public String timer(){
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(d);
}

}

posted @ 2017-07-18 15:46  归然  阅读(187)  评论(0编辑  收藏  举报