三、不常用的注解
1.RequestParam注解
1. 作用:把请求中的指定名称的参数传递给控制器中的形参赋值
2. 属性
1. value:请求参数中的名称
2. required:请求参数中是否必须提供此参数,默认值是true,必须提供
3. 代码如下
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/dept")
public class DeptController {
@RequestMapping("/save")
public String save(@RequestParam(value = "username",required = false) String name){
System.out.println(name);
return "suc";
}
}
2. RequestBody注解
1. 作用:用于获取请求体的内容(注意:get方法不可以)
2. 属性
1. required:是否必须有请求体,默认值是true
3. 代码如下
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.RequestParam;
@Controller
@RequestMapping("/dept")
public class DeptController {
@RequestMapping("/save")
public String save(@RequestParam(value = "username",required = false) String name){
System.out.println(name);
return "suc";
}
@RequestMapping("/save2")
public String save2(@RequestBody String body){
System.out.println(body);
return "suc";
}
}