Spring @PathVariable

来源:互联网 发布:html表白源码 编辑:程序博客网 时间:2024/05/18 09:41
1、 @PathVariable 当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 
这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。示例代码:@Controller  @RequestMapping("/owners/{ownerId}")  public class RelativePathUriTemplateController {      @RequestMapping("/pets/{petId}")    public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {          // implementation omitted     }  }  上面代码把URI template 中变量 ownerId的值和petId的值,绑定到方法的参数上。若方法参数名称和需要绑定的uri template中变量名称不一致,需要在@PathVariable("name")指定uri template中的名称。
Java代码  收藏代码
  1. @Controller  
  2. public class PersonController {  
  3.   
  4.     /** 
  5.      * 查询个人信息 
  6.      *  
  7.      * @param id 
  8.      * @return 
  9.      */  
  10.     @RequestMapping(value = "/person/profile/{id}/{name}/{status}", method = RequestMethod.GET)  
  11.     public @ResponseBody  
  12.     Person porfile(@PathVariable int id, @PathVariable String name,  
  13.             @PathVariable boolean status) {  
  14.         return new Person(id, name, status);  
  15.     }  
  16.   
  17.     /** 
  18.      * 登录 
  19.      *  
  20.      * @param person 
  21.      * @return 
  22.      */  
  23.     @RequestMapping(value = "/person/login", method = RequestMethod.POST)  
  24.     public @ResponseBody  
  25.     Person login(@RequestBody Person person) {  
  26.         return person;  
  27.     }  
  28. }  


备注:@RequestMapping(value = "/person/profile/{id}/{name}/{status}", method = RequestMethod.GET)中的{id}/{name}/{status}@PathVariable int id, @PathVariable String name,@PathVariable boolean status一一对应,按名匹配。 这是restful式风格。 
如果映射名称有所不一,可以参考如下方式: 

Java代码  收藏代码
  1. @RequestMapping(value = "/person/profile/{id}", method = RequestMethod.GET)  
  2. public @ResponseBody  
  3. Person porfile(@PathVariable("id"int uid) {  
  4.     return new Person(uid, name, status);  
  5. }  


  • GET模式下,这里使用了@PathVariable绑定输入参数,非常适合Restful风格。因为隐藏了参数与路径的关系,可以提升网站的安全性,静态化页面,降低恶意攻击风险。
  • POST模式下,使用@RequestBody绑定请求对象,Spring会帮你进行协议转换,将Json、Xml协议转换成你需要的对象。
  • @ResponseBody可以标注任何对象,由Srping完成对象——协议的转换。
  • 做个页面测试下: 
  • Js代码  收藏代码
    1. $(document).ready(function() {  
    2.     $("#profile").click(function() {  
    3.         profile();  
    4.     });  
    5.     $("#login").click(function() {  
    6.         login();  
    7.     });  
    8. });  
    9. function profile() {  
    10.     var url = 'http://localhost:8080/spring-json/json/person/profile/';  
    11.     var query = $('#id').val() + '/' + $('#name').val() + '/'  
    12.             + $('#status').val();  
    13.     url += query;  
    14.     alert(url);  
    15.     $.get(url, function(data) {  
    16.         alert("id: " + data.id + "\nname: " + data.name + "\nstatus: "  
    17.                 + data.status);  
    18.     });  
    19. }  
    20. function login() {  
    21.     var mydata = '{"name":"' + $('#name').val() + '","id":"'  
    22.             + $('#id').val() + '","status":"' + $('#status').val() + '"}';  
    23.     alert(mydata);  
    24.     $.ajax({  
    25.         type : 'POST',  
    26.         contentType : 'application/json',  
    27.         url : 'http://localhost:8080/spring-json/json/person/login',  
    28.         processData : false,  
    29.         dataType : 'json',  
    30.         data : mydata,  
    31.         success : function(data) {  
    32.             alert("id: " + data.id + "\nname: " + data.name + "\nstatus: "  
    33.                     + data.status);  
    34.         },  
    35.         error : function() {  
    36.             alert('Err...');  
    37.         }  
    38.     });  

    Table 
    Html代码  收藏代码
    1. <table>  
    2.     <tr>  
    3.         <td>id</td>  
    4.         <td><input id="id" value="100" /></td>  
    5.     </tr>  
    6.     <tr>  
    7.         <td>name</td>  
    8.         <td><input id="name" value="snowolf" /></td>  
    9.     </tr>  
    10.     <tr>  
    11.         <td>status</td>  
    12.         <td><input id="status" value="true" /></td>  
    13.     </tr>  
    14.     <tr>  
    15.         <td><input type="button" id="profile" value="Profile——GET" /></td>  
    16.         <td><input type="button" id="login" value="Login——POST" /></td>  
    17.     </tr>  
    18. </table>  
0 0
原创粉丝点击