|
Adding Displaytag and dt-Source
Keeping in mind all the requirements, go ahead and add Displaytag and dt-Source.
Here, you will be adding the Displaytag code together with dt-Source. Displaytag can be used alone, but it does have its limitations. For instance, it's designed not to load data and you need to pass a collection to it. When you're dealing with large amounts of data, this then also becomes a resource problem. dt-Source solves most of these issues.
The first step is to modify the controller's bean (ListMailingController.java):
package us.prokhorenko.samples;
import us.prokhorenko.samples.MailingManager;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ListMailingsController implements Controller {
private MailingManager mailingManager;
public void setMailingManager(MailingManager mailingManager) {
this.mailingManager = mailingManager;
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// request.setAttribute("allMailings",
mailingManager.listMailings("from Mailing m"));
return new ModelAndView("list_mailings");
}
}
Yes, you don't need to set the attribute with the list of mailings anymore, because the dt-Source framework will take care of it. You can even get the rid of the controller completelythough you may find it useful in other situations. For example, Displaytag requires you to pass data collections to it, so if you'd like to use Displaytag without dt-Source, you wouldn't add any changes to controller, you'd leave it as is. Itt's up to you to decide. In this particular example, you don't need it, since dt-Source takes care of all the background work, so the line is commented (it is marked in bold).
Now, you're ready to start working with the list_mailings.jsp file, but first you need to add all necessary libraries and configuration files. All of the libraries, both for Displaytag and dt-Source, are available for free download from their sites.
Go ahead and make the basic configuration. The applicationContext.xml is the only file you'll need to edit. You'll be editing the beans, which helps dt-Source work with your Hibernate-mapped data. Simply add this to the end of the file:
...
<!-- Beans for Displaytag-Source framework -->
<!-- Basic Entity DAO implementation for Hibernate -->
<bean id="entityHibernateDAOImpl"
class="com.objecty.dtsource.dao.impl.EntityDAOHibernateImpl">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- basic implementation of Entity Manager (think like business delegate) -->
<bean id="entityHibernateManagerImpl"
class="com.objecty.dtsource.service.impl.EntityManagerImpl">
<property name="entityDAO">
<ref local="entityHibernateDAOImpl"/>
</property>
</bean>
<!-- Basic EntityManager with transactions definitions for Hibernate -->
<bean id="entityHibernateManager"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="entityHibernateManagerImpl"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
Now, take a look at the changes to list_mailings.jsp:
1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2. <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
3. <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
4. <%@ taglib prefix="display" uri="http://displaytag.sf.net" %>
5. <%@ taglib prefix="dtsource" uri="http://dt-source.sf.net" %>
6. <html>
7. <head>
8. <title>List All Mailings</title>
9. <link type="text/css" rel="stylesheet" href="css/display.css" />
10. </head>
11. <body>
12. <center>
13. <dtsource:search id="mailingHS" requestURI="">
14. from|FROM to|TO subject|SUBJECT id|ID
15. </dtsource:search>
16. <dtsource:hibernate pagesize="20" id="mailingHS" list="mlng" sizelist="sizemlng"
defaultsortName="id" entity="Mailing" alias="m">
17. select ~alias~ from ~entity~ as ~alias~ order by ~sort~
18. </dtsource:hibernate>
19. <display:table class="mlngClass" name="mlng" sort="external" pagesize="20"
id="mailingHS" partialList="true" size="sizemlng">
20. <display:column property="id" sortable="true" sortName="id" title="ID" />
21. <display:column sortable="true" title="FROM" sortName="from">
22. <center>
23. ${fn:escapeXml(mailingHS.from)}
24. </center>
25. </display:column>
26. <display:column sortable="true" title="TO" sortName="to">
27. <center>
28. ${fn:escapeXml(mailingHS.to)}
29. </center>
30. </display:column>
31. <display:column property="subject" sortable="true" sortName="subject"
title="SUBJECT" />
32. </display:table>
33. </center>
34. </body>
35. </html>
Step-by-step:
- Lines 4-5 are adding taglibs to the code.
- Line 9 adds a style-sheet for tables, which will be produced by Displaytag and dt-Source. It is very straighforwarded, so your CSS gurus (or whoever else helps you with page layout) will have no problem streamlining the page.
- Lines 13 to 15 are where dt-Source places a search form.
- Line 14 lists the fields to be searched (in a notation like
REAL_FIELDNAME|DISPLAYNAME). You can add as many fields here as you like, though their display names should be completely different.
- In Lines 16-18, dt-Source fetches data from Hibernate and adds it to the session. The most interesting attributes from line 16 are
pagesize (you want paging and you need to set the size of it, so you don't have to fetch more data) and entity (an actual Hibernate entity which you'll be exploring in depth shortly).
- Lines 19-32 are Displaytag's tags, which build the actual table with pagination and sorting.
Figure 1 shows the end result.
Note: One of the most useful functionalities of Displaytag (as well as dt-Source) is its ability to handle multiple tables on the same page. You just need to keep the ID unique, and everything else is completely transparentany changes to any table will not influence any other tables.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|