Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Batis pagination based on Struts2 and Freemarkeri?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how the Batis page based on Struts2 and Freemarkeri is like, the content is very detailed, interested friends can refer to, hope to be helpful to you.

We have previously introduced the principle and design of Hibernate-based paging. Here, the paging we use is physical paging technology, which is not implemented by JS, but is executed on SQL statements. You can get a fixed number of result sets list with high execution efficiency. Let's take a look at how to design paging in iBatis. This article is based on Struts2,Spring3 integration, because Spring does not support MyBatis3 (you can choose MyBatis's official MyBatis-Spring plug-in to achieve it, with Chinese documentation, easy to understand), we still use iBatis2 as a carrier to introduce.

The first is to build a development environment, it can be said that here is also a simple integration of Struts2,Spring3 and iBatis2, you can also refer to. The structure of the project is as follows, using the web project created by Maven:

Add the necessary dependencies, because of the integration of Struts2 and Spring, there are more dependencies, as follows:

First of all, let's configure Struts2, this is relatively simple, I believe we are no stranger. In web.xml:

Xml code

Struts2filter-name > org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class > filter > struts2filter-name > * .actionurl-pattern > filter-mapping >

Then there is struts.xml to configure the content related to Struts. Here we configure freemarker as the default result type, and then configure a test Action. Because it is integrated with Spring, the specific configuration of Action is put into Spring, as shown below:

Xml code

"- / / Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"http://struts.apache.org/dtds/struts-2.1.7.dtd"> result-types > user_list.ftlresult > action > package > struts >

Make a simple setting for Freemarker and uninstall the freeemarer.properties file. Here we mainly refer to a macro file, that is, the configuration of paging macros, as follows:

Properties code

Template_update_delay=5 default_encoding=UTF-8 url_escaping_charset=UTF-8 number_format=0.# date_format=yyyy-MM-dd time_format=HH:mm:ss datetime_format=yyyy-MM-dd HH:mm:ss boolean_format=true,false whitespace_stripping=true tag_syntax=auto_detect auto_import=/Freemarker/page_macro.ftl as p

The Log4J configuration code is no longer posted here. You can download the source code and understand it at a glance. Then we configure Spring, create a spring subdirectory under the resources folder, place the Spring configuration file in it, and load the Spring configuration file in web.xml by setting the following settings:

Xml code

ContextConfigLocationparam-name > classpath:spring/*.xmlparam-value > context-param > org.springframework.web.context.ContextLoaderListenerlistener-class > listener >

Spring mainly configures data sources, SqlMapClient and SqlMapClientTemplate of iBatis, transaction processing and management of Action and Service. In fact, everyone is familiar with the content, which is relatively simple:

Xml code

Bean > bean > constructor-arg > bean > bean > Tx:attributes > tx:advice > aop:config >

Then configure Service and Action:

Xml code

Bean > bean >

Let's take a look at the configuration of iBatis. When configuring SqlMapClient, we added the configuration file of iBatis. Let's take a look at how to set sqlMapConfig.xml:

Xml code

PUBLIC "/ / iBATIS.com//DTD SQL MapConfig 2.0//EN"http://www.ibatis.com/dtd/sql-map-config-2.dtd"> sqlMapConfig >

In fact, the content is also very simple, that is, to set up the necessary information, the meaning of which can be referred to the previous article on the introduction of iBatis, and finally, don't forget to add the sqlMaps configuration file. Here we only have a user.xml file. For testing, that is, a query, pagination is performed for this query:

Xml code

> select * from user select > sqlMap >

ParameterMap has appeared many times in the previous introduction, so let's take a look at it again:

Java code

Package org.ourpioneer.bean; import java.util.HashMap; public class ParameterMap extends HashMap {public ParameterMap (Object... Parameters) {for (int I = 0; I

< parameters.length - 1; i += 2) { super.put(parameters[i], parameters[i + 1]); } } } 其实就是扩展了一下HashMap类,来进行参数的放置,注意参数类型是可变参数的形式,也就是名-值对的形式出现的,不过本例中没有使用它。下面就是分页类的设计了: Java代码 package org.ourpioneer.bean; import java.util.List; import org.springframework.orm.ibatis.SqlMapClientTemplate; /** * iBatis分页类 * * @author Nanlei * */ public class PagingList { private int rowCount = 0; // 记录总数 private int pageCount = 1; // 分页总数 private int pageSize = 10; // 每页记录数 private int pageNum = 1; // 当前页数 private int startIndex = 1; // 起始记录数 private int endIndex = 1; // 结束记录数 private List list;// 记录列表 /** * 构造方法,进行分页 * * @param statementName * iBatis中语句的ID * @param parameterObject * SQL语句参数 * @param pageNum * 起始页数 * @param pageSize * 每页大小 * @param sqlMapClientTemplate * iBatis的sqlMapClientTemplate对象 */ public PagingList(String statementName, Object parameterObject, int pageNum, int pageSize, SqlMapClientTemplate sqlMapClientTemplate) { preProcessParams(pageNum, pageSize); execute(statementName, parameterObject, pageNum, pageSize, sqlMapClientTemplate); } /** * 构造方法,进行分页 * * @param statementName * iBatis中语句的ID * @param pageNum * 起始页数 * @param pageSize * 每页大小 * @param sqlMapClientTemplate * iBatis的sqlMapClientTemplate对象 */ public PagingList(String statementName, int pageNum, int pageSize, SqlMapClientTemplate sqlMapClientTemplate) { preProcessParams(pageNum, pageSize); execute(statementName, pageNum, pageSize, sqlMapClientTemplate); } /** * 执行方法 * * @param statementName * @param parameterObject * @param pageNum * @param pageSize * @param sqlMapClientTemplate */ blic void execute(String statementName, Object parameterObject, int pageNum, int pageSize, SqlMapClientTemplate sqlMapClientTemplate) { // 计算记录总数 this.rowCount = sqlMapClientTemplate.queryForList(statementName, parameterObject).size(); // 计算分页数及起止记录 countPage(); // 获取分页列表 this.list = sqlMapClientTemplate.queryForList(statementName, parameterObject, (pageNum - 1) * pageSize, pageSize); } /** * 执行方法 * * @param statementName * @param pageNum * @param pageSize * @param sqlMapClientTemplate */ public void execute(String statementName, int pageNum, int pageSize, SqlMapClientTemplate sqlMapClientTemplate) { // 计算记录总数 this.rowCount = sqlMapClientTemplate.queryForList(statementName).size(); // 计算分页数及起止记录 countPage(); // 获取分页列表 this.list = sqlMapClientTemplate.queryForList(statementName, (pageNum - 1) * pageSize, pageSize); } /** * 预处理SQL语句和页面参数 */ private void preProcessParams(int pageNum, int pageSize) { if (pageNum >

0) {this.pageNum = pageNum;} if (pageSize > 0) {this.pageSize = pageSize;} if (pageSize > 1000) {this.pageSize = 1000 }} / * calculate the number of pages and start and end records * / private void countPage () {/ / calculate the total number of pages if ((rowCount% pageSize) = = 0) {pageCount = rowCount / pageSize;} else {pageCount = rowCount / pageSize + 1 } if (pageCount = = 0) {pageCount = 1;} / / determine whether pageNum is out of bounds if (pageNum > pageCount & & rowCount! = 0) {pageNum = pageCount } / / calculate the start-stop record startIndex = (pageNum-1) * pageSize + 1; endIndex = (pageNum) * pageSize;} / * * get the object list * / public List getList () {return list } / * get the number of starting records * / public int getStartIndex () {return startIndex;} public Integer getStartIndexInteger () {return new Integer (startIndex);} / * get the number of ending records * / public int getEndIndex () {return endIndex } public Integer getEndIndexInteger () {return new Integer (endIndex);} / * get other paging information * / public int getPageCount () {return pageCount;} public int getPageNum () {return pageNum;} public int getPageSize () {return pageSize } public int getRowCount () {return rowCount;}}

After writing the paging class and integrating with the framework, we can abstract the base class of Service and call it in the business logic layer to get paging information:

Java code

Package org.ourpioneer.service; import org.ourpioneer.bean.PagingList; import org.springframework.orm.ibatis.SqlMapClientTemplate; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.util.ValueStack; public class BaseService {/ * get ValueStack * * @ return ValueStack object * / public ValueStack getValueStack () {return ActionContext.getContext () .getValueStack () } / * get the paged List * * @ param statementName * @ param sqlMapClientTemplate * @ return * / public PagingList getPagingList (String statementName, SqlMapClientTemplate sqlMapClientTemplate) {int pageNum = ((Integer) getValueStack (). FindValue ("pageNum")) .intValue () Int pageSize = ((Integer) getValueStack (). FindValue ("pageSize")) .intValue (); return new PagingList (statementName, pageNum, pageSize, sqlMapClientTemplate) } / * get the paged List * * @ param statementName * @ param parameterObject * @ param sqlMapClientTemplate * @ return * / public PagingList getPagingList (String statementName, Object parameterObject SqlMapClientTemplate sqlMapClientTemplate) {int pageNum = ((Integer) getValueStack () .findValue ("pageNum")) .intValue () Int pageSize = ((Integer) getValueStack (). FindValue ("pageSize")) .intValue (); return new PagingList (statementName, parameterObject, pageNum, pageSize, sqlMapClientTemplate);}}

We have used both construction methods, that is, one with parameters and one without parameters. Let's take a look at the abstract Action base class, which mainly deals with the paging parameters passed in by the page:

Java code

Package org.ourpioneer.action; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.ourpioneer.util.QueryUtil; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class BaseAction extends ActionSupport {@ Override public String execute () throws Exception {return SUCCESS } public Map getParameters () {return ActionContext.getContext () .getParameters ();} public HttpServletRequest getRequest () {return ServletActionContext.getRequest ();} / * paging information * / protected int pageNum = 1; protected int pageSize = 10; public int getPageNum () {return pageNum } public void setPageNum (int pageNum) {this.pageNum = pageNum;} public int getPageSize () {return pageSize;} public void setPageSize (int pageSize) {this.pageSize = pageSize;} public int getMaxPageSize () {return 1000 } public int getDefaultPageSize () {return 10;} / / method used to parse paging information public String getQueryStringWithoutPageNum () {Map m = getParameters (); m.remove ("pageNum"); return QueryUtil.getQueryString (m) } public String getFullUrlWithoutPageNum () {return getRequest (). GetServletPath () + "?" + getQueryStringWithoutPageNum ();} public String getQueryStringWithoutPageInfo () {Map m = getParameters (); m.remove ("pageNum"); m.remove ("pageSize"); return QueryUtil.getQueryString (m) } public String getFullUrlWithoutPageInfo () {return getRequest () .getServletPath () + "?" + getQueryStringWithoutPageInfo ();}

For demonstration purposes, we define the paged information directly. You can modify it as needed. For the QueryUtil that handles the information, you can refer to the source code directly. Without explanation here, the following is the writing of the UserAction processing code:

Java code

Package org.ourpioneer.action; import org.ourpioneer.bean.PagingList; import org.ourpioneer.service.UserService; public class UserAction extends BaseAction {private UserService userService; public PagingList userList; public void setUserService (UserService userService) {this.userService = userService;} public PagingList getUserList () {return userList } public String list () {userList = userService.getAllUsers (); return "list";}}

According to the previous configuration, it is not difficult for us to write the code. Here is the view processing. We use Freemarker for parsing and write the paging macro of FreeMarker:

Html code

# if > # function > # if > # function > No related records div > # if > Total ${rowCount} records ${pageCount} page All display a > span > paging display a > span > # if > # if > 1) > 11) > 9a > # if > 3a > 11) > 9span > # if > 3span > # if > ${x} span > ${x} a > span > # if > # list > 4a > 11) > : a > # if > 4span > 11) >: span > # if > # if > # if > td > tr > table > # if > # macro >

After that, let's run the project:

You can view the paging effect by clicking Show all and the page.

On the Struts2 and Freemarkeri-based Batis pages is how to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report