@requestParam注解
Spring MVC éè¿åæå¤çæ¹æ³çç¾åï¼å° HTTP 请æ±ä¿¡æ¯ç»å®å°å¤çæ¹æ³çç¸åºäººåä¸ã
Spring MVC 对æ§å¶å¨å¤çæ¹æ³ç¾åçéå¶æ¯å¾å®½æ¾çï¼å ä¹å¯ä»¥æå欢çä»»ä½æ¹å¼å¯¹æ¹æ³è¿è¡ç¾åã
å¿ è¦æ¶å¯ä»¥å¯¹æ¹æ³åæ¹æ³å ¥åæ 注ç¸åºç注解ï¼@PathVariableã @RequestParamã @RequestHeader çï¼ã SpringMVC æ¡æ¶ä¼å° HTTP 请æ±çä¿¡æ¯ç»å®å°ç¸åºçæ¹æ³å ¥åä¸ï¼å¹¶æ ¹æ®æ¹æ³çè¿åå¼ç±»åååºç¸åºçåç»å¤çã
å¨å¤çæ¹æ³å ¥åå¤ä½¿ç¨ @RequestParam å¯ä»¥æ请æ±åæ°ä¼ éç»è¯·æ±æ¹æ³
â valueï¼åæ°å
â requiredï¼æ¯å¦å¿ é¡»ãé»è®¤ä¸º true, 表示请æ±åæ°ä¸å¿ é¡»å å«å¯¹åºçåæ°ï¼è¥ä¸åå¨ï¼å°æåºå¼å¸¸
æ§å¶å¨ç±»åå¤çå½æ°å¦ä¸ï¼
-
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请æ±åæ°çé»è®¤å¼