Java SSH Project Summary-Jquery LigerUI-Table Json conversion, jqueryligerui-
Summary
In the previous article, we introduced the page effects of LigerUI tables and how to load Json at the front end. Next we will look at how to process Json at the backend.
Process
Jump URL to action
In the previous article, we used the url "statisticalQuery_list.action" to jump to the list Method of the StatisticalQueryAction class, and how to jump from the front-end url to the list method of this action class. the configuration in the xml file is as follows:
<Pre name = "code" class = "html"> <! -- Statistics query --> <action name = "statisticalQuery _ *" class = "statisticalQueryAction" method = "{1}"> <result name = "tolist">/admin/jsp/ statisticalQuery/StatisticalQuery1.jsp </result> <result name = "forupdatelist">/admin/jsp/StatisticalQuery. jsp </result> <result name = "toDetail">/admin/jsp/StatisticalQuery/detailInfo. jsp </result> <interceptor-ref name = "checkAdminPrivilege"/> <interceptor-ref name = "defaultStack"/> </action>
StatisticalQueryAction class
Next, let's take a look at the methods used in this class:
// Foreground query condition private String keyWord; public String getKeyWord () {return keyWord;} public void setKeyWord (String keyWord) {this. keyWord = keyWord;}/***** @ MethodName: list * @ Description: obtains all data and displays * @ return * @ throws Exception */public String list () in the table () throws Exception {try {String page = ServletActionContext. getRequest (). getParameter ("page"); String pagesize = ServletActionContext. getRequest (). getParameter ("pagesize"); String strWhere = ""; int statNum = (Integer. parseInt (page)-1) * Integer. parseInt (pagesize) + 1; int endNum = Integer. parseInt (page) * Integer. parseInt (pagesize); if (StringUtils. isNotBlank (keyWord) {strWhere = java.net. URLDecoder. decode (keyWord, "UTF-8");} System. out. println (strWhere); // The data obtained from the view, which needs to be converted. // The List of query records <StatisticalQueryView> statisticalQueryViewsCount = statisticalQueryService. findAllBypage (strWhere); // query data List by page <StatisticalQueryView> statisticalQueryViews = statisticalQueryService. findAllBypage (statNum, endNum, strWhere); // converts a view set to a common list set and then to jsonList <StatisticalQuery> statisticalQueries = viewNormal (statisticalQueryViews ); // convert the entity set in the background to the special form jsonString resultJson = JsonUtils that can be received in the foreground table. toJsonGirdN (statisticalQueries, statisticalQueryViewsCount); System. out. println ("resultJson" + resultJson); outJson (ServletActionContext. getResponse (), resultJson);} catch (Exception e) {e. printStackTrace ();} return null;}/***** @ MethodName: viewNormal * @ Description: convert a view set to a common list set * @ param statisticalQueryViews * @ return */public List <StatisticalQuery> viewNormal (List <StatisticalQueryView> statisticalQueryViews) {// convert the View list set to a common set List <StatisticalQuery> statisticalQueries = new ArrayList <StatisticalQuery> (); StatisticalQuery statisticalQuery = null; try {for (int I = 0; I <statisticalQueryViews. size (); I ++) {statisticalQuery = new StatisticalQuery (); statisticalQuery = statisticalQueryViews. get (I ). getStatisticalQuery (); statisticalQueries. add (statisticalQuery) ;}} catch (Exception e) {e. printStackTrace ();} return statisticalQueries;}/***** @ Title: outJson * @ Description: output results in json format * @ param response * @ param result set file * @ return void return type * @ throws */private void outJson (HttpServletResponse response, String data) {response. setContentType (contentType); response. setCharacterEncoding (encoding); try {PrintWriter out = response. getWriter (); out. print (data); out. flush (); out. close ();} catch (Exception e) {System. out. println (e. getMessage ());}}
First, we will explain the above three methods. First, the first "list" method gets the conditions passed by the foreground through the dependency injection of the "keyWord" attribute, then, a list set of views is obtained through conditional query. Because of other special requirements on the foreground, the second method "viewNormal" is used to convert the list set of views into the desired set; the third method is to transmit Json data to the foreground.
Next we will focus on introducing how to convert an object set to a front-end Json that can be received. We call the toJsonGirdN method of the JsonUtils class to convert it. Gson is a Java class library provided by Google for ing between Java objects and JSON data.
Let's take a look at this class:
Package cn. bjjczb. ybyy. util; import java. lang. reflect. type; import java. util. hashMap; import java. util. list; import java. util. map; import org. omg. CORBA. PRIVATE_MEMBER; import cn. bjjczb. ybyy. domain. role; import com. google. gson. gson; import com. google. gson. jsonNull; import com. google. gson. reflect. typeToken; public class JsonUtils {private static Gson gson = new Gson (); private JsonUtils () {}/ ***** @ MethodName: toJsonGird * @ Description: convert the set to a json String in the form of a table required by the ligerUI foreground, and add * @ param list * @ return */@ SuppressWarnings ("unchecked") public static String toJsonGirdN (List list, list list2) {String result = null; try {// put the set into map and convert it into json. It is still not what we want, but it is very close to Map listMap = new HashMap (); listMap. put ("Rows", list); String json = gson. toJson (listMap); // System. out. println (json); // The result is: {"Rows": [{"a": "Test 1", "B": "Test 1 "}, {"a": "Test 2", "B": "test only 2" },{ "a": "Test 3", "B ": "Pure Test 3" },{ "a": "Test 3", "B": "Pure Test 3"}]} String total = String. valueOf (list2.size (); String totalReplace = "{\" Total \ ":" + total + ", \" Rows \ ""; result = json. replace ("{\" Rows \ "", totalReplace); // System. out. println (result);} catch (Exception e) {e. printStackTrace () ;}finally {return result ;}}}
In the above class, we first put the list object set we want to convert to json into map, and then convert map to Json using the toJson method of gson class, then, convert Json to get the Json data to be loaded in the front-end table.
Expansion
The following describes how to convert commonly used objects into Json and Json objects.
/***** @ MethodName: toJson * @ Description: converts an object to a JSON string. * This method can meet most of the requirements * @ param src: the Object to be converted * @ return: */public static String toJson (Object src) {if (src = null) {return gson. toJson (JsonNull. INSTANCE);} return gson. toJson (src);}/***** @ MethodName: fromJson1 * @ Description: converts a JSON string to an object, * This method cannot be used to convert a List * @ param <T> * @ param json * @ param classOfT * @ return */public static <T> Object fromJson1 (String json, class <T> classOfT) {return gson. fromJson (json, (Type) classOfT);}/*** test to convert a json string to an object. Conversion to a generic Type is not available * @ MethodName: test * @ Description: this method is used to test Role as the object */public void test () {Role role = new Role (); String json = ""; JsonUtils. fromJson1 (json, role. getClass ();}/***** @ MethodName: fromJson2 * @ Description: This method is used to convert a JSON string to an object, this method can be used to convert List * Type with generics to new TypeToken <List <T> ()*{}. getType (). Other classes can also be called using this method, * replace List <T> with the class you want to convert * @ param json * @ param typeOfT * @ return */public static Object fromJson2 (String json, Type typeOfT) {return gson. fromJson (json, typeOfT);}/*** test converting json to a generic object * @ MethodName: test1 * @ Description: this method is used to test the Role as the object */public void test1 () {Type roles = new TypeToken <List <Role> (){}. getType (); String json = ""; JsonUtils. fromJson2 (json, roles );}
The above methods convert objects into Json and convert them into objects, including converting Json to common objects and Json to generic objects. The test and test1 methods are my tests, you can scale as needed.
Last
There are still a lot of third-party class libraries for mutual conversion between Json and objects. gson is only one of them, but they are also similar. Once used, the others will be the same.