SpringMVC笔记 02:请求参数绑定

简单的参数绑定

要求:请求的参数名要与响应请求的方法的参数名相同

@RequestMapping("/testParam")
public String testRequestParamsBinding(String username, String password) {
    System.out.println("用户名:" + username);
    System.out.println("密码:" + password);
    return "success";
}
<a href="params/testParam?username=dudu&password=dudu123">请求参数绑定</a>

将数据封装到JavaBean

要求:请求的参数名与JavaBean中的属性名相同,且属性一定有对应的set方法

@RequestMapping("/saveAccount")
public String testSaveAccount(Account account) {
    System.out.println(account);
    return "success";
}
/*
Account中的属性:
  private String userName;
  private String password;
  private double money;
*/
<form action="params/saveAccount" method="post">
    用户名:<input type="text" name="username"/> <br>
    密码:<input type="text" name="password"/> <br>
    金额:<input type="text" name="money"/> <br>
    <input type="submit" value="提交">
</form>

JavaBean中含有引用类型

要求:参数名要写成【引用类型名.属性名】

@RequestMapping("/saveAccount")
public String testSaveAccount(Account account) {
    System.out.println(account);
    return "success";
}
/*
Account中的属性:
  private String userName;
  private String password;
  private double money;
  private User user;
User中的属性:
  private String name;
  private Integer age;
*/
<form action="params/saveAccount" method="post">
    用户名:<input type="text" name="username"/> <br>
    密码:<input type="text" name="password"/> <br>
    金额:<input type="text" name="money"/> <br>
    姓名:<input type="text" name="user.name"/> <br>
    年龄:<input type="text" name="user.age"/> <br>
    <input type="submit" value="提交">
</form>

JavaBean中含有集合类型

要求:参数名要写成【引用类型名[xx].属性名】

@RequestMapping("/saveAccount")
public String testSaveAccount(Account account) {
    System.out.println(account);
    return "success";
}
/*
Account中的属性:
  private String userName;
  private String password;
  private double money;
  private List<User> list;
  private Map<String, User> map;
User中的属性:
  private String name;
  private Integer age;
*/
<form action="params/saveAccount" method="post">
    用户名:<input type="text" name="username"/> <br>
    密码:<input type="text" name="password"/> <br>
    金额:<input type="text" name="money"/> <br>
    <%-- 存到list中 --%>
    姓名:<input type="text" name="list[0].name"/> <br>
    年龄:<input type="text" name="list[0].age"/> <br>
    <%-- 存到map中 --%>
    姓名:<input type="text" name="map['1'].name"/> <br>
    年龄:<input type="text" name="map['1'].age"/> <br>
    <input type="submit" value="提交">
</form>

获取Servlet原生的API

@RequestMapping("/servletApi")
public String testServletAPI(HttpServletRequest request, HttpServletResponse response) {
    System.out.println("request: " + request);
    System.out.println("response: " + response);
    return "success";
}
<a href="params/servletApi">获取Servlet原生的API</a>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值