SpringMVC——笔记
@RequestMapping("/delete/{id}") public String delete(@PathVariable("id" ) Integer id ){ UserDao.delete(id); return "redirect:/user/list.action"; }
@RequestMapping("/handle5") public String handle5(@RequestParam(value="userName",required=false) String usernName,@RequestParam("age") int age){ return "success"; }
@RequestMapping("/handle7") public String handle7(@RequestHeader("Accept-Encoding") String encoding, @RequestHeader("Keep-Alive")long keppAlieve){return "success";}
@RequestParam("/headle6") public String handle6(@CookieValue(value="sessionId",required=false) String sessionId,@RequestParam("age") int age){ return "seccess"; }
// @SessionAttributes(value={"user"},types={String.class}) @RequestMapping("/springmvc") @Controller public class SpringMVCTest { private static final String SUCCESS = "success"; @ModelAttribute public void getUser(@RequestParam(value="id",required= false) Integer id,Map<String,Object> map){ if(id != null){ // 模拟从数据库中获取对象 User user = new User(1,"Tom","123456","tom@atguigu.com",12); System.out.println("从数据库中获取一个对象:"+ user); map.put("user",user); } } @RequestMapping("/testModelAttribute") public String testModelAttribute(User user){ System.out.println("修改:" + user); return SUCCESS; }
/*注意这个注解只能放在类的上面,不能放在方法上面*/
@RequestMapping("/testSessionAttributes") public String testSessionAttributes(Map<String,Object> map){ User user = new User(1,"Tom", "123456","tom@123.com",15); map.put("user",user); map.put("school","atguigu"); return SUCCESS; }
/*
* 目标方法可以添加 Map 类型的(实际上也可以Model 类型的 或ModeMap 类型的)参数
* */
@RequestMapping("/testMap") public String testMap(Map<String,Object> map){ System.out.println(map.getClass().getName()); map.put("names",Arrays.asList("Tom","Jerry","Mike")); return SUCCESS; }
/*
* 返回值可是ModelAndView 类型
* 其中可以包含视图和模型信息
* SpringMVC 会吧ModelAndView 的model 数据放到request 域中。
* */
@RequestMapping("/testModelAndView") public ModelAndView testModelAndView(){ String viewName = SUCCESS; ModelAndView modelAndView = new ModelAndView(viewName); // 添加模型到ModelAndView 中 modelAndView.addObject("time",new Date()); return modelAndView; } @RequestMapping("/testServletAPI") public String testServletAPI(HttpServletRequest request,HttpServletResponse response) { System.out.println("testServletAPI,"+request+","+ response); return SUCCESS; }
/*
* Spring MVC 会按请求参数名和POJO 属性进行资助匹配
* 自动为该对象填充属性值,支持级联动,如 dept.deptId,dept.address.tel 等
* */
@RequestMapping("/testPojo") public String testPojo(User user){ System.out.println("testPojo" + user); return SUCCESS; } /*@CookieValue: 映射一个Cookie 值,属性同@RequestParam*/ @RequestMapping("/testCoookieValue") public String testCoookieValue(@CookieValue("JSESSIONID")String sessionId){ System.out.println("CoookieValue:"+ sessionId); return SUCCESS; }
/*
* 映射请求头信息
* 用法同@RequestParam
* */
@RequestMapping("/testRequestHander") public String testRequestHander(@RequestHeader(value="Accept-Language") String al){ System.out.println("testRequestHander,Accept-Language:"+ al); return SUCCESS; }
/*
* @RequestParam 来映射请求参数
* */
@RequestMapping(value="/testRequestParam") public String testRequestParam(@RequestParam(value="username") String un,@RequestParam(value="age",required=false,defaultValue="0") int age){ System.out.println("testRequestParam,username:"+ un + age); return SUCCESS; }
/*
* Rest 风格的URL
* 以CRUD 为例:
* 新增: /order POST
* 修改: /order/1 PUT update?id=1
* 获取: /order/1 GET get?id=1
* 删除: /order/1 DELETE delete?id=1
*
* 如何发送PUT 请求和DELETE 请求呢?
* 1.需要配置 HiddenHttpMethodFilter
* 2.需要发送POST请求
* 3.需要在发送POST 请求时携带一个name="_method"值的隐藏域,值为 DELETE 或PUT
*
* */
@RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT) public String testRestPut(@PathVariable Integer id) { System.out.println("testRest PUT:" + id ); return SUCCESS; } @RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE) public String testRestDelete(@PathVariable Integer id) { System.out.println("testRest DELETE:" + id ); return SUCCESS; } @RequestMapping(value="/testRest",method=RequestMethod.POST) public String testRest() { System.out.println("testRest POST" ); return SUCCESS; } @RequestMapping(value="/testRest/{id}",method=RequestMethod.GET) public String testRest(@PathVariable Integer id) { System.out.println("testRest GET:" + id ); return SUCCESS; }
/*
* @PathVariable 可以来映射URL 中的站位符到目标方法的参数中
*
* */
@RequestMapping("testPathVariable/{id}") public String testPathVariable(@PathVariable("id") Integer id) { System.out.println("testPathVariable:" + id); return SUCCESS; } @RequestMapping("testAntPath/*/abc") public String testAntPath() { System.out.println("testAntPath"); return SUCCESS; }
/*
* 了解: 可以使用 params 和headers 来更加精确的映射请求,params 和headers 支持简单的表达式,
* */
@RequestMapping(value="testParamsAndHeaders",params={"username","age!=10"},headers={"Accept-Language=en-US,zh;q=0.8"}) public String testParamsAndHeaders(){ System.out.println("testParamsAndHeaders"); return SUCCESS; }
/*
*
* 使用method 属性来指定请求方式
*
*
* */
@RequestMapping(value="/testMethod",method=RequestMethod.POST) public String testMethod() { System.out.println("testMethod"); return SUCCESS; }
/*
*
*1. @RequestMapping 除了修饰方法还可以修饰类
*2.
*
* */
@RequestMapping("/testRequestMapping") public String testRequestMapping(){ System.out.println("testRequestMapping"); return SUCCESS; } }