Spring MVC 学习笔记 第二章 Spring MVC注解
常用注解:
注解 | 说明 |
---|---|
@RequestMapping | Spring MVC通过@RequestMapping注解将URL请求与业务方法进行映射,在Handle的类定义处以及方法定义处都可以添加 |
@RequestMapping | 在类定义处添加,相当于客户端多了一层访问路径。 |
@Controller | 在类的定义处添加,将该类交给IoC容器管理,同时使其成为一个控制器,可以接受客户端请求。 |
代码实例:
package com.fw.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloHandler {
@RequestMapping("/index")
public String index(){
System.out.println("执行了index。。。");
return "index";
}
}
访问时需要通过以下URL:
http://localhost:8080/hello/index
@RequestMapping相关参数
1.value:指定URL请求的实际地址,是@RequestMapping的默认值。
注:以下两种方式是相同的。
@RequestMapping("/index")
@RequestMapping(value="/index")
2.method:指定请求的method类型,GET,POST,PUT,DELETE
@RequestMapping(value = "/index",method = RequestMethod.GET)
表示只能接受GET请求。
不添加的情况下,表示没有限制。
3.params:指定请求中必须包含默认参数,否则无法调用该方法。
@RequestMapping(value = "/index",method = RequestMethod.GET,params = {"id","name"})
public String index(){
System.out.println("执行了index。。。");
return "index";
}
上述代码表示请求中必须包含指定参数id和name。
通过以下URL访问时会出现错误:
http://localhost:8080/hello/index
错误信息如下:
HTTP Status 400 – 错误的请求
Type Status Report
消息 Parameter conditions "id, name" not met for actual request parameters:
正确访问方式:
需要在URL里添加参数才可以正常访问:
http://localhost:8080/hello/index?id=10&name=aa
另外,也可以给参数指定值,当参数为指定值时才可以正常访问,否则会报错。
params = {"id=10","name"}
①关于参数取得:
package com.fw.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/hello")
public class HelloHandler {
@RequestMapping(value = "/index",method = RequestMethod.GET,params = {"id","name"})
public String index(int id,String name){
System.out.println("id:" + id);
System.out.println("name:" + name);
System.out.println("执行了index。。。");
return "index";
}
}
@RequestMapping 中的params形参名和index方法的形参名必须是一致的,才能够成功取得。
访问URL:
http://localhost:8080/hello/index?id=111&name=tom
运行结果:
id:111
name:tom
执行了index。。。
②参数绑定:
当形参名不一致的情况下,在形参列表中通过添加@RequestParam注解完成HTTP请求参数与业务方法形参的映射。
@RequestMapping(value = "/index",method = RequestMethod.GET,params = {"id","name"})
public String index(@RequestParam("id") int i, @RequestParam("name") String str){
System.out.println("i:" + i);
System.out.println("str:" + str);
System.out.println("执行了index。。。");
return "index";
}
访问URL:
http://localhost:8080/hello/index?id=222&name=Mike
运行结果:
i:222
str:Mike
执行了index。。。
上述代码表示将请求的参数id和name分别赋给形参i和str,同时自动完成了数据类型转换,将“222”转换成了int类型的222,再赋给i,这些工作是由HandlerAdapter来完成的。
③Spring MVC也支持RESTful风格的URL:
传统类型: http://localhost:8080/hello/index?id=222&name=Mike
REST: http://localhost:8080/hello/index/222/Mike
RESTful实例:
@RequestMapping("/rest/{id}/{name}")
public String rest(@PathVariable("id") int id, @PathVariable("name") String name){
System.out.println("id:" + id);
System.out.println("name:" + name);
System.out.println("执行了index。。。");
return "index";
}
通过@PathVariable注解完成请求参数与形参的映射。