@RequestBody can only be used with @RequestHeader and cannot be used with @RequestParam (some methods of the iterative function are called only once, and then passed on)

@RequestBody can only be used with @RequestHeader and cannot be used with @RequestParam (some methods of the iterative function are only called once, and then passed on)

Example:

Some methods of the iterative function are only called once, and then passed on. You need to pass some parameters that @controller does not have in the foreground, and ensure that the foreground (jsp) can call this method. Use required=false, @RequestBody passes list, and @RequestHeader makes up for the inability to simultaneously

Use @RequestParam

 

 

@RequestBody can only be used with @RequestHeader and cannot be used with @RequestParam

 

@RequestParam is used with model-driven

 

@RequestBody is used in conjunction with @RequestHeader to transfer streams (pictures, lists, etc.). The priority of the stream is higher than that of ordinary attributes (@RequestParam). After the stream is taken, Ma Shan will close reuqest, so @RequestParam cannot be retrieved and passed. the value of

@RequestHeader is used to make up for the situation where @RequestParam cannot be used when using streams

 

Note the @RequestBody(required=false) attribute

 The essence of all values ​​request.getparamnter(""); just use tags such as @ to help you out

see

http://blog.csdn.net/kobejayandy/article/details/12690161

http://blog.sina.com.cn/s/blog_46debefc0101192j.html

http://www.cnblogs.com/chenxizhang/archive/2010/02/28/1675259.html

 

 

How to set header in ajax

 beforeSend: function(xhr){

        xhr.setRequestHeader('id', id);

        xhr.setRequestHeader('leave', leave);

        xhr.setRequestHeader('roleId', roleId);

        xhr.setRequestHeader('pId', pId);

        },

 

 

 

function appendTable() {

var roleId=$("input[name='roleId']").val();

var leave=$("input[name='leave']").val();

var pId=$("input[name='pId']").val();

var id=$("input[name='id']").val();

var dataParent = {

roleId: "1",

leave:"2",

pId:"3",

id:"5"

 

   };

$.ajax({

        type: "post",

        url: "${pageContext.request.contextPath}/system/opmRole/Auths",

        //data: dataParent,

       // headers:"{\"id\": \"1\",\"leave\":\"2\",\"roleId\":\"3\",\"pId\":\"5\"}",

        beforeSend: function(xhr){

        xhr.setRequestHeader('id', id);

        xhr.setRequestHeader('leave', leave);

        xhr.setRequestHeader('roleId', roleId);

        xhr.setRequestHeader('pId', pId);

        xhr.setRequestHeader('fg', "1");

        },

        //dataType: 'json',

        //contentType: "application/json",

        success: function (data) {

        if(data!=''&&data!=null){

        var div1=$("#div1");

        var table1=$("<table></table>");

        var arr = eval (data);

        for(j=0;j<arr.length;j++){

        if(arr[j].leave=='1'){

        var row =$("<tr></tr>");

        var td =$("<td></td>");

        td.append($(arr[j].str));

        row.append(td);

        table1.append(row);

        }

        var row2;

        if(arr[j].leave=='2'){

        row2 =$("<tr></tr>");

        var td =$("<td></td>");

        td.append(arr[j].str);

        row2.append(td);

        table1.append(row2);

        }if(arr[j].leave=='3'){

        var td =$("<td></td>");

        td.append(arr[j].str);

       

        table1.find("tr:last").append(td);

        }

        }

        div1.html(table1);

        }else{

        alert("The member number you entered does not exist!");

        }

       

        },

        error:function(data){

        alert("Data loading exception!");

       

        }

    }); 

 

 

}

 

 

 

@SuppressWarnings("rawtypes")

@RequestMapping(value="/system/opmRole/Auths")

@ResponseBody

public JSONArray treeRead(@RequestHeader(value="fg") String fg,@RequestHeader(value="id") String id,@RequestHeader(value="leave") String leave,@RequestHeader(value="roleId") String roleId,@RequestHeader(value="pId")  String pId,@RequestHeader(value="groupId",required=false)  String groupId1,@RequestBody(required=false) List<OpmRolelimit> o){

List<String> strl= new ArrayList<String>();

String str="";

JSONArray JsonArray = new JSONArray();

Map<String,Object> param = new HashMap<String,Object>();

param.put("pId", pId);

List<Map> map= opmMenufolderService.getfordMenuLimt(param);

if("1".equals(fg)&&id!=null&&!"".equals(id)){ // Check only once in the iteration, then pass in

//Query the role permission table

System.out.println("==========Query role permission table=======");

OpmRolelimit oo =new OpmRolelimit();

oo.setRoleid(id);

o = opgRoleLimitService.getRoleLimt (oo);

fg="0";

}

Map<String,Object> param1 = new HashMap<String,Object>();

String groupId=groupId1;

for(Map p: map){

 

param1.put("pId", p.get("ID"));

String l= (String) p.get("LEAVE");

if("1".equals(l)){

if("100000".equals(p.get("PARENTID")+"")){

groupId=(String)p.get("ID");

   str+="<label style=\"float:left\"><input type=\"checkbox\" class=\"checkboxCtrl\" value="+p.get("ID")+" group="+groupId+" selectType=\"invert\" />"+p.get("NAME")+"</label>"+"\n";

   strl.add(str);

   JSONObject Json = new JSONObject();

   Json.put("ID", p.get("ID")+"");

   Json.put("NAME", p.get("NAME")+"");

   Json.put("groupId", groupId);

   Json.put("parentId", p.get("PARENTID"));

   Json.put("leave", l);

   Json.put("str", "<label style=\"float:left;width:150px;\"><input type=\"checkbox\" class=\"checkboxCtrl\" value="+p.get("ID")+"  onclick=\"checkedAllBox(this);\" group="+groupId+" //>"+p.get("NAME")+"</label>"+"\n");

   JsonArray.add(Json);

   

   

}else{

str+="<label><input type=\"checkbox\" value="+p.get("ID")+" name="+groupId+"/>"+p.get("NAME")+"</label>"+"\n";

strl.add(str);

JSONObject Json = new JSONObject();

   Json.put("ID", p.get("ID")+"");

   Json.put("NAME", p.get("NAME")+"");

   Json.put("groupId",groupId);

   Json.put("parentId", p.get("PARENTID"));

   Json.put("leave", l);

   Json.put("str","<label style=\"width:150px;\">   "+p.get("NAME")+"</label>"+"\n");

   JsonArray.add(Json);

}

}

if("2".equals(l)){

if(Integer.valueOf(leave)<0){

if(Integer.valueOf(p.get("LEAVE1")+"") >=Integer.valueOf(leave)){

str+="<label><input type=\"checkbox\" value="+p.get("ID")+"  name="+groupId+"/>"+p.get("NAME")+"</label>"+"\n";

strl.add(str);

JSONObject Json = new JSONObject();

   Json.put("ID", p.get("ID")+"");

   Json.put("NAME", p.get("NAME")+"");

   Json.put("groupId", groupId);

   Json.put("parentId", p.get("PARENTID"));

   Json.put("leave", l);

   String bs ="<label style=\"width:150px;\">      <input type=\"checkbox\" value="+p.get("ID")+"  name='"+groupId+"'/>"+p.get("NAME")+"</label>"+"\n";

   if(o!=null){

   for(OpmRolelimit k : o){

   if( (p.get("ID")+"").equals(k.getMenuitemid())){

   bs ="<label style=\"width:150px;\">      <input type=\"checkbox\" value="+p.get("ID")+" checked=\"checked\"  name='"+groupId+"'/>"+p.get("NAME")+"</label>"+"\n";

       break;

   }

   }

   }

   Json.put("str",bs);

   JsonArray.add(Json);

}

}else{

//EVERYTHING

Map<String,Object> param2 = new HashMap<String,Object>();

param2.put("menuItemId", p.get("ID"));

param2.put("menuItemLimitId", null);

param2.put("roleId", roleId);

param2.put("displayOrder", Integer.valueOf(-1));

List <OpmRolelimit> rl = opgRolelimitService.getRoleLimitByTJ (param2);

if(rl!=null&&rl.size()>0&&Integer.valueOf(p.get("LEAVE1")+"") >=Integer.valueOf(leave)){

str+="<label><input type=\"checkbox\" value="+p.get("ID")+" name="+groupId+"/>"+p.get("NAME")+"</label>"+"\n";

strl.add(str);

JSONObject Json = new JSONObject();

   Json.put("ID", p.get("ID")+"");

   Json.put("NAME", p.get("NAME")+"");

   Json.put("groupId", groupId);

   Json.put("parentId", p.get("PARENTID"));

   Json.put("leave", l);

//    Json.put("str","<label style=\"width:150px;\">      <input type=\"checkbox\" value="+p.get("ID")+" name='"+groupId+"'/>"+p.get("NAME")+"</label>"+"\n");

   String bs ="<label style=\"width:150px;\">      <input type=\"checkbox\" value="+p.get("ID")+"  name='"+groupId+"'/>"+p.get("NAME")+"</label>"+"\n";

   if(o!=null){

   for(OpmRolelimit k : o){

   if( (p.get("ID")+"").equals(k.getMenuitemid())){

   bs ="<label style=\"width:150px;\">      <input type=\"checkbox\" value="+p.get("ID")+" checked=\"checked\"  name='"+groupId+"'/>"+p.get("NAME")+"</label>"+"\n";

       break;

   }

   }

   }

   Json.put("str",bs);

   JsonArray.add(Json);

}

}

}

if("3".equals(l)){

Map<String,Object> param2 = new HashMap<String,Object>();

param2.put("menuItemId", p.get("PARENTID"));

param2.put("menuItemLimitId", p.get("ID"));

param2.put("roleId", roleId);//Current login user role Id

param2.put("displayOrder", null);

List <OpmRolelimit> rl2 = opgRolelimitService.getRoleLimitByTJ (param2);

String tempg=groupId;

if(rl2!=null&&rl2.size()>0){

str+="<label><input type=\"checkbox\"  value="+p.get("ID")+" name="+groupId+"/>"+p.get("NAME")+"</label>"+"\n";

strl.add(str);

JSONObject Json = new JSONObject();

   Json.put("ID", p.get("ID")+"");

   Json.put("NAME", p.get("NAME")+"");

   Json.put("groupId", groupId);

   Json.put("parentId", p.get("PARENTID"));

   Json.put("leave", l);

//    Json.put("str","<label style=\"width:150px;\"><input type=\"checkbox\"  value="+p.get("ID")+" name='"+groupId+"'/>"+p.get("NAME")+"</label>"+"\n");

   String bs ="<label style=\"width:150px;\"><input type=\"checkbox\"  value="+p.get("ID")+" name='"+groupId+"'/>"+p.get("NAME")+"</label>"+"\n";

   if(o!=null){//Reverse display of permission tree

   for(OpmRolelimit k : o){

   if( (p.get("ID")+"").equals(k.getMenuitemlimitid())){

   bs ="<label style=\"width:150px;\"><input type=\"checkbox\"  value="+p.get("ID")+" checked=\"checked\" name='"+groupId+"'/>"+p.get("NAME")+"</label>"+"\n";

       break;

   }

   }

   }

   Json.put("str",bs);

   JsonArray.add(Json);

}

}

 

//str+=treeRead(leave,roleId,p.get("ID")+"",groupId);

JsonArray.addAll(treeRead(fg,id,leave,roleId,p.get("ID")+"",groupId,o));

}

return JsonArray;

}

 

 

==========================================================

@RequestBody

This annotation is often used to deal with Content-Type: not application/x-www-form-urlencoded encoded content, such as application/json, application/xml, etc.;

It parses the post data body by using the HttpMessageConverters configured by the HandlerAdapter, and then binds to the corresponding bean.

Because it is configured with FormHttpMessageConverter, it can also be used to process the content of application/x-www-form-urlencoded. The processed result is placed in a MultiValueMap<String, String>. This situation is used under some special requirements. Details Check out the FormHttpMessageConverter api;

Sample code:

[java] view plaincopy

@RequestMapping(value = "/something", method = RequestMethod.PUT)  

public void handle(@RequestBody String body, Writer writer) throws IOException {  

  writer.write(body);  

}  

 

@RequestParam 

A) It is often used to deal with the binding of simple types, and the String obtained through Request.getParameter() can be directly converted to a simple type ( String--> The conversion of simple types is done by the converter configured by ConversionService); because the use of The request.getParameter() method obtains parameters, so it can process the value of queryString in the get method and the value of the body data in the post method;

B) Used to process Content-Type: the content encoded for application/x-www-form-urlencoded, the submission method is GET, POST;

C) The annotation has two attributes: value and required; value is used to specify the id name of the value to be passed in, and required is used to indicate whether the parameter must be bound;

Sample code:

 

 

@RequestHeader("userId") Long userId, @RequestBody CaseCommitBean caseCommitBean, can be used together

However, @RequestParam cannot be used together with @RequestBody and cannot be used at the same time. If it is in the form of a stream (such as json), it can only be read once, and a stream close exception will occur. . . So adopt a one-way approach

The surface is the view of error/defaulterror, but when the content is wrong 

 

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

 

Because there is no problem with getting data with Get, now I use post to get in and out of json. There has always been a problem,

It took a long time to find

 

function adduser(){

                 var url=".."+"/userses";

                $.ajax({

                  type: "POST",

                  async: false,

                  url:url +"/add",

                  dataType:"html",

                  contentType: "application/json",

                  data:JSON.stringify({

                      loginName:        'loginName',

                      password:            'password',

                      userId:            'userId',

                      email:            'email',

                      }),

                  success:function(data){

                     

                       var jsonstr = eval_r (data);

                       

                  }

           });

 

 

By entering the debugging of spring web, you can probably know the internal things. Spring is indeed a container that brings together many content ideas. Ha ha, you can do it too. I guessed that the weak reference used internally by hibernate was also used by spring mvc when debugging.

 

 

@RequestMapping(value = "/add", method = RequestMethod.POST  )

    @ResponseBody

    public Map<String,String> addUsers(

            //HttpEntity< User> user,

            @RequestBody User user2

            //@RequestParam(value = "loginName", required = true)String loginName,

            //@RequestParam(value = "password", required = true)String password,

            //@RequestParam(value = "userId", required = true)String userId,

            //@RequestParam(value = "email", required = true)String email

            ){

       

        userservice.getUserById(user2.getUserId());

        Map<String,String> result = new HashMap<String,String>();

        result.put("result", "true");

       

        return result;

    }

 

Note that several of the above are mapped to the corresponding variables of the request content and cannot be used at the same time. If it is in the form of a stream and (such as json), it can only be read once, and a stream close exception will occur at that time. . . So adopt a one-way approach

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326542310&siteId=291194637