標籤:div 資訊 test ati imp static tcl gtest cal
SpringMVC使用@RequestMapping 註解為控制器指定可以處理哪些URL請求。
可以用於類定義以及方法定義:
類定義:提供初步的請求映射資訊。相對於WEB應用的根目錄。
方法處:提供進一步的細分映射資訊。相對於類定義處的URL。若類定義處沒有定義,則是相對於根目錄。
如:針對類設定了@RequestMapping("pathclass")註解,針對方法設定了@RequestMapping("method"),則最終調用到方法的url為pathclass/method,完整路徑如http://localhost:8080/HelloWorld/pathclass/helloworld.
參考如下Controller測試代碼:
package com.tiekui.springmvc.handlers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("pathclass")public class RequestMappingTest { private static String SUCCESS="success"; @RequestMapping("helloworld") public String hello(){ System.out.println("hello world from " + getClass()); return SUCCESS; }}
jsp中調用這個方法的參考代碼如下,可以將以下代碼加在HelloWorld工程中的index.jsp中。
<a href="pathclass/helloworld">Pathclass Hello world Test</a>
SpringMVC(三) RequestMapping修飾類