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.

 Avaya DevConnect Center
 Service Component Architecture/Service Data Objects Solution Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 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%.

Windows 7: It's Not Just a Codename Anymore
Microsoft Shines Silverlight on Eclipse
OpenOffice Hits 3.0: Can It Challenge Microsoft?
Get Ready for Microsoft's 'Oslo' Modeling Tool
Latest Linux Hits Networking Flaws
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next
Is .NET on Linux Finally Ready?
Red Hat Takes on HPC Market, Microsoft

Intel Sees Fewer Power Cords in Your Future
F# 101
Use Explicit Conversion Functions to Avert Reckless Implicit Conversions
Polyglot Programming: Building Solutions by Composing Languages
Automated testing for .NET by Ben Hall
"Supply Chain" SOA with SKOS
Service Component Architecture in Real Life
C++Ox: The Dawning of a New Standard
Getting Started with Virtualization
Master Complex Builds with MSBuild

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: BitLocker Encryption on Windows Server 2008
Go Parallel Article: Intel Thread Checker, Meet 20 Million LOC
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES