(1)注解介绍
作用:把请求中的指定名称的参数传递给控制器中的形参赋值
属性
value:请求参数中的名称
required:请求参数中是否必须提供此参数,默认值是true,必须提供
defaultValue:默认值
(2)案例
创建RequestParamController类,并在类中添加add方法,代码如下:
@Controller
@RequestMapping(value = "/annotation")
public class RequestParamController {
@RequestMapping(value = "/request/param/add")
public String add(
@RequestParam(
value = "uname",
required = false,
defaultValue = "赵六") String name,
@RequestParam(value = "uage") Integer age){
System.out.println("name:"+name+",age:"+age);
return "success";
}
}
可以请求http://localhost:8080/annotation/request/param/add?uname=王五&uage=21测试
RequestBody注解(1)注解介绍
作用:用于获取请求体的内容(注意:get请求不可以) 属性 required:是否必须有请求体,默认值是true
(2)案例
创建RequestBodyController类,并在里面添加add方法,代码如下:
@Controller
@RequestMapping(value = "/annotation")
public class RequestBodyController {
@RequestMapping(value = "/request/body/add")
public String add(@RequestBody String body){
System.out.println("body:"+body);
return "success";
}
}
可以请求http://localhost:8080/annotation/request/body/add?uname=王五&uage=21测试
注意,这里提交方式用POST,不能用GET。
PathVariable注解(1)注解介绍
作用:拥有绑定url中的占位符的。例如:url中有/delete/{id},{id}就是占位符
属性
value:指定url中的占位符名称
(2)RESTful风格介绍
Restful风格的URL
请求路径一样,可以根据不同的请求方式去执行后台的不同方法
restful风格的URL优点
结构清晰
符合标准
易于理解
扩展方便
Restful风格建议
1)推荐使用通用协议 http https
2)请求路径尽量使用小写字母
3)url地址中尽量使用中划线代替下划线
4)推荐使用通用格式传输数据 json xml
5)一个路径代表一种资源
6)响应数据推荐使用拿来可用原则
(3)案例
例如
用户请求http://localhost:8080/annotation/pathVariable/user/1 以GET提交表示查询id=1的用户
用户请求http://localhost:8080/annotation/pathVariable/user/1 以DELETE提交表示删除id=1的用户
创建PathVariableController类,并在代码里添加2个方法,代码如下:
@Controller
@RequestMapping(value = "/annotation")
public class PathVariableController {
@RequestMapping(value = "/pathVariable/user/{id}",method = RequestMethod.GET)
public String add(@PathVariable(value = "id")Integer id){
System.out.println("查询ID="+id);
return "success";
}
@RequestMapping(value = "/pathVariable/user/{id}",method = RequestMethod.DELETE)
public String delete(@PathVariable(value = "id")Integer id){
System.out.println("删除ID="+id);
return "success";
}
}
注意:这里请求的时候会报错,主要是因为JSP只能识别get和post。不过请求会正常到达后台,jsp在未来已经淘汰。
测试
GET请求http://localhost:8080/annotation/pathVariable/user/1
POST请求http://localhost:8080/annotation/pathVariable/user/1
RequestHeader注解(1)注解介绍
作用:获取指定请求头的值
属性
value:请求头的名称
required:是否是必须头信息
defaultValue:默认值
(2)案例
创建RequestHeaderController类,代码如下:
@Controller
@RequestMapping(value = "/annotation")
public class RequestHeaderController {
@RequestMapping(path="/header/get")
public String header(@RequestHeader(value="Accept") String header) {
System.out.println(header);
return "success";
}
}
后台可以直接获取到Accept头文件数据。
cookievalue注解(1)注解介绍
作用:用于获取指定cookie的名称的值
属性
value:请求头的名称
required:是否是必须头信息
defaultValue:默认值
(2)案例
创建RequescookieController,代码如下:
@Controller
@RequestMapping(value = "/annotation")
public class RequescookieController {
@RequestMapping(path="/cookie/get")
public String header(@cookievalue(value="JSESSIONID") String cookievalue) {
System.out.println(cookievalue);
return "success";
}
}
后台会输出cookie中的JSESSIONID信息。
ModelAttribute注解
作用
出现在方法上:表示当前方法,会在控制器方法执行之前执行。
出现在参数上:获取指定的数据给参数赋值。
应用场景
当提交表单数据不是完整的实体数据时,保证没有提交的字段使用数据库原来的数据。
修饰的方法有返回值
在上面的案例基础之上,给User中添加一个sex属性,再到UserController中添加一个parameterUser()方法,并创建User再赋值返回,方法上加上注解@ModelAttribute注解。
@Controller
@RequestMapping(value = "/user")
public class UserController {
@ModelAttribute//理解为像以前的@Bean
public User parameterUser(){
User user = new User();
user.setSex("男");
return user;
}
@RequestMapping(value = "/add")
public String addUser(User user){
System.out.println("用户" + user.getName() +",性别:"+user.getSex()+","+ "今年" + user.getAge() + "岁,住在" + user.getIdCard().getAddress() + ",身份证号是" + user.getIdCard().getNumber());
for (Mobile mobile : user.getMobiles()) {
System.out.println(mobile.getMobileName()+"花了"+mobile.getPrice());
}
return "success";
}
}
修饰的方法没有返回值
在上面案例基础上,把parameterUser的返回值去掉,增加一个Map来存储数据,存储的key为user,在addUser中使用@ModelAttribute("user")User user获取在parameterUser()方法中赋值的数据。程序运行后,我们发现addUser方法的user参数能取到parameterUser方法赋值的数据。
@Controller
@RequestMapping(value = "/user")
public class UserController {
@ModelAttribute
public void parameterUser(Map userMap){
User user = new User();
user.setSex("男");
userMap.put("user",user);//1键是user
}
@RequestMapping(value = "/add")
public String addUser(@ModelAttribute("user")User user){//2根据键取值,理解为一个集合map
System.out.println("用户" + user.getName() +",性别:"+user.getSex()+","+ "今年" + user.getAge() + "岁,住在" + user.getIdCard().getAddress() + ",身份证号是" + user.getIdCard().getNumber());
for (Mobile mobile : user.getMobiles()) {
System.out.println(mobile.getMobileName()+"花了"+mobile.getPrice());
}
return "success";
}
}
SessionAttributes注解,Model存,ModelMap取,Session状态完成清除
(1)注解介绍
作用:用于多次执行控制器方法,之间的参数共享
属性
value:指定存入属性的名称
(2)案例
@Controller
@RequestMapping(path="/user")
@SessionAttributes(value= {"username","password","age"},types= {String.class,Integer.class}) // 把数据存入到session域对象中
public class HelloController2 {
@RequestMapping(path="/save")
public String save(Model model) {
System.out.println("向session域中保存数据");
model.addAttribute("username", "root");
model.addAttribute("password", "123");
model.addAttribute("age", 20);
return "success";
}
@RequestMapping(path="/find")
public String find(ModelMap modelMap) {
String username = (String) modelMap.get("username");
String password = (String) modelMap.get("password");
Integer age = (Integer) modelMap.get("age");
System.out.println(username + " : "+password +" : "+age);
return "success";
}
@RequestMapping(path="/delete")
public String delete(SessionStatus status) {
status.setComplete();
return "success";
}
}



