Spring MVC-從零開始-@RequestMapping結合@PathVariable (從URL路徑中取值,作用於函數參數)


1、可以直接在RequestMapping中value元素中使用{key}描述屬性鍵

2、也可以在{key}中使用正則限定key的取值范圍,從而限定url的變化范圍

 

package com.jt;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/FirstControl")
public class HelloControl {
    @RequestMapping(value="/var/{name}/{id}")
    @ResponseBody
    public String viewVar(@PathVariable String name,@PathVariable String id){
        System.out.println("name "+name);
        System.out.println("id "+id);
        return ""+name+id;
    }
    
    @RequestMapping(value="/varregs/{name:[a-z]+}/{id:[0-9]+}")
    @ResponseBody
    public String viewVarReg(@PathVariable String name ,@PathVariable String id){
        return ""+name+id;
    }
    
}

 

1、未使用正則限定的情況下效果

 

 


2、使用正則限定的情況下效果

 

 3、使用正則限定的情況下效果(第一個參數不符合正則匹配)

4、使用正則限定的情況下效果(第二個參數不符合正則匹配)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM