advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Simplify List Screen Creation with AJAX :

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 completely—though 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.

figure1.JPG

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 transparent—any changes to any table will not influence any other tables.

How to Add Java Applets to Your Site

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.

 DevX Skillbuilding from IBM developerWorks
 RIA Run Contest: Build Next-Gen Apps in Microsoft Silverlight 2
 Avaya DevConnect Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Alfresco's Latest ECM: Prying Open a Sector?
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads for the JON 2.0
Out with the Old, in with the New at JavaOne
Trolltech Expands WebKit Footprint
Oracle: Eating its Own Open Source Food
Big Money and Open Source May Not Compute
Open Source Embrace Gives Sun New Fans

Getting Started with TBB on Windows
Moving to VoIP: Should You Go It Alone?
Introduction to the WPF Command Framework
7.0, Microsoft's Lucky Version?
Will Hyper-V Make VMware This Decade's Netscape?
Eliminate Fragmentation Frustration with Netbiscuits
Taming Trees: Building Branching Structures
Clean Up Function Syntax Mess with decltype
Sutter Speaks: The Future of Concurrency
INTEL SCAVENGER HUNT, LENOVO X300 AND APPLE IPOD TOUCH GIVEAWAY (the "Giveaway")

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES