springMVC筆記系列(8)——RequestParam註解

前面的文章介紹過註解@PathVariable,它可以爲Rest風格的URL用佔位符的方式傳遞一個參數,可是這個參數並非真正意義上的請求參數。請求參數怎麼處理是本文的主要內容。html

Spring MVC 經過分析處理方法的簽名,將 HTTP 請求信息綁定處處理方法的相應人蔘中。java

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

必要時能夠對方法及方法入參標註相應的註解(@PathVariable、 @RequestParam、 @RequestHeader 等)、 SpringMVC 框架會將 HTTP 請求的信息綁定到相應的方法入參中,並根據方法的返回值類型作出相應的後續處理。spring

(本文出自:http://my.oschina.net/happyBKs/blog/417032)瀏覽器

在處理方法入參處使用 @RequestParam 能夠把請求參數傳遞給請求方法mvc

– value:參數名app

– required:是否必須。默認爲 true, 表示請求參數中必須包含對應的參數,若不存在,將拋出異常框架


控制器類和處理函數以下:jsp

package com.happyBKs.springmvc.handlers;

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

@RequestMapping("class")
@Controller
public class RPTestHandler {
	
	String page="successrm";
	
	@RequestMapping("student")
	public String handle(@RequestParam(value="username") String un, @RequestParam(value="age") int age)
	{
		System.out.println("a student's request has come. username: "+un+", age: "+age);
		return page;
	}
	
	

}

這裏使用@RequestParam註解的value屬性值來映射請求參數。函數

請求頁面index8.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="class/student?username=happyBKs&age=100">class/student?username=happyBKs&age=100</a>
</body>
</html>

好,運行過程以下:

點擊超攔截請求:

控制檯此時輸出:

a student's request has come. username: happyBKs, age: 100


也能夠直接在瀏覽器地址欄輸入http://localhost:8080/mymvc/class/student?username=happyBKs&age=101

控制檯顯示   a student's request has come. username: happyBKs, age: 101



問題1:若是咱們的請求少一個參數age,會怎麼樣?

這個問題該如何解決呢?

這裏須要用到@RequestParam註解的required屬性或defaultValue屬性。

required屬性標註這個參數是不是必需大的,默認是true,若是想讓它能夠不存在,那麼就設置爲false。可是請注意,required設置成false的參數對應的處理函數的參數類型必須是對象類型,不然回報錯500!

例如這裏咱們若是將控制器類改爲:

package com.happyBKs.springmvc.handlers;

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

@RequestMapping("class")
@Controller
public class RPTestHandler {
	
	String page="successrm";
	
	@RequestMapping("student")
	public String handle(@RequestParam(value="username") String un, @RequestParam(value="age",required=false) int age)
	{
		System.out.println("a student's request has come. username: "+un+", age: "+age);
		return page;
	}
	
	

}

結果會報錯,由於age雖然設置了required爲false,請求參數能夠不包含age,可是在處理函數中對應的參數類型是int,int是基本數據類型,沒法爲空,因此報錯。解決辦法是將int改成Integer。

運行結果:


控制檯輸出:

a student's request has come. username: happyBKs, age: null



若是咱們仍然想用基本類型做爲參數類型,那麼能夠用到@RequestParam註解的defaultValue屬性來指定默認值。

package com.happyBKs.springmvc.handlers;

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

@RequestMapping("class")
@Controller
public class RPTestHandler {
	
	String page="successrm";
	
	@RequestMapping("student")
	public String handle(@RequestParam(value="username") String un, @RequestParam(value="age",required=false, defaultValue="0") int age)
	{
		System.out.println("a student's request has come. username: "+un+", age: "+age);
		return page;
	}
	
	

}

運行結果:


控制檯 a student's request has come. username: happyBKs, age: 0


最後,作個總結:(@RequestParam註解是很經常使用的,因此很重要)

* @RequestParam 來映射請求參數

* value值 即請求參數名

* required 該參數是否必需。默認爲true

* defaultValue請求參數的默認值

相關文章
相關標籤/搜索