Struts2 does not use a plug-in to return json data

Source: Internet
Author: User

Struts2 does not use a plug-in to return json data
First, we need to obtain the HttpServletResponse object first. Here we will not detail how to obtain the object in struts2. There are several methods not available for Baidu.
I am using
HttpServletResponse response = (HttpServletResponse) ActionContext. getContext (). get (ServletActionContext. HTTP_RESPONSE );
// The following two sentences are critical. The first sentence that is not added may cause the front end to be unidentifiable.
// Garbled characters may occur if the second sentence is not added
Response. setContentType ("text/json ");
Response. setCharacterEncoding ("UTF-8 ");
Try {
Out = response. getWriter ();
} Catch (IOException e ){
E. printStackTrace ();
}

GetBookInfoByPageNum gbip = new GetBookInfoByPageNum ();
List Bookinfos = gbip. getByPage (page, anywords );

// Here I use fastjson to convert data from List to Json
Out. print (JSON. toJSONString (bookinfos, true )));

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.