1. 程式人生 > >@RequestMapping 的地址對映 寫法

@RequestMapping 的地址對映 寫法

介紹

下面就來介紹一下RequestMapping中常用註解的用法。

RequestMapping中有六個屬性分別是:

value:     指定請求的實際地址,指定的地址可以是URI Template 模式;
method:  指定請求的method型別, GET、POST、PUT、DELETE等;
consumes: 指定處理請求的提交內容型別(Content-Type),例如application/json, text/html;
produces:    指定返回的內容型別,僅當request請求頭中的(Accept)型別中包含該指定型別才返回;
params: 指定request中必須包含某些引數值是,才讓該方法處理。
headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請求。

1.RequestMapping 地址對映的基本寫法:

@RequestMapping(value="/test",method = RequestMethod.POST)
private String tableShow(Model model, Entity entity){
    system.out.printly("輸出:   "+ entity);
    return "forward:/test.jsp";
}
<form id="loginTestForm" action="test" method="post">
    <input type="submit" value="submit">
</form>

 2.RequestMapping帶引數的情況:

@RequestMapping(value = "test2",method = RequestMethod.POST)
public String testLogin(@RequestParam("test2Id") String test2Id){
     System.out.println("輸入的引數為:"+test2Id);
     return "redirect:test2.jsp";
}

表單提交

<form id="" action="test2" method="post">
    <input type="text" name="test2Id"/></br>
    <input type="submit" value="submit"/>
</form>

超連結拼接

<a href="/test2?test2Id=1"></a><!--  值在多幾個後面用&  例 /test2?test2Id=1&test2Name='wangmx'  -->

3.RequestMapping中獲取URL中的引數資訊:

通過{變數名稱}這樣的寫法,就可以在@PathVariable("變數名")  實現引數的呼叫了。

@RequestMapping(value = "test3/{test3Id}",method = RequestMethod.POST)
public String testLogin(@PathVariable("test3Id") String test3Id){
     System.out.println("輸入的URL引數為:"+test3Id);
     return "redirect:test3.jsp";
}

當然也可以繫結多個引數

@RequestMapping(value = "test4/test4Id/{test4Id}/test4Name/{test4Name}",method = RequestMethod.POST)
public String testLogin(@PathVariable("test4Id") String test4Id,@PathVariable("test4Name") String test4Name){
        System.out.println("輸入的URL引數為1:"+test4Id);
	System.out.println("輸入的URL引數為2:"+test4Name;
        return "redirect:test4.jsp";
}