@SessionAttributes(value = {"zhangsan"})
数据存入request域中,要从request把值取出来拿到跳转的页面,相当于将msg=zhangsan存入session域中
(@RequestParam(name="name") String username)
页面要获取数据,必须名称统一 如果不同一,取不出来数据,可以通过@RequestParam注解将两个关联,从而取出数据
@RequestHeader(value="Accept") String header
取数数据的请求头,可以根据亲求头做出判断,运行什么浏览器访问之类的选址
@CookieValue(value="JSESSIONID") String cookieValue
可以从Cookie域中取出数据
@ModelAttribute
标注此注解的方法可以提前运行,比如可以先从数据库查出数据,当表单不完整的时候,可以使用数据库里面的数据
(Model model) (ModelMap modelMap) (SessionStatus status)
Model,ModelMap向request域中放入json数据,前端页面可以直接取出数据
status可以将域中数据清空,也就是删除数据 status.setComplete();
@Controller
@SessionAttributes(value = {"zhangsan"})
public class annoController {
@RequestMapping("/testRequestParam")
public String testRequestParam(@RequestParam(name="name") String username){
System.out.println("ok");
System.out.println(username);
return "success";
}
@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestBody String body){
System.out.println( "ok");
System.out.println(body);
return "success";
}
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable(name="id") String id){
System.out.println( "ok");
System.out.println(id);
return "success";
}
@RequestMapping("/testRequestHeader")
public String testRequestHeader(@RequestHeader(value="Accept") String header ){
System.out.println( "ok");
System.out.println(header);
return "success";
}
@RequestMapping("/testCookieValue ")
public String CookieValue(@CookieValue(value="JSESSIONID") String cookieValue ){
System.out.println( "ok");
System.out.println(cookieValue);
return "success";
}
@RequestMapping("/testModelAttribute")
public String testModelAttribute(@ModelAttribute("1") User user){
System.out.println( "ok");
System.out.println(user);
return "success";
//User{username='admin', age=19, date=null}
}
@ModelAttribute//用于表单不完整,使用数据库的数据
public void showUser(String username, Map<String,User> map) {
User user = new User();
user.setUsername(username);
user.setAge(20);
user.setDate(new Date());
map.put("1",user);
}
@ModelAttribute//用于表单不完整,使用数据库的数据
public User showUser(String username){
User user=new User();
user.setUsername(username);
user.setAge(20);
user.setDate(new Date());
return user;
}
@RequestMapping("/testSessionAttributes")
public String testSessionAttributes(Model model){
model.addAttribute("msg","perfect");
System.out.println( "ok");
return "success";
}
@RequestMapping("/getSessionAttributes")
public String getSessionAttributes(ModelMap modelMap){
String msg = (String) modelMap.get("msg");
System.out.println(msg);
System.out.println( "ok");
return "success";
}
@RequestMapping("/SessionStatus")
public String testSessionStatus(SessionStatus status){
status.setComplete();
System.out.println( "ok");
return "success";
}