The DEMO uses the ssh framework. The data operation difficulty of a paging table is data presentation. For addition, deletion, and modification, you can simply use the native method of hibernate.
Preliminary analysis: to implement paging in a table, the following two conditions are required: PageSize and CurrentPage. To Sort by clicking the header of any column, the column name (Sidx) and sorting rule (Sort) are required. With these four parameters, basic data presentation is not a problem. However, if there is a large amount of data. You also need a Map parameter to save the Request Parameters of the page. For example, query by number or by name. Let's not talk about it. Let's look at the Dao interface definition first.
| 1234567891011121314 |
/*** @param pageSize* How many entries are displayed on each page* @param currentPage* Current page* @param paramMap* Parameters* @param sidx* Sorted Columns* @param sord* Ascending or descending* @return*/public Map<?, ?> queryByJQGrid(int pageSize, int currentPage, Map<String, String> paramMap, String sidx, String sord); |
Next we will implement this interface.
| 12345678910111213141516171819202122232425262728293031 |
@Overridepublic Map<?, ?> queryByJQGrid(int pageSize, int currentPage, Map<String, String> paramMap, String sidx, String sord) {Map result = new HashMap();List list = null;try {Criteria cr = DetachedCriteria.forClass(Student.class).getExecutableCriteria(this.getSession());if ((paramMap != null) && (paramMap.size() > 0)) {String key = null;String value = null;Iterator ite = paramMap.keySet().iterator();while (ite.hasNext()) {key = (String) ite.next();value = (String) paramMap.get(key);if ((key != null) && (key.trim().length() > 0) && (value != null) && (value.trim().length() > 0)) {cr.add(Restrictions.like(key, "%" + value.trim() + "%"));}}}Integer totcount = (Integer) cr.setProjection(Projections.rowCount()).uniqueResult();cr.setProjection(null).setResultTransformer(Criteria.ROOT_ENTITY);if (!StringUtils.isEmpty(sidx)) {list = cr.addOrder(((sord != null) && (sord.trim().equalsIgnoreCase("asc"))) ? Order.asc(sidx.split(" ")[0]) :Order.desc(sidx.split(" ")[0])).setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();}result.put("totalCount", totcount);result.put("dataList", list);} catch (Exception e) {e.printStackTrace();}return result;} |
The Service layer does not need to do anything to explain it. Next, let's take a look at how Action is implemented. To work with Jqgrid's JsonReader option parameters, we can save the parameters in JsonReader with JavaBean, and use Action to inherit the JavaBean, omitting the get and set methods.
| 12345678 |
protected List dataList = Collections.emptyList(); // Data setprotected Integer rows = Integer.valueOf(0); // Number of rowsprotected Integer page = Integer.valueOf(0); // Current page numberprotected Integer total = Integer.valueOf(0); // Total number of data pagesprotected Integer record = Integer.valueOf(0); // Total number of data entriesprotected String sord; // Sorting methodprotected String sidx; // Sort Fieldsprotected String search; |
Compile Action to inherit the JavaBean just now and call the Dao layer method.
| 12345678910111213 |
public String query() {Map<String, String> paramMap = new HashMap<String, String>();Map map = null;if (null != stuName && stuName.trim().length() > 0) {paramMap.put("name", stuName);}map = queryAllStudentService.queryByJQGrid(rows, page, paramMap, sidx, sord);this.dataList = (ArrayList) map.get("dataList");Object totalCount = map.get("totalCount");this.record = Integer.valueOf(Integer.parseInt((totalCount == null) ? "0" : totalCount.toString()));this.total = Integer.valueOf((int) Math.ceil(this.record.intValue() / this.rows.intValue()));return "query_success";} |
The implementation of key code is almost the same. Next, pay attention to the returned data format. Because we need data in JSON format. Type 1: JSONObject can be used to serialize the set to return data in json format. Type 2: json-default can be inherited in Struts. xml, and data can be returned through configuration. The second type is recommended, because the first type will serialize all the fields, which will have a certain impact on performance. The Struts. xml configuration is as follows:
| 12345678910111213141516171819 |
<package name="student" extends="json-default"><action name="QueryActionUrl_*" method="{1}"><result name="query_success" type="json"><param name="includeProperties">dataList\[\d+\]\.id,dataList\[\d+\]\.name,dataList\[\d+\]\.age,dataList\[\d+\]\.address,dataList\[\d+\]\.class,dataList\[\d+\]\.likedo,dataList\[\d+\]\.phone,dataList\[\d+\]\.sex,rows, page, total,record</param><param name="noCache">true</param><param name="ignoreHierarchy">false</param><param name="contentType">text/html</param></result></action></package> |
Note: Using json in Struts2 also requires json-plagin.jar support. Otherwise, an exception occurs in There is no mapping for namespace. If you have any questions, leave your comments.
Original article, reprinted Please note:Reproduced from java developers
Link:Jqgrid-data presentation with Struts2 + json (5)