标签:标记 get请求 消息 通过 mil param efault 指定 msu
首先来看下一段代码:
@Controller @RequestMapping("/user") public class UsersController { @RequestMapping(value="/findAll") private String findAll(){ return "index"; } }
@Controller:表明这个类是一个控制器类。假设用这个注解。须要在spring-mvc配置文件加上这一段。<context:component-scan base-package="com.ztz.springmvc.controller"/>
@RequestMapping:能够为控制器指定处理能够请求哪些URL请求,@RequestMapping能够定义在类或方法上。
相对于类定义处的 URL。
若类定义处未标注 @RequestMapping。则方法处标记的 URL 相对于WEB 应用的根文件夹
@Controller @RequestMapping("/user") public class UsersController { @RequestMapping(value="/findAll",method=RequestMethod.GET) private String findAll(){ System.out.println("仅仅接受get请求"); return "index"; } }
@Controller @RequestMapping("/user") public class UsersController { @RequestMapping(value="/findAll",method=RequestMethod.POST) private String findAll(){ System.out.println("仅仅接受post请求"); return "index"; } }
@Controller @RequestMapping("/user") public class UsersController { @RequestMapping(value="/findAll",params="name") private String findAll(){ System.out.println("仅仅接受name參数"); return "index"; } }
@Controller @RequestMapping("/user") public class UsersController { @RequestMapping(value="/findAll",headers="Content-Type:text/html;charset=UTF-8") private String findAll(){ System.out.println("仅仅接受请求头中Content-Type为text/html;charset=UTF-8的请求"); return "index"; } }
@Controller @RequestMapping("/user") public class UsersController { @RequestMapping(value="/findAll") private String findAll(@RequestParam(value="name",required=true)String name,//參数name不能为空 @RequestParam(value="sex",required=false)String sex,//參数sex能够为空 @RequestParam(value="age",defaultValue="20")String age){//參数age假设为空,默认值为20 System.out.println(name); System.out.println(sex); System.out.println(age); return "index"; } }
@Controller @RequestMapping("/user") public class UsersController { @RequestMapping(value="/findAll") private String findAll(@RequestHeader(value="User-Agent")String user_Agent, @RequestHeader(value="Cookie")String cookie){ System.out.println(user_Agent); System.out.println(cookie); return "index"; } }
@Controller @RequestMapping("/user") public class UsersController { @RequestMapping(value="/findAll") private String findAll(@CookieValue(value="JSESSIONID")String cookie){ System.out.println(cookie); return "index"; } }
@Controller @RequestMapping("/user") public class UsersController { @RequestMapping(value="/findAll",method=RequestMethod.POST) private String findAll(@RequestBody(required=true) String body){ System.out.println(body); return "index"; } }
springmvc之@Controller、@RequestMapping等注解解说
标签:标记 get请求 消息 通过 mil param efault 指定 msu
原文地址:http://www.cnblogs.com/yangykaifa/p/6944219.html