RequestMapping
作用 表示请求的地址,可以用在类上也可以用在方法上 应用
@RequestMapping ( path = "/demo" )
public class HelloController {
@RequestMapping ( "/hello" )
public String sayHello ( ) {
System. out. println ( "大河之剑天上来!" ) ;
return "success" ;
}
}
< a href = " demo/hello" > Hello</ a>
其他属性 1.method = RequestMethod.GET ( 限制请求方式 ) 2.value = “/hello” ( 与path属性相同 ) 3.params = {“username=dudu”} ( 限制请求参数,只有参数符合条件才能正常请求 )
RequestParam
作用 处理请求参数与响应请求的函数的参数名不相同的问题 应用
@RequestMapping ( "/requestParam" )
public String testRequestParam ( @RequestParam ( name = "name" , required = false ) String username) {
System. out. println ( "用户名:" + username) ;
return "success" ;
}
< a href = " anno/requestParam?name=dudu" > RequestParam</ a>
其他属性 1. required = false ( 默认为true,表示该参数一定要传,否则无法请求 )
RequestBody
作用 用于获取请求体内容(get请求方式没有请求体,所以不适用) 应用
@RequestMapping ( "/requestBody" )
public String testRequestBody ( @RequestBody String body) {
System. out. println ( "请求体:" + body) ;
return "success" ;
}
< form action = " anno/requestBody" method = " post" >
姓名:< input type = " text" name = " name" /> < br>
年龄:< input type = " text" name = " age" /> < br>
< input type = " submit" value = " 提交" >
</ form>
PathVariable
作用 绑定url中的占位符,名称要与name属性值一致 应用
@RequestMapping ( "/pathVariable/{sid}" )
public String testPathVariable ( @PathVariable ( name = "sid" ) String id) {
System. out. println ( id) ;
return "success" ;
}
< a href = " anno/pathVariable/1208" > PathVariable</ a>
RequestHeader
@RequestMapping ( "/requestHeader" )
public String testRequestHeader ( @RequestHeader ( value = "Accept" ) String header) {
System. out. println ( header) ;
return "success" ;
}
< a href = " anno/requestHeader" > RequestHeader</ a>
CookieValue
@RequestMapping ( "/cookieValue" )
public String testCookieValue ( @CookieValue ( value = "JSESSIONID" ) String cookie) {
System. out. println ( cookie) ;
return "success" ;
}
< a href = " anno/cookieValue" > CookieValue</ a>
ModelAttribute
作用 用于方法上,该方法会先执行,后执行的方法可以接收该方法的返回值,测试的结果显示是按照类型来接收,与返回值名称无关 应用1:作用于方法上
@RequestMapping ( "/modelAttribute" )
public String testModelAttribute ( User user) {
System. out. println ( user) ;
return "success" ;
}
@ModelAttribute
public User show ( String name, Integer age) {
System. out. println ( "show_User方法执行了……" ) ;
User user = new User ( name, age, new Date ( ) ) ;
return user;
}
< form action = " anno/modelAttribute" method = " post" >
姓名:< input type = " text" name = " name" /> < br>
年龄:< input type = " text" name = " age" /> < br>
< input type = " submit" value = " 提交" >
</ form>
@RequestMapping ( "/modelAttribute" )
public String testModelAttribute ( @ModelAttribute ( "du" ) User user) {
System. out. println ( user) ;
return "success" ;
}
@ModelAttribute
public void show ( Map< String, User> map) {
System. out. println ( "show_void方法执行了……" ) ;
User user = new User ( ) ;
map. put ( "du" , user) ;
}
< form action = " anno/modelAttribute" method = " post" >
姓名:< input type = " text" name = " name" /> < br>
年龄:< input type = " text" name = " age" /> < br>
< input type = " submit" value = " 提交" >
</ form>
SessionAttributes
@SessionAttributes ( value = { "du" } )
public class AnnotationController implements Serializable {
@RequestMapping ( "/sessionAttributes" )
public String testSessionAttributes ( Model model) {
model. addAttribute ( "du" , "费渡" ) ;
return "success" ;
}
}
< a href = " anno/sessionAttributes" > SessionAttributes</ a>
ModelMap类
@RequestMapping ( "/getSessionAttributes" )
public String getSessionAttributes ( ModelMap modelMap) {
System. out. println ( modelMap. get ( "du" ) ) ;
return "success" ;
}
< a href = " anno/getSessionAttributes" > getSessionAttributes</ a>
SessionStatus类
@RequestMapping ( "/deleteSessionAttributes" )
public String deleteSessionAttributes ( SessionStatus status) {
status. setComplete ( ) ;
return "success" ;
}
< a href = " anno/deleteSessionAttributes" > deleteSessionAttributes</ a>