1: The need to import the jar
commons-beanutils-1.8.3.jar
commons-collections-3.2.1
commons-lang-2.5
commons-logging-1.1.1
ezmorph-1.0.6
json-lib-2.3-jdk15
Note: If you import imported json-lib-2.3-jdk15 but also reported net.sf.json.JSONArray.class not find the words, need to re-publish to tomcat
2: page end
<script type="text/javascript">
$(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 ( "wrong");
}
});
}
if(loginUserId!=""){
setInterval(message, 5000);
}
});
</script>
3: struts2 end
List<Message> list=messageService.findAllMessage(loginUserId);
// cascade, can not be directly transformed into the map inside to remove the List
JsonConfig cfg = new JsonConfig();
// filter association, to avoid infinite loop net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
cfg.setJsonPropertyFilter(new PropertyFilter()
{
public boolean apply(Object source, String name, Object value) {
// filter out as to what to attribute the user will not be put in json
if(name.equals("user")||name.equals("id")) {
System.out.println(name+"--");
return true;
} else {
System.out.println(name+"==");
return false;
}
}
});
//net.sf.json.JSONException: java.lang.reflect.InvocationTargetException abnormal
cfg.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
// javabean in the cycle call occurs quickly with a parent or excludes kill children
// cfg.setExcludes(new String[]{"addressGroup"});
//net.sf.json.JSONException: java.lang.reflect.InvocationTargetException date format conversion error
cfg.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
// cfg.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd hh:mm:ss"));
JSONArray json=new JSONArray().fromObject(list,cfg);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
if(out!=null){
out.print(json.toString());
out.flush();
out.close();
}