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

ajax+springmvc实现C与View之间的数据交流方法

程序员文章站 2022-03-31 14:07:44
jquery.post(url, [data], [callback], [type]) url,[data],[callback],[type]string,map,f...

jquery.post(url, [data], [callback], [type])

url,[data],[callback],[type]string,map,function,stringv1.0url:发送请求地址。

data:待发送 key/value 参数。

callback:发送成功时回调函数。

type:返回内容格式,xml, html, script, json, text, _default。

套用格式:

$.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><tr><td>"+time+"</td></tr></table>";
  appendbutton = appendbutton+table+"<button type = 'button' id = 'toaddcommentid' onclick = 'replacefrom("+parentid+",\""+name+"\""+")'>回复</button>";
  }
  appendbutton = appendbutton+"<button type = 'button' onclick = 'replacefrom("+parentid+","+"\""+username+"\""+")'>我也说一句</button>";
 }
 appendbutton = appendbutton+"<div id = 'commentbutton' ></div><div id = 'textareaid'></div>";
 if(data==""){
  appendbutton = appendbutton+"<textarea id='textareaid"+parentid+"' rows='2' cols='77' validate='required' validate-message='不能为空!' name = 'content' >@"+username+"...."+"...."+parentid+":</textarea><button type = 'button' id = 'commentcontentid' onclick = 'submit("+topicid+","+parentid+","+"\""+username+"\""+")'>发表</button>";
 }
 $("#addcommentid"+parentid).html(appendbutton);
 });

后台:

@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();
 }

注意,用springmvc3的注解@responsebody来传递参数。

经常用到的js函数:

上面由于使用json来传递的数据,而js解析json传过来的日期时,不是我们想要的格式,这时需要对日期进行操作:

首先传过去的日期将它设为time传过去 date.gettime()

然后再在js中操作:

var date= "/date("+time+")/";
date = dateformat(date);



/**
 * 处理时间
 * @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;
}

以上这篇ajax+springmvc实现c与view之间的数据交流方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。