1. 程式人生 > >SpringMVC學習筆記(一)@RequestMapping、@RequestParm等註解

SpringMVC學習筆記(一)@RequestMapping、@RequestParm等註解

1.1 @RequestMapping對映請求
SpringMVC使用 @RequestMapping註解為控制器指定可以處理那些URL請求

@RequestMapping 可以定義在類和方法上

package com.ibigsea.springmvc.helloworld;

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

@Controller
public class HelloWorld {

    /**
     * 配置@RequestMapping 攔截 localhost:8080/springmvc/hello 請求 
     * @return
*/
@RequestMapping("/hello") public String helloWorld() { System.out.println("hello world"); return "helloworld"; } }
package com.ibigsea.springmvc.helloworld;

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

@Controller
@RequestMapping("/hello") public class HelloWorld { /** * 配置@RequestMapping 攔截 localhost:8080/springmvc/hello/world 請求 * @return */ @RequestMapping("/world") public String helloWorld(){ System.out.println("hello world"); return "helloworld"; } }

@RequestMapping
–類定義處:提供初步的請求對映資訊。相對於WEB應用的根目錄
–方法處: 提供進一步的細分對映資訊。相對於類定義處的URL。若類定義處未標註@RequestMapping,則方法處標記的URL相對於WEB應用的根目錄

DispathcherServlet截獲請求後,就通過控制器上@RequestMapping提供的對映資訊確定請求所對應的處理方法

@RequestMapping除了可以使用請求URL對映請求外,還可以使用請求方法、請求引數及請求頭對映請求

[email protected]限定請求方法、請求引數、請求頭

/**
     * 接收GET請求
     * @return
     */
    @RequestMapping(value="/get",method = RequestMethod.GET)
    public String get(){
        System.out.println("get");
        return "get";
    }

    /**
     * 接收POST請求
     * @return
     */
    @RequestMapping(value="/post",method = RequestMethod.POST)
    public String post(){
        System.out.println("post");
        return "post";
    }

    /**
     * 只接收 name 引數
     * @return
     */
    @RequestMapping(value="/params",params="name")
    public String params(String name){
        System.out.println("hello "+name);
        return "helloworld";
    }

    /**
     * 只接收請求頭中 Content-Type 為 text/html;charset=UTF-8的請求
     * @return
     */
    @RequestMapping(value="/headers",headers="Content-Type:text/html;charset=UTF-8")
    public String headers(){
        System.out.println("headers");
        return "helloworld";
    }

[email protected]匹配符
– ?:匹配檔名中的一個字元

– *:匹配檔名中的任意字元

匹配多層路徑

例項:

URL : /user/*/create

– /user/bigsea/create 、 /user/sea/create 等URL

URL : /user/**/create

– /user/big/sea/create 、 /user/sea/big/create 等URL

URL : /user/create??

– /user/createaa 、/user/createbb

1.4 @RequestParam 繫結請求引數

在處理方法入參處使用@RequestParam可以把請求引數傳遞給請求方法
-value:引數名
-required:表示這個引數是否是必須的 預設為true,表示請求引數中必須包含對應的引數,若不存在,將丟擲異常

/**
     * 如果 required = true 則表示請求引數對應的 欄位 必須存在.如果不存在則會丟擲異常<br/>
     * @param firstName 可以為null
     * @param lastName 不能為null .為null報異常
     * @param age age欄位表示如果沒有 age 引數 則預設值為 0 
     * @return
     */
    @RequestMapping("/requestParam")
    public String requestParam(@RequestParam(value="firstName",required=false)String firstName,
            @RequestParam( value="lastName" ,required = true) String lastName,
            @RequestParam(value="age",required = false ,defaultValue="0")int age) {
        System.out.println("hello my name is " + (firstName == null ? "" : firstName)
                                        + lastName + "," + age +" years old this year");
        return "helloworld";
    }