Displaytag allows you to display a collection of Objects as a table including direct support for paging and sorting
Read The Full Tutorial.
Displaytag allows you to display a collection of Objects as a table including direct support for paging and sorting, It would do a request to a controller for every action such as a sorting or selecting a next page.
In this example we want to display episodes in a table using DWR:
1- we create a domain object called Episode:
public class Episode { long id; String show; String name; String episode; String airDate; .... }
2- we create a repository:
public class EpisodeRepository extends HibernateDaoSupport { public List<Episode> getAllEpisodes(int firstResult, int maxResults, String orderBy, boolean ascending) { Criteria criteria = getSession().createCriteria(Episode.class) .setFirstResult(firstResult) .setMaxResults(maxResults) .addOrder(ascending ? Order.asc(orderBy) : Order.desc(orderBy)); return criteria.list(); } public int getNumberOfEpisodes() { Criteria criteria = getSession().createCriteria(Episode.class); criteria.setProjection(Projections.count("id")); return (Integer)criteria.uniqueResult(); } }
And a Jsp containing the Displaytag for presenting the collection of Episodes. <display:table id="ep" name="eps" sort="external" requestURI="replaceURI" pagesize="30" partialList="true" size="${nrOfEps}"> <display:setProperty name="basic.empty.showtable" value="true" /> <display:column title="Show" property="show" sortable="true" sortName="show" /> <display:column title="Name" property="name" sortable="true" sortName="name" /> <display:column class="Episode" property="episode" sortable="true" sortName="episode" /> <display:column title="Airdate" property="airDate" sortable="true" sortName="airDate" /> </display:table>
Now you will create an abstract generic class to use it to enable any service you like, you can allow the following methods to add abstract class:
public abstract class AbstractExposedDisplayTagService<T> implements InitializingBean { public void afterPropertiesSet() throws Exception { .... } public String findAllObjects(String criteria) { WebContext wctx = WebContextFactory.get(); HttpServletRequest request = wctx.getHttpServletRequest(); // split results and set values; int maxResults = Integer.parseInt(getCriterionValue(criteria, "maxResults", DEFAULT_MAXIMUM_RESULTS)); int page = Integer.parseInt(getCriterionValue(criteria, displayTagPage, "1")); boolean ascending = Integer.parseInt(getCriterionValue(criteria, displayTagSortOrder, "1")) == 1 ? true : false; String orderBy = getCriterionValue(criteria, displayTagOrderBy, "id"); int firstResult = (page - 1) * maxResults; int numberOfObjects = getNumberOfObjects(); // set the episodes on the request so dwr can reload the jsp part. request.setAttribute(getObjectsName(), getObjects(firstResult, maxResults, orderBy, ascending)); request.setAttribute(getNumberOfObjectsName(), numberOfObjects); try { String html = wctx.forwardToString(viewFragment); html = DisplayTagReplacementUtil.updatePagingHtml(html, page, maxResults, numberOfObjects, displayTagPage); html = DisplayTagReplacementUtil.updateSortOrderHtml(html, ascending, displayTagSortOrder); html = DisplayTagReplacementUtil.updateHtmlLinks(html); return html; } catch (ServletException e) { return ""; } catch (IOException e) { return ""; } } }
Here is the implementation.
public class EpisodeService extends AbstractExposedDisplayTagService<Episode> { private EpisodeRepository episodeRepository; @Override public int getNumberOfObjects() { return episodeRepository.getNumberOfEpisodes(); } @Override public String getNumberOfObjectsName() { return "numberOfEpisodes"; } @Override public List<Episode> getObjects(int firstResult, int maxResults, String orderBy, boolean ascending) { return episodeRepository.getAllEpisodes(firstResult, maxResults, orderBy, ascending); } @Override public String getObjectsName() { return "episodes"; } public void setEpisodeRepository(EpisodeRepository episodeRepository) { this.episodeRepository = episodeRepository; } }
3- Adding javascript to call the exposed service methods, You must to add this code in the head of the jsp , Here is the code:
<script type='text/javascript' src='<c:url value="/dwr/interface/EpisodeService.js"/>'></script> <script type='text/javascript' src='<c:url value="/dwr/engine.js"/>'></script> <script type='text/javascript' src='<c:url value="/dwr/util.js"/>'></script> <link rel="stylesheet" type="text/css" href='<c:url value="/css/displaytag.css"/>' />
and the javascript:
<script type="text/javascript"> function update(criteria) { EpisodeService.findAllObjects(criteria, function(data) { dwr.util.setValue("displayTable", data, { escapeHtml:false }); }); } update(""); </script>
|