springmvc接收浏览器ajax的请求并以json的形式发送数据给浏览器(关于 @ResponseBody的使用)

ajax查询过程
index.jsp页面直接发送ajax请求进行员工分页数据的查询
服务器将查出的数据,以json字符串的形式返回给浏览器
浏览器收到js字符串。可以使用js对json进行解析,使用js通过dom增删改改变页面。
返回json。实现客户端的无关性

<!-- 返回json字符串的支持 -->
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.8</version>
    </dependency>

对比一下代码

/**
 * 处理员工的crud请求
 * @author liantao.me
 *
 */
@Controller
public class EmployeeController {
    
    @Autowired
    EmployeeService employeeService;
    
//使用json
    @RequestMapping("/emps")
    @ResponseBody
    public Msg getEmpsWithJson(@RequestParam(value="pn",defaultValue="1")Integer pn){
        //引入PageHelper分页插件
        //查询之前需要调用,,传入页码,以及每页的大小
        PageHelper.startPage(pn,5);
        //startPage后面紧跟的是这个查询就是一个分页查询
        List<Employee> emps = employeeService.getAll();
        //使用pageInfo包装查询后的结果,只需要将Pageinfo交给页面就行了
        //封装了详细的分页信息,包括我们查出来的数据,传入连续显示的数据
        PageInfo page = new PageInfo(emps,5);
        
        return Msg.success().add("pageInfo",page);
        
    }
    

//不使用json
    //@RequestMapping("/emps")
    public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
        
        //引入PageHelper分页插件
        //查询之前需要调用,,传入页码,以及每页的大小
        PageHelper.startPage(pn,5);
        //startPage后面紧跟的是这个查询就是一个分页查询
        List<Employee> emps = employeeService.getAll();
        //使用pageInfo包装查询后的结果,只需要将Pageinfo交给页面就行了
        //封装了详细的分页信息,包括我们查出来的数据,传入连续显示的数据
        PageInfo page = new PageInfo(emps,5);
        model.addAttribute("pageInfo",page);
        return "list";
    }
   
}

domain中添加 msg


import java.util.HashMap;
import java.util.Map;

public class Msg {
    
    //状态码  100=success  200=fail
    private int code;
    //提示信息
    private String msg;
    
    //用户要返回给浏览器的数据
    private Map<String, Object> extend = new HashMap();

    public static Msg success(){
        Msg result = new Msg();
        result.setCode(100);
        result.setMsg("处理成功");
        return result;
    }
    
    public static Msg fail(){
        Msg result = new Msg();
        result.setCode(200);
        result.setMsg("处理失败");
        return result;
    }
    
    public Msg add(String key,Object value){
        this.getExtend().put(key, value);
        return this;
    }
    
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Map<String, Object> getExtend() {
        return extend;
    }

    public void setExtend(Map<String, Object> extend) {
        this.extend = extend;
    }
    
    
}

在这里插入图片描述
以上转载至
https://www.cnblogs.com/famine/p/9922842.html

@ResponseBody的作用其实是将java对象转为json格式的数据。
用了responsebody后就不会走视图解析器

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值