1. 程式人生 > >SpringMVC 對映請求引數 & 請求引數(四)

SpringMVC 對映請求引數 & 請求引數(四)

一、請求處理方法簽名

  • Spring MVC 通過分析處理方法的簽名,將 HTTP 請求資訊繫結到處理方法的相應入參中。

  • Spring MVC 對控制器處理方法簽名的限制是很寬鬆的,幾乎可以按喜歡的任何方式對方法進行簽名。

  • 必要時可以對方法及方法入參標註相應的註解(@PathVariable、@RequestParam、@RequestHeader 等)、SpringMVC 框架會將 HTTP 請求的資訊繫結到相應的方法入參 中,並根據方法的返回值型別做出相應的後續處理。

二、使用 @RequestParam 繫結請求引數值

在處理方法入參處使用 @RequestParam 可以把請求引數傳遞給請求方法

  • value:引數名

  • required:是否必須。預設為 true, 表示請求引數中必須包含對應的引數,若不存在,將丟擲異常

(1)前端頁面

<body>
    <a href="helloHandler?username=crr&age=18">helloHandler</a>
</body>

(2)Controller

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

@Controller
public class HelloHandler {
    @RequestMapping("/helloHandler")
    public String hello(@RequestParam("username") String userName, @RequestParam("age") int age){
        System.out.println("userName : " + userName);
        System.out.println("age : " + age);
        return "success";
    }
}

輸出結果:

userName : crr
age : 18

問題: 有時url中有些引數可能不存在,會出問題

(1)url引數修改

<body>
    <a href="helloHandler?username=crr">helloHandler</a>
</body>

解決辦法: 

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

@Controller
public class HelloHandler {
    @RequestMapping("/helloHandler")
    public String hello(@RequestParam(value = "username") String userName, @RequestParam(value = "age", required = false) Integer age){
        System.out.println("userName : " + userName);
        System.out.println("age : " + age);
        return "success";
    }
}

輸出結果:

userName : crr
age : null

或者也可以(對於基本型別的引數,要設定預設的值,否則會報錯):

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

@Controller
public class HelloHandler {
    @RequestMapping("/helloHandler")
    public String hello(@RequestParam(value = "username") String userName, @RequestParam(value = "age", required = false, defaultValue = "18") int age){
        System.out.println("userName : " + userName);
        System.out.println("age : " + age);
        return "success";
    }
}

輸出結果:

userName : crr
age : 18

三、使用 @RequestHeader 繫結請求報頭的屬性值

  • 請求頭包含了若干個屬性,伺服器可據此獲知客戶端的資訊,通過 @RequestHeader 即可將請求頭中的屬性值繫結到處理方法的入參中。

    

(1)URL

<body>
    <a href="headerInfoTest">helloHandler</a>
</body>

(2)Controller

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

@Controller
public class RquestHeaderInfo {
    @RequestMapping("headerInfoTest")
    public String headerInfoTest(@RequestHeader(value = "Accept-Encoding") String encoding){
        System.out.println("Accept-Encoding : " + encoding);
        return "success";
    }
}

輸出結果:

Accept-Encoding : gzip, deflate

四、使用 @CookieValue 繫結請求中的 Cookie 值

  • @CookieValue 可讓處理方法入參繫結某個 Cookie 值

    

(1)URL

http://localhost:8080/springmvc/requestCookieValue

(2)Controller

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

@Controller
public class RquestCookieValue {
    @RequestMapping("/requestCookieValue")
    public String requestCookieValue(@CookieValue("JSESSIONID") String session){
        System.out.println("session : " + session);
        return "success";
    }
}

輸出結果:

session : 16FEC1D91F3570AEA95AE576E73DEFF4

五、使用 POJO 物件繫結請求引數值

  • Spring MVC 會按請求引數名和 POJO 屬性名進行自動匹配,自動為該物件填充屬性值。支援級聯屬性如:dept.deptId、dept.address.tel 等

(1)URL

http://localhost:8080/springmvc/pojoTest?name=crr&age=18&address.province=jiangsu&address.city=nanjing

(2)Controller

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class POJO {

    @RequestMapping("/pojoTest")
    public String pojoTest(User user){
        System.out.println(user);
        return "success";
    }
}

輸出結果:

User{name='crr', age=18, address=Address{province='jiangsu', city='nanjing'}}

六、使用 Servlet API 作為入參

MVC 的 Handler 方法可以接受哪些 ServletAPI 型別的引數?

  • HttpServletRequest

  •  HttpServletResponse

  • HttpSession

  • java.security.Principal

  • Locale

  • InputStream

  • OutputStream

  • Reader

  • Writer

(1) Controller

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

@Controller
public class SertvletAPI {
    @RequestMapping("testServletAPI")
    public void testServletAPI(HttpServletRequest req, HttpServletResponse resp, Writer writer) throws IOException {
        writer.write("hello");
    }
}

輸出結果: