Java EE using JSON data __js

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.