Display tag Use note

Source: Internet
Author: User
After using the display tag for a period of time, I think it is very useful. On the forum, I found that there are very few people using this stuff, so I occasionally write an article.
Display tag Lib is a tag library used to process tables on JSP web pages. It has powerful functions such as paging, data export, grouping, and column sorting, all of the functions I need when working on a project are provided to me, and they are very convenient to use. This greatly reduces the amount of code.
The official website of the display tag is http://displaytag.sourceforge.net /.

First, download its jar package. You can download the latest version here. Place the jar package in the Lib folder of the WEB-INF. In addition, two auxiliary packages are required: Apache commons-Lang and standard packages. More auxiliary packages can be downloaded here.

Add a filter under Web. xml
<Filter>
<Filter-Name> exportfilter </filter-Name>
<Filter-class> org. displaytag. Filter. responseoverridefilter </filter-class>
</Filter>

Make a reference on the JSP page:
<% @ Taglib uri = "http://displaytag.sf.net/el" prefix = "display" %>

First, we define a list
<%
List test = new arraylist (6 );
Test. Add ("test string 1 ");
Test. Add ("test string 2 ");
Test. Add ("test string 3 ");
Test. Add ("test string 4 ");
Test. Add ("test string 5 ");
Test. Add ("test string 6 ");
Request. setattribute ("test", test );
%>

When we want to display this list on the JSP page, we only need to write one sentence
<Display: Table name = "test"/>
The display tag automatically generates a table

If the list is thrown from the control layer, the name can be expressed using the El expression.
<Display: Table name = "$ {test}"/>

This is the simplest use of the display tag. we can add a style to it, or define the display columns. The following table display is more complex.
<Display: Table name = "test" styleclass = "list" cellspacing = "0" cellpadding = "0">
<Display: column property = "ID" Title = "ID" class = "idcol"/>
<Display: column property = "name"/>
<Display: column property = "email"/>
<Display: column property = "Description" Title = "Comments"/>
</Display: Table>

If you want to add a link to it, it is very easy. The following Code adds a connection to the name with the ID parameter attached, and the email is automatically connected to mailto: XXX.
<Display: Table name = "test" styleclass = "list" cellspacing = "0" cellpadding = "0">
<Display: column property = "ID" Title = "ID" class = "idcol"/>
<Display: column property = "name" url = "detail. jsp" paramid = "ID" paramproperty = "ID"/>
<Display: column property = "email" autolink = "true"/>
<Display: column property = "Description" Title = "Comments"/>
</Display: Table>

The following describes the most common display functions. For more functions, see http://displaytag.homeip.net/displaytag-examples-1.1 /.
1. Paging
If you want to pagination code, you only need to add a pagesize = "lines per page" in the display: Table label, as shown in figure
<Display: Table name = "test" pagesize = "10"/>

2. Sort Columns
The display tag can be used to sort the column, that is, click the column name to sort the column data. You only need to add sort = "true" to the column to be sorted. The following code can sort the first three columns. Add defaultsort = "Number of columns" to display: Table to sort the specified columns by default.
<Display: Table name = "test" styleclass = "list" cellspacing = "0" cellpadding = "0" defaultsort = "1">
<Display: column property = "ID" Title = "ID" class = "idcol" Sort = "true"/>
<Display: column property = "name" url = "detail. jsp" paramid = "ID" paramproperty = "ID" Sort = "true"/>
<Display: column property = "email" autolink = "true" Sort = "true"/>
<Display: column property = "Description" Title = "Comments"/>
</Display: Table>
If the table has pagination, the display tag only sorts the current page by default. to sort the entire list, you can add a piece of code between the display: Table:
<Display: setproperty name = "sort. amount" value = "list"/>

3. Export data
Add export = "true" to display: Table to see what will happen! By default, the display TAG provides three data export Methods: CSV, Excel, and XML.
In addition, the display tag can be exported to PDF format:
<Display: setproperty name = "export.pdf" value = "true"/>.

4. Display tag attribute settings
The display: setproperty mentioned above is a method to change the display tag attribute, but it is too troublesome to write in every JSP.
Many default attributes are set in the display tag. It has a special attribute file named displaytag/properties/tabletag. properties in its jar package.
To change its default properties, you can create a new file displaytag. properties under the WEB-INF/classes to set the properties to be modified in the format of the properties in tabletag. properties.
# Messages in tabletag. properties sets the prompt information displayed on the page. It is in English by default. We can change it to Chinese. In this example, you can only use unicodeto convert the Chinese Character Library to the unicodecode, which can be converted using native2ascii.exe with JDK.

5. Other functions
Displaytag also has some useful small functions. Here we will mention two. One is the data format, which is a new feature added in version 1.1. You can format the time, number, and string using tags. For example, to add format = "{0, date, yyyy-mm-dd}" to the column label to be formatted, the first parameter is the formatted data serial number, the second parameter is the data type, the number is number, and the third parameter is the data format.
Another function is the aggregate function of table data. Add decorator = "org. displaytag. decorator. totaltabledecorator ", and then add total =" true "to the column label of the data column to be aggregated, the total number of columns can be calculated. However, this function has a disadvantage. It cannot be used when there is a page, it can only aggregate the data on the first page.

Insufficient displaytag
People who use displaytag for the first time may be pleasantly surprised, but after a long time, they will find many problems. The biggest problem is poor support for Chinese. For example, if the query conditions contain Chinese characters, they will not be able to flip pages, chinese characters cannot be sorted. garbled characters occur when Chinese characters are exported as specified files. These problems are sometimes depressing, and sometimes you have to modify its source code. The solution to the above problems is as follows:
1. The easiest way to do this is to modify the server. xml file in Tomcat. Find the HTTP ctor tag and add a URL in it. The content in the quotation marks depends on your page code, such as GBK and utf8. In this way, the above two problems can be solved.
2. export as a file: in fact, this function has many other problems besides Chinese support. For example, it exports HTML tags together and only exports the displayed content. However, if you decorator the table, the content after decorator cannot be exported. If you want to export Chinese characters correctly, you need to modify the displaytag source code.
Download the source code of the same version at Org. displaytag. export. excelview. in the Java file, find the getmimetype () method and change this method to return "application/vnd. MS-Excel; charset = gb2312 ";, the data export speed will be much slower after modification, but it will be fine.
3. The new version of displaytag1.1 adds support for retrieving part of data at a time. Related labels include partiallist and size. You must set partiallist = "true" and size. I have not studied how to use it.

 

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.