标签:alert oca 注解 public 图片 map model 数据 post
@ResponseBody
@RequestMapping(value = "/addStudent",method = RequestMethod.GET)
public String addStudent(Student student){
if (ssi.addStudent(student)) {
return "<script>alert(‘添加成功‘);location.href=‘/sc/list‘</script>";
}
return "<script>alert(‘添加失败‘);location.href=‘addStudent‘</script>";
}
@ResponseBody
@RequestMapping(value = "/addStudent",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
public String addStudent(Student student){
if (ssi.addStudent(student)) {
return "<script>alert(‘添加成功‘);location.href=‘/sc/list‘</script>";
}
return "<script>alert(‘添加失败‘);location.href=‘addStudent‘</script>";
}
produces可能不算一个注解,因为什么呢,它是注解@requestMapping注解里面的属性项,
它的作用是指定返回值类型,不但可以设置返回值类型还可以设定返回值的字符编码;
还有一个属性与其对应,就是consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
他们的使用方法如下:
produces第一种使用,返回json数据,下边的代码可以省略produces属性,因为我们已经使用了注解@responseBody就是返回值是json数据:
@Controller
@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Pet getPet(@PathVariable String petId, Model model) {
// implementation omitted
}
produces第二种使用,返回json数据的字符编码为utf-8.:
@Controller
@RequestMapping(value = "/pets/{petId}", produces="MediaType.APPLICATION_JSON_VALUE"+";charset=utf-8")
@ResponseBody
public Pet getPet(@PathVariable String petId, Model model) {
// implementation omitted
}
@Controller
@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")
public void addPet(@RequestBody Pet pet, Model model) {
// implementation omitted
}
标签:alert oca 注解 public 图片 map model 数据 post
原文地址:https://www.cnblogs.com/userzf/p/13824972.html