標籤:
用 @RequestMapping來解析http請求中的參數。
1 通過瀏覽器發送一個帶參數的get請求來訪問伺服器中的服務,
http://localhost:8080/springmvc/RequestParam/testRequestParam2?username=xiaoming&age=11
url描述了一個get請求 網域名稱為springmvc ,後端是一個控制器的映射RequestParam,testRequestParam2是發布的服務名的映射,問號後面對應的請求參數和參數值
2 服務端的介面
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;@Controller@RequestMapping("/RequestParam")public class RequestParamsService { public final static String SUCCEEDD="show"; /** RequestParam * 映射的請求參數 可以是多個參數 * required * 表示參數是否是必須的 預設是true(表示是必須的) * defaultValue * 請求參數的預設值 * * @param username * @param age * @return */ @RequestMapping(value="/testRequestParam1",method=RequestMethod.GET) public String testRequestParam1(@RequestParam(value="username") String username , @RequestParam(value="age",required=false,defaultValue="0") int age){ System.out.println("testRequestParam1 username="+username +" ,age="+age); return SUCCEEDD; } /** * url : * @param username * @param age * @return */ @RequestMapping(value="/testRequestParam2",method=RequestMethod.GET) public String testRequestParam2(@RequestParam(value="username") String username , @RequestParam(value="age",required=false) Integer age){ System.out.println("testRequestParam2 username="+username +" ,age="+age); return SUCCEEDD; } }
3 通過瀏覽器進行測試
關於springMVC的請求參數如何處理(@RequestMapping 來映射請求參數)