Previous Page
How to use HDPagination
Now let's look at how to use HDPagination to implement pagination search functionality:
Step I: Create Action
Create an action class which represents the component processing the pagination based search request.
As you can see, you only deal with normal SQL without any database provider specific pagination SQL syntax included.
QueryCallback wrapper classes provide simple API to allow you to apply restrictions dynamically based on the received input values.
public QueryCallback getQueryCallback(HttpServletRequest request, Object command) {
SearchCriteria crit = (SearchCriteria) command;
JdbcQueryCallbackWrapper callback = JdbcQueryCallbackWrapper
.newInstance("select productId,prodNo,price,madeIn,description from product p", ProductVO.class)
.addRestriction( Restrictions.eq("madeIn", crit.getMadeIn()) )
.addRestriction( Restrictions.contains("description", crit.getDescription()) )
.setOrderBy("p.madeIn");
return callback;
}
Step II: Configure Action
Declare the action class and other configurations in a spring configure file 'WEB-INF/hdpagination.xml". In this case, the database is MySQL, as you can see org.hdpagination.dataaccess.jdbc.mysql.MySqlJdbcQueryTemplate is injected into the action class. This gives the framework indication that pagination SQL conversion should be based on MySQL syntax. However the framework abstracts the detail of SQL conversion from the user of framework.
<bean id="globalConfig" class="org.hdpagination.config.GlobalConfig">
<property name="pageSize" value="10"/>
<property name="defaultDataScope" value="request"/>
<property name="urlSuffix" value="hdp"/>
<property name="sortUpImage" value="/images/pagination_up.gif"/>
<property name="sortDownImage" value="/images/pagination_down.gif"/>
<property name="pageLinksPattern">
<ref bean="defaultLinksPattern"/>
</property>
</bean>
<bean id="defaultLinksPattern" class="org.hdpagination.config.pattern.DefaultPageLinksPattern">
<constructor-arg type="int" value="10"/>
<property name="resourceBundleName" value="eg.web.action.PaginalLinks"/>
<property name="displayFirstPageLink" value="true"/>
<property name="displayLastPageLink" value="true"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
<bean id="queryTemplate" class="org.hdpagination.dataaccess.jdbc.mysql.MySqlJdbcQueryTemplate">
<constructor-arg><ref bean="dataSource"/></constructor-arg>
</bean>
<bean id="searchProductAction" class="eg.web.action.jdbc.SearchProductAction">
<property name="id" value="searchProduct"/>
<property name="forwardPath" value="/searchProduct.jsp"/>
<property name="attributeName4Data" value="query_result"/>
<property name="queryTemplate">
<ref bean="queryTemplate"/>
</property>
</bean>
Step III: Modify web.xml
Modify WEB-INF/web.xml as the follows:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/hdpagination.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>hdpagination</servlet-name>
<servlet-class>org.hdpagination.web.servlet.PaginationServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hdpagination</servlet-name>
<url-pattern>*.hdp</url-pattern>
</servlet-mapping>
This modification is to allow spring to load the configure file and initialize the related components, and declare the PaginationServlet will handle http request with its URL ending with '.hdp' and dispatch it to a particular Action class for processing.
Step IV: Use tags in jsp
To simplify coding in jsp page, HDPagination provides some useful tags to display page links and other html elements (dropdown, text field) allowing user to navigate to different pages. All tags are very easy to use, for instance, to display page links, the code can be:
<hdp:pageLinks actionId="searchProduct" resendParams="true"/>
To display a dropdown for all available pages, the code would be:
<hdp:pagesDropdown actionId="searchProduct" resendParams="true"/>
Selecting the page option in the dropdown list will forward to that particular page.
An important thing worth noting here is that no tag is provided to display the table of search result. This gives page developer the maximum flexibility because he can choose any technology (e.g. JSP code, jstl or struts tags) to display the data, and mix any html tags he wants.
Previous Page
|