SpringMVC中的參數(shù)怎么利用注解獲取

SpringMVC中的參數(shù)怎么利用注解獲?。亢芏嘈率謱Υ瞬皇呛芮宄?,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)建站是專業(yè)的武邑網(wǎng)站建設(shè)公司,武邑接單;提供網(wǎng)站制作、成都網(wǎng)站設(shè)計,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行武邑網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

SpringMVC可以通過RequestParam注解來映射獲得參數(shù),具體用法如下:

SpringMVC中的參數(shù)怎么利用注解獲取

例子:

配置過程省略

1.新建controller類

package com.loger.controller;

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


@Controller
public class RequestParam {
  
  public static final String SUCCESS = "success";
  
  @RequestMapping(value="/requestparam")
  public String requestParam(@org.springframework.web.bind.annotation.
      RequestParam(value="username") String un,
      @org.springframework.web.bind.annotation.RequestParam(value="age") Integer age){
    
    System.out.println(un + " " + age);
    
    return SUCCESS;
  }
}

2.index.jsp

SpringMVC中的參數(shù)怎么利用注解獲取

運行結(jié)果:

SpringMVC中的參數(shù)怎么利用注解獲取

SpringMVC中的參數(shù)怎么利用注解獲取

補充:如果表單名跟方法的參數(shù)名一致的話,無需再用@RequestParam注解來映射。

如改為

@RequestMapping(value="/requestparam")
public String requestParam(String username,Integer age)即可!

用類作為參數(shù),且包含級聯(lián)屬性的參數(shù)獲取方法:  

1.新建Adress類

package com.loger.bean;

public class Address {
  private String province;
  private String city;
  public String getProvince() {
    return province;
  }
  public void setProvince(String province) {
    this.province = province;
  }
  public String getCity() {
    return city;
  }
  public void setCity(String city) {
    this.city = city;
  }
  @Override
  public String toString() {
    return "Address [province=" + province + ", city=" + city + "]";
  }
  
}

2.新建User類

package com.loger.bean;

public class User {
  private String name;
  private int age;
  private Address address;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public Address getAddress() {
    return address;
  }
  public void setAddress(Address address) {
    this.address = address;
  }
  @Override
  public String toString() {
    return "User [name=" + name + ", age=" + age + ", address=" + address + "]";
  }
  
}

3.controller

SpringMVC中的參數(shù)怎么利用注解獲取

4.表單

User有級聯(lián)屬性Address,表單傳入的參數(shù)是address.city address.province

<form action="pojoparam">
    姓名:<input type="text" name="name"><br>
    年齡:<input type="text" name="age"><br>
    城市:<input type="text" name="address.city"><br>
    省份:<input type="text" name="address.province"><br>
    <input type="submit" value="提交"><br>
  </form>

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

分享文章:SpringMVC中的參數(shù)怎么利用注解獲取
當(dāng)前地址:http://muchs.cn/article14/jpejde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作做網(wǎng)站、網(wǎng)站設(shè)計、Google、App開發(fā)、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計