ajax 与 fastjson 在jsp页面 ssh框架

1:导入的jar包

fastjson-1.1.36.jar

2:页面端

$(function(){
     var loginUserId=${session.loginUser.id};
     //新消息提醒
     function message(){
    $.ajax({
  type: "POST",
  url: "${pageContext.request.contextPath }/client/cMessageAction_show.action",
  data:"loginUserId="+loginUserId+"",
  dataType:"json",
  success: function(msg){
    alert( "Data Saved: "+msg[0].time);
  },
  error:function(){
    alert("出错了");
  }
});
     }
       if(loginUserId!=""){
    setInterval(message, 5000); 
 
     });
   

3后台端

  在struts2中

List list=messageService.findAllMessage(loginUserId);

SimplePropertyFilter filter=new SimplePropertyFilter();
String s=JSON.toJSONString(list,filter);


    HttpServletResponse response = ServletActionContext.getResponse();  
    response.setCharacterEncoding("UTF-8");  
PrintWriter out = response.getWriter();
if(out!=null){
             out.print(s);
         out.flush();
         out.close();
      }

SimplePropertyFilter类:

import com.alibaba.fastjson.serializer.PropertyFilter;


import org.hibernate.collection.PersistentCollection;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
public class SimplePropertyFilter implements PropertyFilter {


@Override
public boolean apply(Object object, String name, Object value) {
if (value instanceof HibernateProxy) {//hibernate代理对象
     LazyInitializer initializer = ((HibernateProxy) value).getHibernateLazyInitializer();
     if (initializer.isUninitialized()) {
       return false;
     }
   } else if (value instanceof PersistentCollection) {//实体关联集合一对多等
     PersistentCollection collection = (PersistentCollection) value;
     if (!collection.wasInitialized()) {
       return false;
     }
     Object val = collection.getValue();
     if (val == null) {
       return false;
     }
   }
   return true;
}
}




你可能感兴趣的:(ajax 与 fastjson 在jsp页面 ssh框架)