欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

@RequestBody 请求实体注解

程序员文章站 2022-03-20 08:15:41
...

User实体类:

package com.htxx.entity.business;

public class User {
   private String name;
   private String pwd;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getPwd() {
	return pwd;
}
public void setPwd(String pwd) {
	this.pwd = pwd;
}
   
}

jsp:

     function myfunction() {
    	 var name=$("#username").val();
    	 var pwd=$("#password").val();
    <%----%>	 var data= {
                 "name": name,
                 "pwd": pwd
             };
         $.ajax({
				url:"rpc/rb",
				data:JSON.stringify(data),
				//data:{name:name,pwd:pwd},
				type: 'POST', //必须是POST提交
				dataType: "json",
				contentType:'application/json;charset=UTF-8',  //这句不能少,要不然报415错误
				success:function(result){

				},
				error:function(){
					alert("操作失败");
					return false;
				}
			});
    }  
后台:

	@RequestMapping(value="/rb")
	public void  rb( @RequestBody User user){
        System.out.println("name="+user.getName());
        System.out.println("pwd="+user.getPwd());
	}

效果演示:

@RequestBody 请求实体注解

@RequestBody 请求实体注解

相关标签: spring requestbody