05springmvc-json-ajax使用.txt spring mvc 框架内置json使用的是jackson组件pom.xml 加入如下依赖 org.codehaus.jackson jackson-mapper-asl 1.9.13 com.fasterxml.jackson.core jackson-databind 2.8.6 会自动引入如下jar文件Ma
spring mvc 框架内置json使用的是jackson组件 pom.xml 加入如下依赖org.codehaus.jackson jackson-mapper-asl1.9.13 会自动引入如下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 com.fasterxml.jackson.core jackson-databind2.8.6 show(){ List addr = new ArrayList (); 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依赖 编写代码 @RequestMapping("/bk") @ResponseBody public String showbk(){ Book book = new Book(100,"java项目开发",80d); return JSON.toJSONString(book); } @RequestMapping("/sw") @ResponseBody public String shows(){ List com.alibaba fastjson1.2.33 addr = new ArrayList (); 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" %> Document user-okspringmvc - jquery - ajax - 时钟效果
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(""); out.println(""); out.println(""); out.println(" "); out.println("玉灵 QQ:7031633 Email:webrx@126.com "); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println("Hello World 中文效果
"); out.println(""); out.println(""); 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(""); out.println(""); out.println(""); out.println(" "); out.println("玉灵 QQ:7031633 Email:webrx@126.com "); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println("" + name + "
"); out.println("" + id + "
"); out.println(""); out.println(""); 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(""); out.println(""); out.println(""); out.println(" "); out.println("玉灵 QQ:7031633 Email:webrx@126.com "); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(""+ name +"
"); out.println(""+ age +"
"); out.println(""); out.println(""); out.flush(); out.close(); } @RequestMapping("/view") @ResponseBody public String[] view(){ String[] addr = new String[]{"郑州","洛阳","开封","北京"}; return addr; } @RequestMapping("/show") @ResponseBody public Listshow(){ List addr = new ArrayList (); 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 addr = new ArrayList (); 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); } } 项目开发实际经验 注意细节
- 标题不能超过30个字符
- 多个关键字用,隔开