@RequestMapping
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
@RequestMapping匹配符
– ?:匹配文件名中的一个字符
– *:匹配文件名中的任意字符
– : 匹配多层路径
实例:
URL : /user/*/create
– /user/bigsea/create 、 /user/sea/create 等URL
URL : /user/**/create
– /user/big/sea/create 、 /user/sea/big/create 等URL
URL : /user/create??
– /user/createaa 、/user/createbb
@RequestParam 绑定请求参数
– value:参数名
– required:是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常
在spring MVC中,两者的作用都是将request里的参数的值绑定到contorl里的方法参数里的,区别在于,URL写法不同。
使用@RequestParam时,URL是这样的:http://host:8080/path?参数名=参数值
使用@PathVariable时,URL是这样的:http://host:8080/path/参数值
拦截请求
@Controller
@RequestMapping(“/student”)
public class StudentController {
/**
* 配置@RequestMapping 拦截 localhost:8080/student/hello 请求
* @return
*/
@RequestMapping(“/hello”)
public String helloWorld(@RequestParam(“id”) int id){
id=3;
System.out.println(id);
return “hello”;
}
访问路径
http://localhost:8080/student/hello?id=3
但是类似的路径都会拦截,只不过控制台输出的id的值不变,为3
http://localhost:8080/student/hello?id=888
控制台输出3
@RequestMapping("/hello/{id}")
public String helloWorld(@PathVariable("id") int id){
id=5;
System.out.println(id);
return "hello";
访问路径
http://localhost:8080/student/hello/5
类似路径,同上
}
既可以通过这中类似的路径访问,又可以获取这个路径里的参数的值。
/**
* 配置@RequestMapping 拦截 localhost:8080/student/hello 请求
* @return
*/
@RequestMapping(“/hello”)
public String testRequestParam(@RequestParam(“id”) int id){
System.out.println(id);
return “hello”;
}
@RequestMapping(“/hello/{id}”)
public String testPathVariable(@PathVariable(“id”) String id){
System.out.println(id);
return “hello”;
}
分别访问
http://localhost:8080/student/hello?id=3
http://localhost:8080/student/hello/5
控制台输出:
3
5
@RequestMapping(“/hello/{id}/{age}/{tel}”)
public String testPathVariable(@PathVariable(“id”) String id, @PathVariable(“age”) int age, @PathVariable(“tel”)
String tel){
System.out.println(“id:”+id+” age:”+age+” tel:”+tel);
return “hello”;
}
}
可以拦截类似 http://localhost:8080/student/hello/4/78/000 这样的请求,控制台输出
id:4 age:78 tel:000
@RequestMapping(“/hello/{id}”)
public String testRequestParam(@PathVariable(“id”) int id, @RequestParam(“age”) int age){
System.out.println(“id:”+id+” age:”+age);
return “hello”;
}
可以拦截类似 http://localhost:8080/student/hello/6?age=22 这样的请求,控制台输出
id:6 age:22
@CookieValue 获取 cookie值
/**
* 使用@CookieValue 绑定cookie值
* 注解@CookieValue 也有 value ,required ,defaultValue 三个参数
* @param session
* @return
*/
@RequestMapping("/testcookie")
public String testCookieValue(@CookieValue("JSESSIONID") String session){
System.out.println(session);
return "hello";
}
@RequestHeader 获取请求头
/**
* 获取请求头中的信息
* @RequestHeader 也有 value ,required ,defaultValue 三个参数
* @param userAgent
* @param cookie
* @return
*/
@RequestMapping("/testrequestHeader")
public String requestHeader(@RequestHeader("Host") String host,
@RequestHeader("User-Agent") String userAgent,
@RequestHeader("Accept") String accept,
@RequestHeader("Accept-Language") String acceptLanguage,
@RequestHeader("Accept-Encoding") String acceptEncoding,
@RequestHeader("Cookie") String cookie,
@RequestHeader("Connection") String conn,
@CookieValue("JSESSIONID") String JSESSIONID){
System.out.println("Host:["+host+"]");
System.out.println("userAgent:["+userAgent+"]");
System.out.println("Accept:["+accept+"]");
System.out.println("Accept-Encoding:["+acceptEncoding+"]");
System.out.println("Connection:["+conn+"]");
System.out.println("cookie:["+cookie+"]");
System.out.println("JSESSIONID:["+JSESSIONID+"]");
return "hello";
}
访问地址:http://localhost:8080/student/testrequestHeader
控制台输出:
Host:[localhost:8080]
userAgent:[Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36]
Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8]
Accept-Encoding:[gzip, deflate, br]
Connection:[keep-alive]
cookie:[JSESSIONID=104EFE351A13F8E1AF99AAAFF76C5B42]
JSESSIONID:[104EFE351A13F8E1AF99AAAFF76C5B42]
一、@ModelAttribute
在默认情况下,ModelMap 中的属性作用域是 request 级别是,也就是说,当本次请求结束后,ModelMap 中的属性将销毁。如果希望在多个请求中共享 ModelMap 中的属性,必须将其属性转存到 session 中,这样 ModelMap 的属性才可以被跨请求访问。
spring 允许我们有选择地指定 ModelMap 中的哪些属性需要转存到 session 中,以便下一个请求属对应的 ModelMap 的属性列表中还能访问到这些属性。这一功能是通过类定义处标注 @SessionAttributes 注解来实现的。
使模型对象的特定属性具有 Session 范围的作用域
@Controller
@RequestMapping(“/bbtForum.do”)
@SessionAttributes(“currUser”) //①将ModelMap中属性名为currUser的属性
//放到Session属性列表中,以便这个属性可以跨请求访问
public class BbtForumController {
…
@RequestMapping(params = “method=listBoardTopic”)
public String listBoardTopic(@RequestParam(“id”)int topicId, User user,
ModelMap model) {
bbtForumService.getBoardTopics(topicId);
System.out.println(“topicId:” + topicId);
System.out.println(“user:” + user);
model.addAttribute(“currUser”,user); //②向ModelMap中添加一个属性
return “listTopic”;
}
}
我们在 ② 处添加了一个 ModelMap 属性,其属性名为 currUser,而 ① 处通过 @SessionAttributes 注解将 ModelMap 中名为 currUser 的属性放置到 Session 中,所以我们不但可以在 listBoardTopic() 请求所对应的 JSP 视图页面中通过 request.getAttribute(“currUser”) 和 session.getAttribute(“currUser”) 获取 user 对象,还可以在下一个请求所对应的 JSP 视图页面中通过 session.getAttribute(“currUser”) 或 ModelMap#get(“currUser”) 访问到这个属性。
这里我们仅将一个 ModelMap 的属性放入 Session 中,其实 @SessionAttributes 允许指定多个属性。你可以通过字符串数组的方式指定多个属性,如 @SessionAttributes({“attr1”,”attr2”})。此外,@SessionAttributes 还可以通过属性类型指定要 session 化的 ModelMap 属性,如 @SessionAttributes(types = User.class),当然也可以指定多个类,如 @SessionAttributes(types = {User.class,Dept.class}),还可以联合使用属性名和属性类型指定:@SessionAttributes(types = {User.class,Dept.class},value={“attr1”,”attr2”})。
二、@ModelAttribute
我们可以在需要访问 Session 属性的 controller 上加上 @SessionAttributes,然后在 action 需要的 User 参数上加上 @ModelAttribute,并保证两者的属性名称一致。SpringMVC 就会自动将 @SessionAttributes 定义的属性注入到 ModelMap 对象,在 setup action 的参数列表时,去 ModelMap 中取到这样的对象,再添加到参数列表。只要我们不去调用 SessionStatus 的 setComplete() 方法,这个对象就会一直保留在 Session 中,从而实现 Session 信息的共享。
@Controller
@SessionAttributes(“currentUser”)
public class GreetingController{
@RequestMapping
public void hello(@ModelAttribute(“currentUser”) User user){
//user.sayHello()
}
}
@responseBody注解的使用
1、
@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML
数据,需要注意的呢,在使用此注解之后不会再走试图处理器,而是直接将数据写入到输入流中,效果等同于通过response对象输出指定格式的数据。
2、
@RequestMapping(“/login”)
@ResponseBody
public User login(User user){
return user;
}
User字段:userName pwd
那么在前台接收到的数据为:’{“userName”:”xxx”,”pwd”:”xxx”}’
效果等同于如下代码:
@RequestMapping(“/login”)
public void login(User user, HttpServletResponse response){
response.getWriter.write(JSONObject.fromObject(user).toString());
}