1. 程式人生 > >Java Spring Controller 獲取請求引數的幾種方法詳解

Java Spring Controller 獲取請求引數的幾種方法詳解

1、直接把表單的引數寫在Controller相應的方法的形參中,適用於get方式提交,不適用於post方式提交。若”Content-Type”=”application/x-www-form-urlencoded”,可用post提交

/**
   * 1.直接把表單的引數寫在Controller相應的方法的形參中
   * @param username
   * @param password
   * @return
   */
  @RequestMapping("/addUser1")
  public String addUser1(String username,String password) {
    System.out.println("username is:"
+username); System.out.println("password is:"+password); return "demo/index"; }

2、通過HttpServletRequest接收,post方式和get方式都可以。

 /**
   * 2、通過HttpServletRequest接收
   * @param request
   * @return
   */
  @RequestMapping("/addUser2")
  public String addUser2(HttpServletRequest request) {
    String username=request.getParameter("username"
); String password=request.getParameter("password"); System.out.println("username is:"+username); System.out.println("password is:"+password); return "demo/index"; }

3、通過一個bean來接收,post方式和get方式都可以。

 /**
   * 3、通過一個bean來接收
   * @param user
   * @return
   */
  @RequestMapping("/addUser3"
) public String addUser3(UserModel user) { System.out.println("username is:"+user.getUsername()); System.out.println("password is:"+user.getPassword()); return "demo/index"; }

4、使用@ModelAttribute註解獲取POST請求的FORM表單資料  

/**
   * 4、使用@ModelAttribute註解獲取POST請求的FORM表單資料
   * @param user
   * @return
   */
  @RequestMapping(value="/addUser5",method=RequestMethod.POST)
  public String addUser5(@ModelAttribute("user") UserModel user) {
    System.out.println("username is:"+user.getUsername());
    System.out.println("password is:"+user.getPassword());
    return "demo/index";
  }

5、用註解@RequestParam繫結請求引數到方法入參

當請求引數username不存在時會有異常發生,可以通過設定屬性required=false解決,例如:

@RequestParam(value="username", required=false)
  **** 若"Content-Type"="application/x-www-form-urlencoded",post get都可以
  **** 若"Content-Type"="application/application/json",只適用get
   /**
   * 5、用註解@RequestParam繫結請求引數到方法入參
   * @param username
   * @param password
   * @return
   */
  @RequestMapping(value="/addUser6",method=RequestMethod.GET)
  public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {
    System.out.println("username is:"+username);
    System.out.println("password is:"+password);
    return "demo/index";
  }

6、用request.getQueryString() 獲取spring MVC get請求的引數,只適用get請求

 @RequestMapping(value="/addUser6",method=RequestMethod.GET)
  public String addUser6(HttpServletRequest request) { 
    System.out.println("username is:"+request.getQueryString()); 
    return "demo/index"; 
  }

相關推薦

Java Spring Controller 獲取請求引數方法

1、直接把表單的引數寫在Controller相應的方法的形參中,適用於get方式提交,不適用於post方式提交。若”Content-Type”=”application/x-www-form-urlencoded”,可用post提交 /** * 1.

spring獲取webapplicationcontext,applicationcontext方法

方法一:在初始化時儲存ApplicationContext物件 程式碼: ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("

Java Spring Controller 獲取請求參數的方法

交流 str eth req turn let oca form表單 val 技術交流群:233513714 1、直接把表單的參數寫在Controller相應的方法的形參中,適用於get方式提交,不適用於post方式提交。若"Content-Type"="applic

Spring Controller 獲取請求引數方法

  1、直接把表單的引數寫在Controller相應的方法的形參中,適用於get方式提交,不適用於post方式提交。若"Content-Type"="application/x-www-form-urlencoded",可用post提交   /** * 1.直接把表單的引數寫在Control

Spring獲取 request 的方法,及其線程安全性分析

變種 bject input sta cli 方法參數 一律 內存空間 足夠 概述在使用Spring MVC開發Web系統時,經常需要在處理請求時使用request對象,比如獲取客戶端ip地址、請求的url、header中的屬性(如cookie、授權信息)、body中的數據

$.ajax()方法從伺服器獲取json資料方式

一.什麼是json json是一種取代xml的資料結構,和xml相比,它更小巧但描述能力卻很強,網路傳輸資料使用流量更少,速度更快。 json就是一串字串,使用下面的符號標註。 {鍵值對} : json物件 [{},{},{}] :json陣列 "" :雙引號內是屬性或值

php抓取頁面的方法

close deb clas win exe _array error: fopen ini 一、 PHP抓取頁面的主要方法:1. file()函數 2. file_get_contents()函數 3. fopen()->fread()->fclose

jQuery使用JSONP實現跨域獲取資料的三方法

本文例項講述了jQuery使用JSONP實現跨域獲取資料的三種方法。分享給大家供大家參考,具體如下: 第一種方法是在ajax函式中設定dataType為'jsonp' $.ajax({ dataType: 'jsonp', url: 'http://www.a

Spring Controller 獲取請求參數的方法

不存在 orm red ID DDU usermod ping 接收 發生 1、直接把表單的參數寫在Controller相應的方法的形參中,適用於get方式提交,不適用於post方式提交。若"Content-Type"="application/x-www-form-ur

Spring 獲取 request 的方法及其線程安全性分析

就是 base p地址 sco 便是 情況 C4D cookie tex 本文將介紹在Spring MVC開發的Web系統中,獲取request對象的幾種方法,並討論其線程安全性。 一、概述 在使用Spring MVC開發Web系統時,經常需要在處理請求時使用request

Java從檔案路徑中獲取檔名的方法

Java從檔案路徑中獲取檔名的幾種方法 舉例:String fName =” G:\Java_Source\navigation_tigra_menu\demo1\img\lev1_arrow.gif ” 方法一: ? View Code 

java獲取時間的方法

1. 通過Date類來獲取當前時間 Date day=new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:

Java使用HttpClient傳送請求常用方式

使用的jar包有3個,Maven中新增以下依賴: <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient<

Javaspring讀取配置檔案的方法

    在現實工作中,我們常常需要儲存一些系統配置資訊,大家一般都會選擇配置檔案來完成,本文根據筆者工作中用到的讀取配置檔案的方法小小總結一下,主要敘述的是spring讀取配置檔案的方法。     一、讀取xml配置檔案     (一)新建一個java bean

獲取JAVA[WEB]專案相關路徑的方法

在jsp和class檔案中呼叫的相對路徑不同。 在jsp裡,根目錄是WebRoot 在class檔案中,根目錄是WebRoot/WEB-INF/classes 當然你也可以用System.getProperty("user.dir")獲取你工程的絕對路徑。 另:在Jsp,Servlet,Java中詳細獲得路

Java實現攔截HTTP請求方式

在Java的服務端開發當中,攔截器是很常見的業務場景,這裡對Java開發當中幾種常見的攔截器的實現方式進行記錄和分析。案例說明基於Spring Boot環境。一:實現javax.servlet.Filter介面(使用過濾器方式攔截請求)import org.springfra

Java獲取隨機數的方法總結

方法1 (資料型別)(最小值+Math.random()*(最大值-最小值+1)) 例: (int)(1+Math.random()*(10-1+1)) 從1到10的int型隨數 方法2 獲得隨機數 for (int i=0;i<30

java獲取隨機數的方法

1.Math.random()方法 例子:求1到10之間的隨機數 public static void main(String[] args) { int n; for (int i

SpringController 獲取請求參數的方法筆記

-type 接收 turn public str json href mod oca 1、直接把表單的參數寫在Controller相應的方法的形參中,適用於get方式提交,不適用於post方式提交。若"Content-Type"="application/x-www-f

java調用webservice接口 方法

href default pst ted 網址 後來 即使 tool poi webservice的 發布一般都是使用WSDL(web service descriptive language)文件的樣式來發布的,在WSDL文件裏面,包含這個webservice暴露在外面可