源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

SpringMVC通过注解获得参数的实例

  • 时间:2020-09-17 04:53 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:SpringMVC通过注解获得参数的实例
SpringMVC可以通过RequestParam注解来映射获得参数,具体用法如下: [img]http://files.jb51.net/file_images/article/201708/2017081809371925.png[/img] [b]例子:[/b] [b]配置过程省略[/b] [b]1.新建controller类[/b]
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;
  }
}
[b]2.index.jsp[/b] [img]http://files.jb51.net/file_images/article/201708/2017081809371926.png[/img] 运行结果: [img]http://files.jb51.net/file_images/article/201708/2017081809371927.png[/img] [img]http://files.jb51.net/file_images/article/201708/2017081809371928.png[/img] [b]补充:如果表单名跟方法的参数名一致的话,无需再用@RequestParam注解来映射。[/b] [b]如改为[/b] @RequestMapping(value="/requestparam") public String requestParam(String username,Integer age)即可! [b]用类作为参数,且包含级联属性的参数获取方法:  [/b] [b]1.新建Adress类 [/b]
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 + "]";
  }
  
}
[b]2.新建User类[/b]
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 + "]";
  }
  
}
[b]3.controller[/b] [img]http://files.jb51.net/file_images/article/201708/2017081809371929.png[/img] [b]4.表单[/b] User有级联属性Address,表单传入的参数是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>
运行结果: [img]http://files.jb51.net/file_images/article/201708/2017081809371930.png[/img] 以上这篇SpringMVC通过注解获得参数的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部