displaytag

Post on 28-Jan-2015

6.323 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

a guide to the display tag

TRANSCRIPT

Gökhan Tanışıkgokhantanisik@hotmail.com

17.07.2008

displaytag

Session ObjectivesThis session is learning about the displaytag:

What displaytag isWhy do we use itThe properties of the displaytag

AgendaWhat displaytag is?What can I do with it?Basic usageImplicit ObjectSupported DataUsing With DatabaseUsing Decorators to Transform DataGenerating LinksExporting TableSorting Data

What displaytag is?The display tag library is an open source

suite of custom tags that provide high-level web presentation patterns which will work in an MVC model.

The library provides a significant amount of functionality while still being easy to use.

What can I do with it?Actually the display tag library can just...

display tables! Give it a list of objects and it will handle

column display, sorting, paging, cropping, grouping, exporting, smart linking and decoration of a table in a customizable XHTML style.

Basic Usage (example_1)<jsp:root version="1.2" xmlns:jsp="http://java.sun.com/JSP/Page"

xmlns:display="urn:jsptld:http://displaytag.sf.net"> <jsp:directive.page contentType="text/html; charset=UTF8" /> <jsp:directive.page import="org.displaytag.sample.*" /> <head> <title>displaytag examples</title> <style type="text/css" media="all"> @import url("css/displaytag.css"); </style> </head>

<jsp:scriptlet> request.setAttribute( "test", new TestList(10, false) ); </jsp:scriptlet>

<h2>Columns</h2><display:table name="test" >

<display:column property="id" title="ID" /><display:column property="name" /><display:column property="email" /><display:column property="status" /><display:column property="description" title="Comments" />

</display:table></jsp:root>

Examining the example_1xmlns:display=“urn:jsptld:http://

displaytag.sf.net”,Declares the namespace of the display tag,

“xmlns:display…”=>display is the prefix will be used for the display tag.

@import url("css/displaytag.css")Formats the view of the table

request.setAttribute( "test", new TestList(10, false) );Declares a random list and fills it with 10 random

objects. The object in the list has attributes: “id, name, email, status, description”. It is declared in the org.displaytag.sample package

Examining the example_1(2)<display:table name=“test”>

The decleration of a simple table, gets the properties from the “test<TestList>” we declerad before.

<display:column property="id" title="ID" />A simple column decleration, “property” used for

the property to display, in here, it is the “id” attribute of the current object in the “test” list.

“title” is used to define the title of the column. If we don’t use it, property name is used as default.

Implicit objects-example_2…<jsp:scriptlet> request.setAttribute( "test", new TestList(10, false) ); </jsp:scriptlet>

<h2>Implicit objects created by table</h2>

<display:table name="test" id="testit"> <display:column property="id" title="ID" /> <display:column property="name" /> <display:column title="static value">static</display:column> <display:column title="row number (testit_rowNum)"> <c:out value="${testit_rowNum}"/> </display:column> <display:column title="((ListObject)testit).getMoney()"> <c:out value="${testit.money}"/> </display:column> <display:column property="money"/> </display:table>

Examining the example_2<display:table name="test" id="testit">

If you add an “id” attribute to the table tag, then it makes the object corresponding to the given row available in the page context, so you could use it inside scriplet code or some other tag.

you can access it using pageContext.getAttribute("id") or <% id %> (but only if the value is not a run time expression), the preferred way is the first one.

<display:column title="static value">static</display:column>You can use a static value for a column

Examining the example_2(2)<c:out value="${testit_rowNum}"/>

You can get the row number of the table as: yourTableId_rowNum

<display:column title="((ListObject)testit).getMoney()">

<c:out value="${testit.money}"/> </display:column>

As we mentioned above we can use the id of the table as a reference to that row. Thus testit.money means the money attribute of the current row

<display:column property="money"/>Has the same meaning with above

Supported DataThe table tag supports the following kind of

objectsa Collectionan Enumerationa Map (values are displayed in row)a Dictionary (values are displayed in row)an arrayan Iteratorany Object with an iterator() method... any other object will be displayed on a single

row

Using With DatabaseUsing JSTL

<sql:query var="results">select * from table

</sql:query><display:table name="${results.rows}" />(or<display:table name="pageScope.results.rows" /> if not using the EL version)

Using With Database(2)Using dynabeans<% Connection con = ...; // just open a connection Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * from

table"); RowSetDynaClass resultSet = new

RowSetDynaClass(rs, false); stmt.close(); con.close(); request.setAttribute("results", resultSet);%> <display:table name="requestScope.results.rows" />

Using Decorators to Transform Data <h2>Using decorators to transform data</h2>

<display:table name="test" decorator="org.displaytag.sample.decorators.Wrapper">

<display:column property="id" title="ID" /> <display:column property="email" /> <display:column property="status" /> <display:column property="date" /> <display:column property="money" /> </display:table>

Output with decoration

Output with decoration

Using Decorators to Transform Data(2)You can specify decorators that work on

individual columns. Example: <display:table name="test">

<display:column property="date" decorator="org.displaytag.sample.LongDateWrapper" />

</display:table>

Generating Linksautolink

If you have a link in the column you can use it (true/false)orhref

the base URL used to construct the dynamic linkparamId

the name of the parameter that gets added to the URL specified above

paramNamename of the bean that contains the data we want to tack on the the URL (typically null, indicating the current object in the List)

paramPropertyproperty to call on the object specified above to return the value that gets tacked onto the URL.

paramScopespecific scope where the databean lives, typically null

Smart Linking Example <h2>Smart linking of column data</h2> <display:table name="test"> <display:column property="id" autolink="true"

title="ID" /> <display:column property="email" autolink="true"

/> <display:column property="url" autolink="true" /> </display:table>

Dynamic Links-example_3<jsp:scriptlet> Object foo = session.getAttribute( "details" ); if( foo == null ) { session.setAttribute( "details", new TestList(10, false) ); } request.setAttribute("testparam", "sendamail");</jsp:scriptlet>…<display:table name="sessionScope.details"> <display:column property="id" title="ID" href="details.jsp“

paramId="id" /> <display:column property="email" href="details.jsp" paramId="action"

paramName="testparam" paramScope="request" /> <display:column property="status" href="details.jsp" paramId="id"

paramProperty="id" /> </display:table>

Exporting Table<h2>Data exporting</h2><display:table name="test" export="true"

id="currentRowObject"><display:setProperty name="export.rtf.filename"

value="example.rtf"/><display:column property="id" title="ID" />

<display:column property="email" /><display:column property="status" /><display:column property="date" />

</display:table>

28

Sorting Data<jsp:scriptlet> Object foo = session.getAttribute( "stest" ); if( foo == null ) { session.setAttribute( "stest", new TestList( 10, false ) ); } </jsp:scriptlet> <h2>Auto-sorting by columns</h2> <display:table name="sessionScope.stest" defaultsort="1"

defaultorder="descending"> <display:column property="id" title="ID" sortable="true"

headerClass="sortable" /> <display:column property="name" <display:column property="email" /> <display:column property="status” /> </display:table>

29

30

RemainderThe presentations is prepared from the

tutorial of the display tag in the official site:http://displaytag.sourceforge.net/All the examples and the codes are taken

from the site.To find the original examples download the

library from the site and import the project displaytag-examples-version.war file to the eclipse or whatever you use...

31

Using displaytag in a projectDownload the library from

http://sourceforge.net/project/showfiles.php?group_id=73068

Drop the displaytag-version.jar file in your application WEB-INF/lib directory

Make sure the directory includes alsocommons-loggingcommons-langcommons-collectionscommons-beanutilslog4jitext (optional, for pdf/rtf export) 32

Thanks…

top related