1. 程式人生 > 其它 >Spring MVC入門(三):獲取請求引數

Spring MVC入門(三):獲取請求引數

  • 通過ServletAPI獲取
    @RequestMapping("/testServletAPI")
    //形參位置的request表示當前請求
    public String testServletAPI(HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:"+username+",password:"+password);
        return "success";
    }

# http://localhost:8080/testServletAPI?username=goudan&password=12345
  • 通過形參獲取請求引數
    @RequestMapping("/testParam")
    public String testParam(String password,String hobby){
        System.out.println(password:"+password+",hobby:"+ hobby);
        return "success";
    }

# http://localhost:8080/testParam?password=12345&hobby=a&hobby=b
# 控制檯:password:12345,hobby:a,b

    @RequestMapping("/testParam")
    public String testParam(String password,String[] hobby){
        System.out.println(password:"+password+",hobby:"+ Arrays.toString(hobby));
        return "success";
    }

# http://localhost:8080/testParam?password=12345&hobby=a&hobby=b
# 控制檯:password:12345,hobby:[a,b]