繼續整理,這個是前段時間用jsp開發的一個站點,說起來php程序員去做jsp程序確實有些小不適應,但是弄完後絕對對於這種強類型語言而比收獲還是頗多的。
1,最基本的,方法級別上應用 @RequestMapping(value="/departments") public String simplePattern(){ System.out.println("simplePattern method was called"); return "someResult"; } 則訪問http://localhost/xxxx/departments的時候,會調用 simplePattern方法了 2, 參數綁定 @RequestMapping(value="/departments") public String findDepatment( @RequestParam("departmentId") String departmentId){ System.out.println("Find department with ID: " + departmentId); return "someResult"; } 形如這樣的訪問形式: /departments?departmentId=23就可以觸發訪問findDepatment方法了 3, REST風格的參數 @RequestMapping(value="/departments/{departmentId}") public String findDepatment(@PathVariable String departmentId){ System.out.println("Find department with ID: " + departmentId); return "someResult"; } 形如REST風格的地址訪問,比如: /departments/23,其中用(@PathVariable接收rest風格的參數 4,REST風格的參數綁定形式之2 先看例子,這個有點象之前的: @RequestMapping(value="/departments/{departmentId}") public String findDepatmentAlternative( @PathVariable("departmentId") String someDepartmentId){ System.out.println("Find department with ID: " + someDepartmentId); return "someResult"; } 這個有點不同,就是接收形如/departments/23的URL訪問,把23作為傳入的departmetnId,,但是在實際的方法findDepatmentAlternative中,使用 @PathVariable("departmentId") String someDepartmentId,將其綁定為 someDepartmentId,所以這裡someDepartmentId為23 5, url中同時綁定多個id @RequestMapping(value="/departments/{departmentId}/employees/{employeeId}") public String findEmployee( @PathVariable String departmentId, @PathVariable String employeeId){ System.out.println("Find employee with ID: " + employeeId + " from department: " + departmentId); return "someResult"; } 這個其實也比較好理解了。 6, 支持正則表達式 @RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\\d]+}") public String regularExpression( @PathVariable String textualPart, @PathVariable String numericPart){ System.out.println("Textual part: " + textualPart + ", numeric part: " + numericPart); return "someResult"; } 比如如下的URL:/sometext.123,則輸出: Textual part: sometext, numeric part: 123.