此代码用于添加留言
后台action代码
public void lawyerreply(){
try{
response.setCharacterEncoding("UTF-8");
String message="";
java.util.Date date=new java.util.Date();
Integer ids=Integer.valueOf(request.getParameter("id"));
Integer addUserIdString=(Integer)request.getSession().getAttribute("userid");
String addmessage=request.getParameter("message");
if(addmessage==null||"".equals(addmessage.trim())){
message="留言内容不能为空";
this.response.getWriter().print(message);
return ;
}
Timestamp time=new Timestamp(date.getTime());
model.setAddTime(time);
model.setRepBody(addmessage);
model.setLawyeId(ids);
model.setAddUser(addUserIdString);
model.setId(null);
lawyeReplyservice.lawyereplyAdd(model);
count=lawyeReplyservice.count("lawyeId="+ids.intValue());
message="添加留言成功!";
String data="{message:\""+message+"\", count:\""+count+"\"}";
//
this.id = String.valueOf(ids);
this.response.getWriter().print(data);
}catch (Exception e) {
e.printStackTrace();
}
}
前台解析json代码
function addMessage(userId){
var addmessage=$("#repBody").val();
//alert(message);加判断
$.ajax({
type: "POST",
url: "lawyerreply.action",
data: "id="+userId+"&message="+addmessage,
success: function(msg){
//对msg解析 得到count 和message
var date=eval("("+msg+")");
//1修改count数 ,取得count数,设置页面上的值
$("#countPersonNum").html(date.count);
//2 提示后台返回信息 message
alert( "温馨提示: " + date.message );
$("#repBody").html("");
}
});
}