SpringMVC获取客户端数据几种方式

南楼画角 提交于 2020-01-08 21:28:50

作者:gqk


 本章目标:

  •  简单数据类型赋值

  •  对象属性赋值

  • 数组赋值

  • restful风格

  • 日期赋值

简单数据类型赋值

通过@RequestParam对简单类型的参数进行绑定。

@RequestMapping("/test1")
    public String test1(@RequestParam("name")String name,

                              @RequestParam("pwd")String pwd,

                              @RequestParam(defaultValue="110")int age){
        System.out.print("name===="+name);
        System.out.print("pwd===="+pwd);
        System.out.print("age===="+age);
        return "hello";

    }

 <!-- 解决post传入中文乱码问题 -->获取参数如果乱码可以在web.xml中配置过滤器

<filter>

        <filter-name>characterEncodingFilter</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>

    </filter>

    <filter-mapping>

对象属性赋值

页面中input的name和controller的pojo形参中的属性名称一致,将页面中数据绑定到pojo对应的属性。

  @RequestMapping("/test2")
    public String test2(User user){
        System.out.print(user);
        return "hello";
    }

数组赋值

1  @RequestMapping("/test3")
2     public String test3(Integer[]ids ){
3         System.out.print(ids);
4         return "hello";
5     }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!