• springMvc--接受请求参数


    表单:

    <h1>接受基本类型参数到Controller</h1>
    <form action="/param/test" method="post">
         用户编号:<input type="text" name="id"/>
         用户名:<input type="text" name="name"/>
         <input type="submit" value="提交"/>
    </form>

    controller:

    /**
     * 接受基本类型参数
     */
    @RequestMapping("/test")
    public String test(Integer id,String name){
         System.out.println(id);
         System.out.println(name);
         return "index";
    }

    post提交方式提交表单, 出现中文乱码, 可以在web.xml中配置字符过滤器解决这个问题

    web.xml

    <!-- 处理post提交中文乱码 -->
    <filter>
        <filter-name>characterEncoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--  指明定编码规则 -->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    接收对象类型:

    实体类:user和order存在关系

    public class User {
        private String id;  
        private String name;
        private Order order;
        ......
    }    
    public class Order {
        private Integer no;
        private String name;
        .......
    )

    1)接收有关系的对象

    表单:

    <h1>接受对象类型参数到Controller</h1>
    <form action="/param/test1" method="post">
        用户编号:<input type="text" name="id"/>
        用户名:<input type="text" name="name"/>
        订单编号:<input type="text" name="order.no"/>
        订单名称:<input type="text" name="order.name"/>
        <input type="submit" value="提交"/>
    </form>

     controller:

    /**
     * 接受对象类型参数,通过user中的关系属性,获得order
     */
    @RequestMapping("/test1")
    public String test1(User user){
       System.out.println(user.getid());
       System.out.println(user.getOrder().getNo());
       return "index";
    }

    2)接收多个对象

    如果是没有关系的实体类,需要把实体类封装在一个Vo中:

    BeanVO:

    /**
     *user和order数据传输类
     */
    public class BeanVO {
        private User user;
        private Order order;
        ......
    }

     controller:

    /**
     * 接受多个对象类型参数
     * 如果多个pojo之间没有关系,也可以采用这个方法
     */
    @RequestMapping("/test2")
    public String test1(BeanVO beanVO){
         System.out.println(beanVO.getUser());
         System.out.println(beanVO.getOrder());
         return "index";
    }

    接收数组类型:

    springMvc支持接收数组类型

    表单:

    <h1>接受数组类型参数到Controller</h1>
    <form action="/param/test3" method="post">
        str[0]:<input type="text" name="str"/>
        str[1]:<input type="text" name="str"/>
        str[2]:<input type="text" name="str"/>
        <input type="submit" value="提交"/>
    </form>

    controller:

    /**
     * 支持接收接受数组类型
     * @requestMapping 中,属性二:method=RequestMethod.POST
     * 注意: 一旦方法声明的@requestMapping注解中加入method属性, 该方法只能被method属性中声明的提交方式访问, 默认支持所有提交方式
     */
    @RequestMapping(value="/test3",method=RequestMethod.POST)
     public String test2(String[] str){
         for (String string : str) {
            System.out.println(string);
         }
         return "index";
     }

    接收集合类型:

    springMvc不支持直接接受集合类型,需要定义一个Vo类

    public class CollectionVO {
        private List<String> list;
        private List<User> users;
        private Map<String,User> maps;
        ......
    }

    接下来分别测试

    1) list集合泛型是基本类型,例如:list<String>

    表单:

    <h1>接受集合类型,泛型是基本类型参数到Controller</h1>
    <form action="/param/test4" method="post">
         list[0]:<input type="text" name="list"/>
         list[1]:<input type="text" name="list"/>
         list[2]:<input type="text" name="list"/>
         <input type="submit" value="提交"/>
    </form>

    controller:

    /**
     * 接受集合类型参数 List<String>
     *     注意:springmvc 不支持在方法中定义集合类型参数接受直接接受
     *  解决办法:
     *      封装接收
     */
    @RequestMapping("/test4")
    public String test3(CollectionVO collectionVO){
        for (String string : collectionVO.getList()) {
            System.out.println(string);
         }
        return "index";
    }

    2)list集合泛型对象类型,例如:List<User> users

    form表单:

    <h1>接受集合泛型User类型参数到Controller</h1>
    <form action="/param/test5" method="post">
        users[0].id:<input type="text" name="users[0].id"/>
        users[1].id:<input type="text" name="users[1].id"/>
        users[2].id:<input type="text" name="users[2].id"/>
        <input type="submit" value="提交"/>
    </form>

    controller: 接受到的view数据,会被封装成3个User对象并添加到List<User> users集合中

    /**
     * 接受集合类型参数 List<User>
     *     注意:springmvc 不支持集合类型参数接受
     *  解决办法:
     *     封装接收
     */
    @RequestMapping("/test5")
    public String test4(CollectionVO collectionVO){
         for (User user : collectionVO.getUsers()) {
             System.out.println(user);
         }
         return "index";
    }

    3)Map集合: Map集合中封装的是 Map<String,User> maps

    form表单:

    <h1>接受Map集合泛型String,User类型参数到Controller</h1>
    <form action="/param/test6" method="post">
         maps:<input type="text" name="maps['123'].id"/>
         maps:<input type="text" name="maps['456'].id"/>
         maps:<input type="text" name="maps['789'].id"/>
         <input type="submit" value="提交"/>
    </form>

    controller: 

    /**
     * map集合参数接受  Map<String,User> maps
     */
    @RequestMapping("/test6")
    public String test5(CollectionVO collectionVO){  
        for (Map.Entry<String, User> map : collectionVO.getMaps().entrySet()) {
             System.out.println(map.getKey()+"   ------>"+map.getValue());
        }
            
         return "index";
    }

    SpringMVC默认支持的参数类型

    @RequestMapping(value="/list")
    public String list(HttpSession session, HttpServletRequest request, 
                       HttpServletResponse response, Model model ){
         //获取请求协议
         String protocol = request.getProtocol();
         return "customer";
    }

     请求参数的名称问题

     SrpingMVC可以直接接受请求参数, 但是页面上的input的name值必须和controller中的参数名称或者vo中的参数名称相同,不然无法封装, 可以使用@RequestParameter注解后, 控制层名称可以任意.

    页面:

    <h1>接受对象类型参数到Controller</h1>
    <form action="/param/test1" method="post">
        用户编号:<input type="text" name="id"/>
        用户名:<input type="text" name="name"/>
        <input type="submit" value="提交"/>
    </form>

    controller

    //使用了@RequestParam("id"),方法中的参数名称可以任意,如idxxx
     @RequestMapping("/test1")
    public String test1(@RequestParam("id") String idxxx, String name){
          
          return "success";
    }
  • 相关阅读:
    软件杯华为ModelArts平台
    软件杯第一阶段博客
    《系统架构》阅读笔记05
    第11周周博客
    测试面试题
    杭电2014 (第一次用vector ac题目)
    杭电 2013 猴子吃桃 递归解法&循环解法
    杭电2012 质数问题
    杭电2629 Identity Card
    杭电1170
  • 原文地址:https://www.cnblogs.com/guo-rong/p/9197775.html
Copyright © 2020-2023  润新知