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 )));