zoukankan      html  css  js  c++  java
  • 将前端请求中的数据绑定到Spring MVC响应方法中参数的四种方法

    一、映射URL绑定的占位符到方法参数

    1.方法

    使用@PathVariable注解

    2.代码示例

    a.接收请求方法

    @RequestMapping(value = "/deleteInfo/{id}")
    public String deleteInfo(@PathVariable("id") int shopCartId) {
        return "删除成功";
    }

    b.发出请求

    <a href="../shopCart/deleteInfo/1"></a>

    二、绑定请求中的参数到方法参数

    1.方法

    使用@RequestParam注解

    2.代码示例

    a.接收请求方法

    @RequestMapping(value = "/changeAmount")
    public String changeAmount(@RequestParam(value = "amount") String amount,
            @RequestParam(value = "shopCartId") String shopCartId) {
      return "修改成功"; }

     b.发出请求

    <a href="../shopCart/changeAmount?amount=3&shopCartId=10"></a>

    三、绑定表单对象到方法参数(实例对象)

    1.方法

    创建表单发送请求

    2.代码示例

    a.接收请求方法

    @RequestMapping("/saveInfo")
    public String saveInfo(Book book){
        return "保存成功";
    }

    b.发出请求

    <form action="../shopCart/saveInfo" method="post">
        BookName:<input type="text" name="bookName"><br>
        BookId:<input type="password" name="bookId"><br>
        sellerId:<input type="text" name="seller.sellerId"><br>
        sellerName:<input type="text" name="seller.sellerName"><br>
        <input type="submit" value="提交">
    </form>

    c.Book.java

    public class Book{
      private int bookId;
      private String bookName;
      private Seller seller;
      // 省略getter和setter方法
    }

    d.Seller.java

    public class Seller{
      private int sellerId;
      private String sellerName;
      // 省略getter和setter方法
    }

    四、绑定表单对象到方法参数(Map对象)

    1.方法

    创建表单发送请求

    2.代码示例

    a.接收请求方法

    @RequestMapping("/getUser")
    public String getUser(UserMap uMap) {
        Set set=uMap.getuMap().keySet();
        Iterator iterator=set.iterator();
        while(iterator.hasNext()) {
            Object keyName=iterator.next();
            User u=uMap.getuMap().get(keyName);
            System.out.println(u);
        }
        return "success";        
    }

    b.发出请求

    <form action="../user/getUser" method="post">
        loginName1:<input type="text" name="uMap['u1'].loginName"><br>
        loginPwd1:<input type="password" name="uMap['u1'].loginPwd"><br>
        province1:<input type="text" name="uMap['u1'].address.province"><br>
        city1:<input type="text" name="uMap['u1'].address.city"><br>
        loginName2:<input type="text" name="uMap['u2'].loginName"><br>
        loginPwd2:<input type="password" name="uMap['u2'].loginPwd"><br>
        province2:<input type="text" name="uMap['u2'].address.province"><br>
        city2:<input type="text" name="uMap['u2'].address.city"><br>
        <input type="submit" value="提交">
    </form>

    c.UserMap.java

    public class UserMap {
        private Map<String, User> uMap;
    
        public Map<String, User> getuMap() {
            return uMap;
        }
    
        public void setuMap(Map<String, User> uMap) {
            this.uMap = uMap;
        }
    }
  • 相关阅读:
    IOS创建Button简单实例
    IOSActionSheet、AlertView、Slider、Switch的简单使用
    IOS翻转注意
    [教程]iOS 4 开发的好东西 (资料)url
    Proxy和Decorator模式
    Using JNDI connecting DB
    影片“One Day”,还不错
    Minabased TCP server examples
    MySQL vs. Oracle on sequence
    Java DB and JDBC(Embedded Derby)
  • 原文地址:https://www.cnblogs.com/YeHuan/p/11681014.html
Copyright © 2011-2022 走看看