Spring MVC各種提交形式以及參數(shù)接收(form表單提交以及Json提交)

一晃就到了11月30號(hào)了,明天就是2018年的最后一個(gè)月份了,時(shí)間呀!?。。?/p>

form表單參數(shù)接收的三種方式:

  1. HttpServletRequest
    @RequestMapping(value = "/getParmByReq.do", method = RequestMethod.POST)
    @ResponseBody
    public String getParmByReq(HttpServletRequest request) {
        String name = request.getParameter("name");
        int age = Integer.parseInt(request.getParameter("age"));
        System.out.println("接收到的參數(shù),name=" + name + ";age=" + age);
        return "接收到的參數(shù),name=" + name + ";age=" + age;
    }

用curl 測(cè)試結(jié)果: (-d 表示post請(qǐng)求)

curl   -d "name=admin&age=34"   http://localhost:8080/ssm-req/form/getParmByReq.do

在這里插入圖片描述
3. 一個(gè)個(gè)參數(shù)接收,單個(gè)參數(shù)按照名稱接收:

  @RequestMapping(value = "/requestParam.do", method = RequestMethod.POST)
    @ResponseBody
    public String getBarBySimplePathWithRequestParam(
            @RequestParam(value = "name") String name,
            @RequestParam(value = "age") Integer age
    ) {
        System.out.println("接收到的參數(shù),name=" + name + ";age=" + age);
        return "接收到的參數(shù),name=" + name + ";age=" + age;
    }

用curl 測(cè)試結(jié)果:

curl   -d "name=ceshi&age=22"   http://localhost:8080/ssm-req/form/requestParam.do

在這里插入圖片描述
4. 自定義實(shí)體類接收,使用注解:@ModelAttribute

  @RequestMapping(value = "modelAttribute.do", method = RequestMethod.POST)
    @ResponseBody
    public String getModelAttribute(@ModelAttribute() User user) {
        System.out.println("接收到的參數(shù),name=" + user.getName() + ";age=" + user.getAge());
        return "接收到的參數(shù),name=" + user.getName() + ";age=" + user.getAge();
    }

用curl 測(cè)試結(jié)果:

curl   -d "name=jaywei&age=33"   http://localhost:8080/ssm-req/form/modelAttribute.do

在這里插入圖片描述
5. 不用流的形式接收

    @RequestMapping(value = "/getNotStream.do")
    @ResponseBody
    public String getNotStream(String order) {
        JSONObject jsonObject = JSONObject.parseObject(order);

        System.out.println("接收到的參數(shù),name=" + jsonObject.getString("name") + ";age=" + jsonObject.getString("age"));
        return "接收到的參數(shù),name=" + jsonObject.getString("name") + ";age=" + jsonObject.getString("age");
    }

用curl 測(cè)試結(jié)果

curl   -d "order={'name':'張三','age':12}"   http://localhost:8080/ssm-req/form//getNotStream.do

在這里插入圖片描述

小結(jié):
推薦使用:@ModelAttribute 接收參數(shù)的方式。
PS: 解決中文亂碼問題,在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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

JSON提交參數(shù)接收

使用@RequestBody注解,該注解是將參數(shù)放在請(qǐng)求體中

  @RequestMapping(value = "getJson2.do",produces = "application/json")
    public String getJson2(@RequestBody String userStr) {
        User user = JSON.parseObject(userStr, User.class);
        System.out.println("接收到的參數(shù),name=" + user.getName() + ";age=" + user.getAge());
        return JSON.toJSONString("接收到的參數(shù),name=" + user.getName() + ";age=" + user.getAge());
    }

用curl測(cè)試結(jié)果

curl   -H "Content-Type:application/json" -X POST -d "{'name':'sange','age':12}"   http://localhost:8080/ssm-req/json/getJson2.do

在這里插入圖片描述

源碼地址:
https://github.com/XWxiaowei/JavaWeb/tree/master/ssm-request-demo


作者:碼農(nóng)飛哥

微信公眾號(hào):碼農(nóng)飛哥