1ResponseBody 和 RequestBody
1简介
@ResponseBody把后台pojo转换为json对象,返回到页面
@RequestBody接受前台json数据,把json数据自动封装javaBean
2案例
导包
添加json转换器
写一个jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>注册 <%--name = oyb & sex = male = { "name":"oyb","sex":"male"}--%>
写一个StudentController
package oyb.Controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import oyb.model.Student;@Controller@RequestMapping("/stu")public class StudentController { @RequestMapping("/toReg") public String toReg( Student stu){ System.out.println(stu); return "stu/register"; } @RequestMapping("/save") /** * @RequestBody:把json数据转成模型对象 *@ResponseBody 返回json数据,把对象转成json字符串返回客户端 */ public @ResponseBody Student save(@RequestBody Student stu){ System.out.println(stu); return stu; }}
测试