ajax 与 fastjson 在jsp页面 ssh框架

前端之家收集整理的这篇文章主要介绍了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);
}
});
</script>

3后台

在struts2中

List<Message> 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; } }

原文链接:https://www.f2er.com/ajax/163477.html

猜你在找的Ajax相关文章