SpringMVC 參數的接收和處理

一.請求參數的接收

1.1servlet中如何接收參數?
request.getParameter(name), request.getParameterValues(name). 方法的特點:
a)參數要求是表單域的name屬性;
b)getParameter方法用於獲取單個值, 返回類型是String;
c)getParameterValues方法用於獲取一組數據, 返回結果是String[];
d)冗餘代碼較多, 使用麻煩, 類型需要自己轉換
1.2.使用SpringMVC進行參數接收
  1. 簡化參數接收形式(不需要調用任何方法, 需要什麼參數, 就在控制器方法中提供什麼參 數)
  2. 參數類型不需要自己轉換了. 日期時間得注意, 需要使用@DateTimeFormat註解聲明日期轉換時遵循的格式, 否則拋出400異常.
  3. 還可以自動將參數封裝爲對象.
1.3.代碼實現
<form action="/demo" method="post">
  用戶名: <input type="text" name="username"><br>
  年齡: <input type="text" name="age"><br>
  婚否: <input type="radio" name="married" value="true" />已婚
  <input type="radio" name="married" value="false" />未婚<br>
  愛好: <input type="checkbox" name="hob" value="java"> java
  <input type="checkbox" name="hob" value="php"> php
  <input type="checkbox" name="hob" value="python"> python<br>
  生日: <input type="text" name="birthday" placeholder="yyyy-MM-dd"><br>
  住址: <input type="text" name="address.city"><br>
  收貨地址: <input type="text" name="addrs[0].city"><br>
  <input type="text" name="addrs[1].city"><br>
  <input type="submit" value="提交">
</form>
@Controller
public class DemoController {
    @RequestMapping("/demo")
    public String demo2(User user) {
        System.out.println("user = " + user);
        return "index.jsp";
    }

    @RequestMapping("/demo1")
    public String demo1(String username, int age, String[] hob,
               @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday) {
        System.out.println("username = " + username);
        System.out.println("age = " + age);
        System.out.println("hob = " + Arrays.toString(hob));
        System.out.println("birthday = " + birthday);
        return "index.jsp";
    }
}

二.請求參數的進階處理

  • 解決參數名稱不匹配的問題, 解決基本類型參數接收問題; Restful風格參數的傳遞.

2.1@RequestParam註解的使用

用於對請求參數進行處理, 該註解有三個屬性:
  1. value | name: 表示前臺傳參的名稱, 如果前臺傳遞的參數名和方法使用的參數名一致, 可以省略不寫.
  2. required: 布爾值, 表示參數是否必須. 默認爲true, 表示該參數必須傳遞.
  3. defaultValue: 默認值. 當某個參數前臺沒有傳遞或傳遞的是空字符串, 此時, 默認值就會生效.
<%--參數名稱不匹配和基本類型問題--%>
<a href="demo?page=2&size=5">同名傳參</a><br>
<a href="demo?param_1=1&param_2=10">不同名傳參</a><br>
<a href="demo">沒有參數傳遞</a><br>
<a href="demo?param_1=">空串傳參</a>
@RequestMapping("/demo")
public String demo(
        @RequestParam(value = "param_1", defaultValue = "1") int page,
        @RequestParam(name = "param_2", defaultValue = "10") int size) {
    System.out.println("page = " + page);
    System.out.println("size = " + size);
    return "index.jsp";
}
2.2Restful風格參數傳遞

傳統形式參數傳遞: http://xxx/xxx?name1=value1&name2=value2…
Restful風格傳參: http://xxx/xxx/value1/value2/…
從訪問路徑中獲取參數, 需要使用註解: @PathVariable

<%--Restful風格傳參--%>
<a href="demo2/zhangsan/123">Restful</a>
@RequestMapping("/demo2/{username}/{pswd}")
public String demo2(
        @PathVariable String username,
        @PathVariable("pswd") String password) {
    System.out.println("username = " + username);
    System.out.println("password = " + password);
    return "/index.jsp";
}

三.@RequestMapping詳解

該註解用於爲後端控制器方法映射訪問路徑, 同時, 還可以對訪問路徑進行限制(窄化). 該註解可以用在方法上, 表示給方法映射訪問路徑; 還可以用在控制器類上, 表示對當前控制器下所有的方法添加訪問前綴路徑. 常用屬性介紹:

  1. value | path: 字符串數組, 代表映射到該控制器方法的所有訪問路徑;
    當配置爲: @RequestMapping("/demo1"), 能匹配的路徑有:
  • http://localhost:8080/demo1
  • http://localhost:8080/demo1.xxx
  • http://localhost:8080/demo1/
  1. method: RequestMethod數組, 表示當前控制器方法允許處理的請求方式. 默認允許所有請求方式. spring4.3後提供了兩個簡化註解: @GetMapping和@PostMapping
/**
 * 當前方法用於處理get請求
 *
 * @return
 */
// @RequestMapping(method = RequestMethod.GET)
@GetMapping
public String demo3() {
    System.out.println("DemoController.demo3: GET");
    return "/index.jsp";
}

/**
 * 當前方法用於處理post請求
 *
 * @return
 */
// @RequestMapping(method = RequestMethod.POST)
@PostMapping
public String demo2() {
    System.out.println("DemoController.demo2: POST");
    return "/index.jsp";
}
  1. params: 字符串數組, 表示對請求攜帶的參數進行限制. 要求請求中必須攜帶指定的參數, 否則拋出異常. token
/**
 * 要求必須傳遞username和password參數
 * 而且username的值必須是admin
 * 
 * @param username
 * @param password
 * @return
 */
@PostMapping(path = "/demo2", params = {"username=admin", "password"})
public String demo4(String username, String password) {
    System.out.println("DemoController.demo4");
    return "/index.jsp";
}
  1. produces: 字符串數組, 用於設置響應頭信息. 需要配合另外一個註解一起使用, 叫@ResponseBody
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章