|
Adding AJAX
There are dozens of reasons why you would want to add AJAX to this codesometimes it's for a sensible reason, other times it's simply because it's became a buzzword. In this case, you're using AJAX to maintain a static page with only one table being updated. This looks slick and adheres to the rules of Web 2.0. In any case, it's up to you to decide whether your application truly needs AJAX or whether you just want a fancy feature.
You'll be using the AjaxTags framework to AJAX-ify your Displaytag code. The first step is adding the libraries, all of which are included in the AjaxTags distro.
The second step involves JavaScripts. You will need four of them: prototype-1.4.0.js, scriptaculous.js, overlibmws.js, and ajaxtags.js.
The third step is the stylesheet. You can make your own or you may decide not to use a stylesheet at all.
Here are the modifications you'll make 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. <%@ taglib prefix="ajax" uri="http://ajaxtags.org/tags/ajax" %>
7. <html>
8. <head>
9. <title>List All Mailings</title>
10. <link type="text/css" rel="stylesheet" href="css/display.css" />
11. <link type="text/css" rel="stylesheet" href="css/ajax.css" />
12. <script type="text/javascript" src="js/prototype-1.4.0.js" />
13. <script type="text/javascript" src="js/scriptaculous.js" />
14. <script type="text/javascript" src="js/overlibmws.js" />
15. <script type="text/javascript" src="js/ajaxtags.js" />
16. </head>
17. <body>
18. <center>
19. <dtsource:search id="mailingHS" requestURI="">
20. from|FROM to|TO subject|SUBJECT id|ID
21. </dtsource:search>
22. <dtsource:hibernate pagesize="20" id="mailingHS" list="mlng"
sizelist="sizemlng" defaultsortName="id" entity="Mailing" alias="m">
23. select ~alias~ from ~entity~ as ~alias~ order by ~sort~
24. </dtsource:hibernate>
25. <ajax:displayTag id="displayTagFrame" ajaxFlag="false" tableClass="mlngClass">
26. <display:table class="mlngClass" name="mlng" sort="external"
pagesize="20" id="mailingHS" partialList="true" size="sizemlng">
27. <display:column property="id" sortable="true" sortName="id" title="ID" />
28. <display:column sortable="true" title="FROM" sortName="from">
29. <center>
30. ${fn:escapeXml(mailingHS.from)}
31. </center>
32. </display:column>
33. <display:column sortable="true" title="TO" sortName="to">
34. <center>
35. ${fn:escapeXml(mailingHS.to)}
36. </center>
37. </display:column>
38. <display:column property="subject" sortable="true"
sortName="subject" title="SUBJECT" />
39. </display:table>
40. </ajax:displayTag>
41. </center>
42. </body>
43. </html>
That was easy, wasn't it?
Now, go through the code to see what has changed:
- Line 6 adds the taglib.
- Lines 11-15 include the stylesheet (you may not have it) and JavaScript. Please note that the order in which the JavaScript tags are added is important.
- Line 25 starts the "framing" of Displaytag's part. It's important to set the
tableClass the same as class in display:table, otherwise, your code will not be AJAX-ified.
- Line 40 closes the "framing."
You are now ready to deploy your application.
In Pursuit of Time Better Spent
While this article hasn't uncovered any new technology or hidden features, it can help you in your daily work: when you find yourself messing with a lot of different screens, but your time would be better spent writing business logic, hopefully the tricks you learned in this article will save you some time.
Resources
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.
|