@Controller
@Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否使用了@RequestMapping 注解。@Controller 只是定义了一个控制器类,而使用@RequestMapping 注解的方法才是真正处理请求的处理器。
@RequestMapping
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
RequestMapping注解有六个属性:
1、 value
value:指定请求的实际地址;
2、method;
method: 指定请求的method类型, GET、POST、PUT、DELETE等,下面例子的@PathVariable后面讲解:
/**
* Rest 风格的 URL(以 CRUD 为例):
* 新增:/order POST
* 修改:/order/1 PUT
* 获取:/order/1 GET
* 删除:/order/1 DELETE
* @param id
* @return
*/
@RequestMapping(value = "/testRestPut/{id}", method = RequestMethod.PUT)
public String testRestPut(@PathVariable int id) {
System.out.println("testRestPut:" + id);
return SUCCESS;
}
@RequestMapping(value = "/testRestDelete/{id}", method = RequestMethod.DELETE)
public String testRestDelete(@PathVariable int id) {
System.out.println("testRestDelete:" + id);
return SUCCESS;
}
@RequestMapping(value = "/testRestPost/{id}", method = RequestMethod.POST)
public String testRestPost(@PathVariable int id) {
System.out.println("testRestPost:" + id);
return SUCCESS;
}
@RequestMapping("/testRestGet")
public String testRestGet() {
System.out.println("testRestGet");
return SUCCESS;
}
3、consumes
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
4、produces
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
5、params
params: 指定request中必须包含某些参数值是,才让该方法处理。
6、headers
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
@RequestMapping(“/helloword/?/aa”) 的 Ant 路径,匹配符:
?:匹配文件名的一个字符
*:匹配文件名的所有字符
**:匹配多层路径
@RequestMapping(“/testPojo”) POJO类用法:
@RequestMapping("/testPojo")
public String testPojo(User user) {
System.out.println("testPojo:" + user);
return "success";
}
@RequestMapping(“/testPojo”) Map用法:
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map) {
map.put("names", Arrays.asList("Tomcat", "Eclipse", "JavaEE"));
return "success";
}
@RequestMapping(“/testPojo”) ModelAndView用法:
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
String viewName = SUCCESS;
ModelAndView modelAndView = new ModelAndView(viewName);
modelAndView.addObject("time", new Date());
return modelAndView;
}
@PathVariable
用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数
@Controller
public class TestController {
@RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET)
public String getLogin(@PathVariable("userId") String userId,
@PathVariable("roleId") String roleId){
System.out.println("User Id : " + userId);
System.out.println("Role Id : " + roleId);
return "hello";
}
@RequestMapping(value="/product/{productId}",method = RequestMethod.GET)
public String getProduct(@PathVariable("productId") String productId){
System.out.println("Product Id : " + productId);
return "hello";
}
@RequestMapping(value="/javabeat/{regexp1:[a-z-]+}",
method = RequestMethod.GET)
public String getRegExp(@PathVariable("regexp1") String regexp1){
System.out.println("URI Part 1 : " + regexp1);
return "hello";
}
}
@RequestParam
@RequestParam用于将请求参数区数据映射到功能处理方法的参数上,用例:
/**
* @RequestParam("id") 带参映射
* @param id
* @return
*/
@RequestMapping("/testRequestParam")
public String testRequestParam(@RequestParam("id") int id) {
System.out.println("testRequestParam " + id);
return "success";
}
@ResponseBody详解
@ResponseBody的作用其实是将java对象转为json格式的数据。
@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据。
注意:在使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。
@ResponseBody是作用在方法上的,@ResponseBody 表示该方法的返回结果直接写入 HTTP response body 中,一般在异步获取数据时使用【也就是AJAX】。
注意:在使用 @RequestMapping后,返回值通常解析为跳转路径,但是加上 @ResponseBody 后返回结果不会被解析为跳转路径,而是直接写入 HTTP response body 中。 比如异步获取 json 数据,加上 @ResponseBody 后,会直接返回 json 数据。@RequestBody 将 HTTP 请求正文插入方法中,使用适合的 HttpMessageConverter 将请求体写入某个对象。
后台 Controller类中对应的方法:
@RequestMapping("/login.do")
@ResponseBody
public Object login(String name, String password, HttpSession session) {
user = userService.checkLogin(name, password);
session.setAttribute("user", user);
return new JsonResult(user);
}
@RequestBody是作用在形参列表上,用于将前台发送过来固定格式的数据【xml格式 或者 json等】封装为对应的 JavaBean 对象,
封装时使用到的一个对象是系统默认配置的 HttpMessageConverter进行解析,然后封装到形参上。
如上面的登录后台代码可以改为:
@RequestMapping("/login.do")
@ResponseBody
public Object login(@RequestBody User loginUuser, HttpSession session) {
user = userService.checkLogin(loginUser);
session.setAttribute("user", user);
return new JsonResult(user);
}
一般用@Controller注解,也可以使用@RestController,@RestController注解相当于@ResponseBody + @Controller,表示是表现层,除此之外,一般不用别的注解代替。