SpringMVC(三) RequestMapping修飾類
阿新 • • 發佈:2018-12-01
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>