Web Development Design: Displaytag Application Guide

Source: Internet
Author: User
Tags date format define object net string sort table name
web| Design Displaytag is a very handy table display tab, suitable for MVC mode, its homepage in http://displaytag.sourceforge.net
  
   first, the simplest case, unused <display:column/> label
  
<%request.setattribute ("Test", New Reportlist (6));%>
<display:table name= "Test"/>
  
The tag iterates through each object in the list and displays all the properties in the object. Generally used for development when checking the integrity of object data.
  
   ii. use of <display:column/> labelling
  
<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>
  
property corresponds to the attributes of the object in the list (obtained by the GetXXX () method), and the title corresponds to the column name in the header of the table. There are two ways of defining columns:
  
A, <display:column property= "email"/>
  
Use the property properties in the <display:column/> tag to define
  
B, <display:column title= "email" >email@it.com</display:column>
  
Add content to the <display:column/> tag body, which can be a constant, or you can use a different label, etc.
  
Two ways to compare, using property properties to define faster and easier to sort.
  
   third, the definition of table display style
  
A, in <display:table/> and <display:column/> tags to specify the standard HTML properties, cumbersome
  
B, modify the style sheet
<display:table name= "Test" class= "Mars" >
<display:column property= "id" title= "id" class= "idcol"/>
<display:column property= "Name"/>
<display:column property= "Email"/>
<display:column property= "status" class= "Tablecellerror"/>
<display:column property= "description" title= "Comments"/>
</display:table>
  
Specify the style you want to apply through the class attribute. Can be directly modified in its default style sheet (./css/screen.css)
  
   iv. data sources for label acquisition data
  
There are four different ranges
  
Pagescope
Requestscope (default) <display:table name= "Test2" >
Sessionscope <display:table name= "sessionScope.holder.list" > Note, here to specify the range, not the default
Applicationscope
  
   v. Create an implied object by adding an id attribute
  
<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)" ><%=pagecontext.getattribute ("Testit_rownum")%>< /display:column>
<display:column title= "((ListObject) testit). Getmoney ()" ><%= ((ListObject) Pagecontext.getattribute (" Testit ")). Getmoney ()%></display:column>
</display:table>
  
Noting that the id attribute was added to the <display:table/>, an implied object was created in the page context, pointing to the current object in the list.
  
This object can be captured by (ListObject) Pagecontext.getattribute ("id"). It also creates a Id_rownum object that, similarly, can be
  
Captured by Pagecontext.getattribute ("Testit_rownum"), which represents only the number of rows in the current row.
  
With these two suppressed objects, you can access them through other tags, such as jstl:
  
<display:table id= "Row" name= "MyList" >
<display:column title= "row number" >
<c:out value= "${row_rownum}"/>
</display:column>
<display:column title= "Name" >
<c:out value= "${row.first_name}"/>
<c:out value= "${row.last_name}"/>
</display:column>
</display:table>
  
   Vi. Display part of the data
  
Show start five data: By setting the Length property
  
<display:table name= "Test" length= "5" >
<display:column property= "id" title= "id"/>
<display:column property= "Email"/>
<display:column property= "status"/>
</display:table>
  
Show third to eighth data: by setting the offset and length properties
  
<display:table name= "Test" offset= "3" length= "5" >
<display:column property= "id" title= "id"/>
<display:column property= "Email"/>
<display:column property= "status"/>
</display:table>
  
   Vii. Direct connection to email and URL address
  
<display:table name= "Test" >
<display:column property= "id" title= "id"/>
<display:column property= "Email" autolink= "true"/>
<display:column property= "url" autolink= "true"/>
</display:table>
  
If you want to display an object containing email and URL address, you can set the autolink= "true" directly in Display:column to connect directly
  
   viii. use of decorative mode to convert data display (write your own decorator)
  
A, apply decorator to the entire table
  
<display:table name= "test" decorator= "Org.displaytag.sample.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>
Org.displaytag.sample.Wrapper is the decorator that writes itself, it wants to inherit Tabledecorator class, look at one of its methods:
Public String Getmoney ()
{
Return This.moneyFormat.format ((ListObject) This.getcurrentrowobject ()). Getmoney ());
}
  
It is obvious that it obtains the current object through the Getcurrentrowobject () method of the parent class, and then ' paints ' the Getmoney () method
  
B, apply decorator to separate column
  
<display:table name= "Test" >
<display:column property= "id" title= "id"/>
<display:column property= "Email"/>
<display:column property= "status"/>
<display:column property= "Date" decorator= "Org.displaytag.sample.LongDateWrapper"/>
</display:table>
Org.displaytag.sample.LongDateWrapper to implement the Columndecorator interface, its approach:
Public final String decorate (Object columnvalue)
{
Date date = (date) Columnvalue;
return This.dateFormat.format (date);
}
  
Obviously, it can't get the current object (because it implements the interface), just get the object's Columnvalue, and then ' paint '
  
   Nine, create dynamic connection
  
There are two ways to create a dynamic connection:
  
A, in <display:column/> by adding href, Paramid, paramname, Paramscope, Paramproperty properties
  
HREF Basic URL address
Paramid the name of the parameter after the URL address
ParamName the name of the data bean, typically null (that is, using the object in the current list)
Paramscope the range of the data bean, typically null
Paramproperty the property name of the data bean that fills the value of the parameter after the URL address
<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>
  
This method is simple and straightforward, but the disadvantage is that it is not possible to produce a composite URL similar to DETAILS.JSP?ID=XX&AMP;ACTION=XX
  
B, apply decorator create dynamic connections:
  
<display:table name= "Sessionscope.details" decorator= "Org.displaytag.sample.Wrapper" >
<display:column property= "Link1" title= "ID"/>
<display:column property= "Email"/>
<display:column property= "Link2" title= "Actions"/>
</display:table>
The method in Org.displaytag.sample.Wrapper:
Public String GetLink1 ()
{
ListObject lobject= (ListObject) getcurrentrowobject ();
int lindex= getlistindex ();
Return "<a href=\" "details.jsp?index=" + lindex + ">" + lobject.getid () + "</a>";
}
  
Public String GetLink2 ()
{
ListObject lobject= (ListObject) getcurrentrowobject ();
int lid= Lobject.getid ();
  
Return "<a href=\" details.jsp?id= "+ lId
+ "&action=view\" >View</a> | "
+ "<a href=\" "details.jsp?id=" + lId
+ "&action=edit\" >Edit</a> | "
+ "<a href=\" "details.jsp?id=" + lId
+ "&action=delete\" >Delete</a> ";
}
  
   10, pagination
  
The implementation of paging is very simple, add a pagesize property to specify the number of rows you want to display at once
  
<display:table name= "Sessionscope.test" pagesize= "ten" >
<display:column property= "id" title= "id"/>
<display:column property= "Name"/>
<display:column property= "Email"/>
<display:column property= "status"/>
</display:table>
  
   11, sorting
  
The sort implementation is also simple, adding the sortable= "true" attribute in the column that needs to be sorted, headerclass= "sortable" is only
  
Specifies the style to display. Property object in column to implement the comparable interface, if not, you can apply the decorator
  
defaultsort= "1" Default first column sort
Defaultorder= "Descending" Default descending sort
<display:table name= "Sessionscope.stest" defaultsort= "1" defaultorder= "Descending" >
<display:column property= "id" title= "id" sortable= "true" headerclass= "sortable"/>
<display:column property= "Name" sortable= "true" headerclass= "sortable"/>
<display:column property= "Email"/>
<display:column property= "status" sortable= "true" headerclass= "sortable"/>
</display:table>
  
Note that when paging is in place, the sort is only for the current page, not the entire list.
  
   12, column group
  
Grouping just needs to add group attributes to column
  
<display:table name= "test" class= "simple" >
<display:column property= "City" title= "City" group= "1"/>
<display:column property= "Project" title= "Project" group= "2"/>
<display:column property= "Amount" title= "HOURS"/>
<display:column property= "task" title= "task"/>
</display:table>
  
   13, export data to other formats (page overflow filter??) )
  
Set export= "true" in <display:table/>
  
Set media= in <display:column/> "CSV Excel XML PDF" determines that the field is not included in the package when it is exported to another format and is not set to contain
  
<display:setproperty name= "Export.csv" value= "false"/>
  
Determine if this format can be exported in the page
  
<display:table name= "Test" export= "true" id= "Currentrowobject" >
<display:column property= "id" title= "id"/>
<display:column property= "Email"/>
<display:column property= "status"/>
<display:column property= "longdescription" media= "CSV Excel XML PDF" title= "not on HTML"/>
<display:column media= "csv Excel" title= "url" property= "url"/>
<display:setproperty name= "Export.pdf" value= "true"/>
<display:setproperty name= "Export.csv" value= "false"/>
</display:table>
  
   14, configuration properties, overriding the default
  
Two ways:
  
A, create a new displaytag.properties file under the program Classpath
  
B, for a single form, Application <display:setProperty> label
  
Specific Configurable properties: http://displaytag.sourceforge.net/configuration.html
  
   15, a complete example
  
<display:table name= "Test" export= "true" sort= "list" pagesize= "8" >
<display:column property= "City" title= "City" group= "1" sortable= "true" headerclass= "sortable"/>
<display:column property= "Project" title= "Project" group= "2" sortable= "true" headerclass= "sortable"/>
<display:column property= "Amount" title= "HOURS"/>
<display:column property= "task" title= "task"/>
</display:table>
  
sort= "List" to sort the entire list
  
Group is not valid when exporting data to another format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.