What is the use of JSON data flow in Java EE?
1 JSP pass value to action
2 action in the processing of the value of the JSP, after processing, processed into a JSON-formatted value
3 returns the JSP. The data that is processed in the JSP can be obtained by the. Gets the value in the form of a key value pair.
JSP
$ (' select[name=classes '). Change (function () {
var classnum= $ (' select[name=classes option:selected '). Val ();
$.getjson (
"Stu/stu_toshowstu",
{Classnum:classnum},
function (Json,status)
{
$.each (Json,function (i) {
$ (' #stuDiv '). Append ("<div> Student study Number:" +json[i].num+ "</div>");
}
);
});
Action
Private String Classnum;
.........................................
Public String Toshowstu ()
{
list<student> Lststu = Stuservice.getstubyclassnum (classnum);
This.outputjson (Lststu);
return null;
}
Outputjson
Protected final void Outputjson (Object obj)
{
String outstr = "";
if (obj instanceof String)
outstr = (String) obj;
Else
OUTSTR =Getjson (obj);
Try
{
HttpServletResponse response = Servletactioncontext.getresponse ();
Response.setcharacterencoding ("UTF-8");
Response.setcontenttype ("Text/html;charset=utf-8");
Response.getwriter (). print (OUTSTR);
}
catch (IOException E)
{
Logger.error (E.getmessage (), E);
E.printstacktrace ();
}
}
Protected final String Getjson (Object obj)
{
Jsonconfig cfg = jsoncfg ();
return Jsonarray.fromobject (obj, cfg). toString ();
}
Private Jsonconfig jsoncfg ()
{
Jsonconfig cfg = new Jsonconfig ();
Cfg.registerjsonvalueprocessor (Java/util/date, New Jsonvalueprocessorimpl ());
return CFG;
}
Student itself has num this field, processing data objects into JSON format data, which has [{"num": "" "," Name ":" 222 "," Address ":" Jiangsu "}] and so on.
Of course, we must guide the bag. See: http://blog.csdn.net/woshixuye/article/details/7244318