SpringMvc接收参数整理

1、表单提交

使用到的form表单和实体类文件

<form action="${pageContext.request.contextPath}/test" method="get">
    <input type="text" name="name">
    <input type="submit" value="提交">
</form>
<form action="${pageContext.request.contextPath}/test" method="post">
    <input type="text" name="name">
    <input type="submit" value="提交">
</form>
package com.xjj.entity;

public class User {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

1、Get方式提交

1、使用基本数据类型直接接收(可直接接收中文)
@RequestMapping("/test")
public void test(String name){
    
}
2、使用request接收(可直接接收中文)
@RequestMapping("/test")
public void test(HttpServletRequest request) {
    System.out.println(request.getParameter("name"));   
}
3、创建java对象(保证页面的参数name和对象属性名字一样)(可直接接收中文)
@RequestMapping("/test")
public void test(User user) {
    System.out.println(user.getName());
}
4、使用Map来接收参数(可直接接收中文)
@RequestMapping("/test")
public void test(@RequestParam Map<String,Object> map) {
    System.out.println(map.get("name"));
}

2、Post方式提交

1、使用基本数据类型直接接收(中文需转码)
@RequestMapping("/test")
public void test(String name) throws UnsupportedEncodingException {
    System.out.println(name);//可直接获取
    System.out.println(new String(name.getBytes("ISO-8859-1"),"utf-8"));   //避免中文乱码 
}
2、使用request进行接收(中文需转码)
@RequestMapping("/test")
public void test(HttpServletRequest request) throws UnsupportedEncodingException {
    System.out.println(request.getParameter("name"));//可直接获取
    System.out.println(new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8"));//避免中文乱码
}
3、创建java对象来接收(中文需转码中文需转码)
@RequestMapping("/test")
public void test(User user) throws UnsupportedEncodingException {
    System.out.println(user.getName());//可直接获取
    System.out.println(new String(user.getName().getBytes("ISO-8859-1"),"utf-8"));//避免中文乱码
}
4、使用Map来接收(中文需转码)
@RequestMapping("/test")
public void test(@RequestParam Map<String,Object> map) throws UnsupportedEncodingException {
    System.out.println(map.get("name"));//可直接获取
    System.out.println(new String(map.get("name").toString().getBytes("ISO-8859-1"),"utf-8"));//避免中文乱码
}

2、ajax

使用到的js代码

<!--普通方式post-->
<script>
    $.ajax({
        type:"post",
        url:"${pageContext.request.contextPath}/test",
        data:{
            name:"中文",
        },
        text:"json",
        success:function(data){

        }
    });
</script>
//使用JSON.stringify改变data从对象到字符串。需要引入依赖
    //json的依赖
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.3</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.3</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.3</version>
    </dependency>
    <!--json额外处理的依赖,不加可能会报错误-->
    <dependency>
      <groupId>net.sf.json-lib</groupId>
      <artifactId>json-lib</artifactId>
      <version>2.4</version>
      <classifier>jdk15</classifier>
    </dependency>
    <!--处理json为javabean格式时的依赖-->
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-core-asl</artifactId>
      <version>1.9.2</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.2</version>
    </dependency>
//js代码
<script>
    $.ajax({
        type:"post",
        url:"${pageContext.request.contextPath}/test",
        data:JSON.stringify({
            name:"中文"
        }),
        dataType : 'json',
        contentType : 'application/json;charset=UTF-8',
        success:function(data){
            alert("返回成功");
        }
    });
</script>
//使用formdata的方式提交,需要添加依赖,并创建文件上传的bean
    //依赖
    <!--文件上传依赖-->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>
	//需要创建的bean
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="54000000"/>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
//js代码
<script>
    var 
     $.ajax({
           type:"post",
           url:"${pageContext.request.contextPath}/test",
           data:formdata,
           contentType:false,
           processData:false,
           success:function(data){
               alert(data);
               return loadPage('${pageContext.request.contextPath}/goods/findGoodsByGoodsType/1');
           }
        });
</script>

1、普通的ajax方式(POST和GET的情况一样,所以合并为一项)

1、直接使用变量名字来借收(可直接接收中文)
 @RequestMapping("/test")
    public void test(String name) {
        System.out.println(name);
    }
2、使用request进行接收(可直接接收中文)
@RequestMapping("/test")
public void test(HttpServletRequest request) {
    System.out.println(request.getParameter("name"));
}
3、使用java对象来接收(可直接接收中文)
@RequestMapping("/test")
public void test(user user) {
    System.out.println(user.getName();
}
4、使用Map来接收(可直接接收中文)
@RequestMapping("/test")
public void test(@RequestParam Map<String,Object> map) {
    System.out.println(map.get("name"));
}

2、改用JSON.stringify的方式传递(只成功了POST,GET全部失败,因为get请求中不能带有{}这种特殊字符)

1、使用@ResponseBody来接收并注入到对象中(可直接接收中文)
@RequestMapping("/test")
public void test(@RequestBody user user) {
    System.out.println(user.getName());
}
2、使用@ResponseBody来接收成为一个字符串(可直接接收中文)
@RequestMapping("/test")
public void test(@RequestBody String name) {
    System.out.println(name);//获得的结果为:{"name":"中文"}
}

3、使用formdata提交数据

1、使用参数名直接获取(可直接获取中文)
@RequestMapping("/test")
public void test(String name)  {
    System.out.println(name);
}
2、使用request获取(可直接获取中文)
@RequestMapping("/test")
public void test(HttpServletRequest request)  {
    System.out.println(request.getParameter("name"));
}
3、使用java对象来接收(可直接获取中文)
@RequestMapping("/test")
public void test(User user)  {
    System.out.println(user.getName());
}
4、使用Map来接收(可直接获取中文)
@RequestMapping("/test")
public void test(@RequestParam Map<String,Object> json)  {
    System.out.println(json.get("name"));
}

3、直接使用地址来传参

1、使用@PathVariable(可接收中文,但是不可以附带’.’)
@RequestMapping("/test/{name}")
public void test(@PathVariable(name = "name")String name) {
    System.out.println(name);
}
"/test")
public void test(@RequestParam Map<String,Object> json)  {
    System.out.println(json.get("name"));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值