Ajax + springmvc implements data exchange between C and View, ajaxspringmvc
JQuery. post (url, [data], [callback], [type])
Url, [data], [callback], [type] String, Map, Function, StringV1.0url: Send request address.
Data:The Key/value parameter to be sent.
Callback:Callback function when sending successfully.
Type:The returned content format is xml, html, script, json, text, and _ default.
Format:
$. Post ("test. php", function (data) {alert ("Data Loaded:" + data) ;}); $. get ("comment/getComments? ParentId = "+ parentId +" & topicId = "+ topicId, function (data) {var appendButton =" "; var append =" "; if (data! = "") {Var arr = data. split ("$"); var allTr = ""; for (var I = 0; I <arr. length; I ++) {var arr2 = arr [I]. split (','); var name = arr2 [3]; var content = arr2 [0]; var time = "/Date (" + arr2 [1] + ") /"; time = DateFormat (time); var id = arr2 [2]; var table = "<table> <tr> <td>" + content + "</td> </tr> <td>" + time + "</td> </tr> </table> "; appendButton = appendButton + table + "<button type = 'button 'id = 'toaddcommentid' oncli Ck = 'replace Rom ("+ parentId +", \ "" + name + "\" "+") '> reply </button> ";} appendButton = appendButton + "<button type = 'button 'onclick = 'replace Rom (" + parentId + "," + "\" "+ userName +" \ "+ ") '> let me also say </button> ";} appendButton = appendButton + "<div id = 'commentcall'> </div> <div id = 'textateaid '> </div>"; if (data = "") {appendButton = appendButton + "<textarea id = 'textareaid" + parentId + "'rows = '2' cols = '77 'validate = 'Requestred' validate-message = 'cannot be blank! 'Name = 'content'> @ "+ userName + ".... "+ ".... "+ parentId +": </textarea> <button type = 'button 'id = 'commentcontentid' onclick = 'submit ("+ topicId +", "+ parentId + ", "+" \ "" + userName + "\" "+") '> post </button> ";}$ (" # addCommentId "paiparentid=.html (appendButton );});
Background:
@RequestMapping(value = "/saveAndGetComments", params = {"topicId","parentId"}, method = RequestMethod.POST) @ResponseBody public String saveAndGetComments(long topicId,Comment comment,long parentId) throws UnsupportedEncodingException{ comment.setParentId(parentId); commentService.save(comment,topicId); List<Comment> comments=commentService.listByCommentId(parentId); return append(comments); } private String append(List<Comment> comments) { StringBuffer sb=new StringBuffer(); for(int i=0;i<comments.size();i++){ Comment comment = comments.get(i); sb.append(comment.getContent()); sb.append(","); sb.append(comment.getCreateTime().getTime()); sb.append(","); sb.append(comment.getId()); sb.append(","); sb.append(comment.getUser().getName()); if(i!=comments.size()-1){ sb.append("$"); } } return sb.toString(); }
Note:Use the annotation @ responseBody of springmvc3 to pass parameters.
Common js functions:
Because the above data is transmitted in json format, js parses the date passed in json instead of the desired format. In this case, you need to perform operations on the date:
First, set the past date to time and pass it to date. getTime ()
Then, perform the following operations in js:
Var date = "/Date (" + time + ")/"; date = DateFormat (date ); /*** processing time * @ param value * @ returns {String} */function DateFormat (value) {var date = new Date (parseInt (value. replace ("/Date (",""). replace (")/", ""), 10); var month = date. getMonth () + 1 <10? "0" + (date. getMonth () + 1): date. getMonth () + 1; var currentDate = date. getDate () <10? "0" + date. getDate (): date. getDate (); var Hours = date. getHours () <10? "0" + date. getHours (): date. getHours (); var Minutes = date. getMinutes () <10? "0" + date. getMinutes (): date. getMinutes (); var Seconds = date. getSeconds () <10? "0" + date. getSeconds (): date. getSeconds (); return date. getFullYear () + "/" + month + "/" + currentDate + "" + Hours + ":" + Minutes + ":" + Seconds ;}
The above ajax + springmvc method for data exchange between C and View is all the content shared by xiaobian. I hope to give you a reference, and I hope you can provide more support for helping customers.