Use the display tag library to show the table in Java EE

Source: Internet
Author: User
Tags net return string sort
When you use a Web page to display a table, if you have too many rows, you sometimes need to divide them into many pages. And each row uses a different background color to facilitate the user to read. Or you might want to sort. While it's easy to implement the above features, using the Display tag library will greatly simplify development. It mimics the style of the Google,baidu page, dividing many rows of tables into individual pages and providing common functionality.

The data model is very simple for the US President JavaBean. It has 3 simple string attributes.

The Java code is as follows:

Pageddata.java

Import java.util.ArrayList;
Import java.util.List;

public class Pageddata {

Private list List;

Public Pageddata () {
List = new ArrayList ();
List.add (New President ("Garfield", "James", "1881"));
List.add (New President ("Arthur", "Chester", "1881-85"));
List.add (New President ("Cleveland", "Grover", "1885-89"));
List.add (New President ("Harrison", "Benjamin", "1889-93"));
List.add (New President ("Cleveland", "Grover", "1893-97"));
List.add (New President ("McKinley", "William", "1897-1901"));
List.add (New President ("Roosevelt", "Theodore", "1901-09"));
List.add (New President ("Taft", "William H.", "1909-13"));
List.add (New President ("Wilson", "Woodrow", "1913-21"));
List.add (New President ("Jackson", "Andrew", "1829-37");
List.add (New President ("Harding", "Warren", "1921-23"));
List.add (New President ("Coolidge", "Calvin", "1923-29"));
List.add (New President ("Hoover", "Herbert", "1929-33"));
List.add (New President ("Roosevelt", "Franklin D.", "1933-45"));
List.add (New President ("Truman", "Harry", "1945-53"));
List.add (New President ("Eisenhower", "Dwight", "1953-61"));
List.add (New President ("Kennedy", "John F.", "1961-63"));
List.add (New President ("Johnson", "Lyndon", "1963-69"));
List.add (New President ("Nixon", "Richard", "1969-74"));
List.add (New President ("Ford", "Gerald", "1974-77"));
List.add (New President ("Carter", "Jimmy", "1977-81"));
List.add (New President ("Reagan", "Ronald", "1981-89"));
List.add (New President ("Bush", "George h.w", "1989-93"));
List.add (New President ("Clinton", "William J.", "1993-2001"));
List.add (New President ("Bush", "George W.", "2001-present"));
List.add (New President ("Washington", "George", "1789-97"));
List.add (New President ("Adams", "John", "1797-1801"));
List.add (New President ("Jefferson", "Thomas", "1801-09"));
List.add (New President ("Madison", "James", "1809-17"));
List.add (New President ("Monroe", "James", "1817-25"));
List.add (New President ("Jackson", "Andrew", "1829-37");
List.add (New President ("Van Buren", "Martin", "1837-41"));
List.add (New President ("Harrison", "William Henry", "1841"));
List.add (New President ("Tyler", "John", "1841-45"));
List.add (New President ("Polk", "James", "1845-49"));
List.add (New President ("Taylor", "Zachary", "1849-50"));
List.add (New President ("Fillmore", "Millard", "1850-53"));
List.add (New President ("Pierce", "Franklin", "1853-57"));
List.add (New President ("Buchanan", "James", "1857"));
List.add (New President ("Lincoln", "Abraham", "1861-65"));
List.add (New President ("Johnson", "Andrew", "1865-69");
List.add (New President ("Grant", "Ulysses S.", "1869-77"));
List.add (New President ("Hayes", "Rutherford B.", "1877-81"));
}
Public List GetData () {
return list;
}
}

President.java

public class President {
Public President (String lname, String fname, string term) {
LastName = lname;
FirstName = fname;
this.term = term;
}

Public String Getfirstname () {
return firstName;
}
public void Setfirstname (String firstName) {
This.firstname = FirstName;
}
Public String Getlastname () {
return lastName;
}
public void Setlastname (String lastName) {
This.lastname = LastName;
}
Public String Getterm () {
return term;
}
public void Setterm (String term) {
this.term = term;
}

Private String LastName;
Private String FirstName;
Private String term;
}
The following JSP page is a presentation table, and also represents the most common usage of the display library:

index.jsp

<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>
<%@ taglib uri= "http://displaytag.sf.net/el" prefix= "Display"%>
<title> Struts cookbook-chapter 4:display Tag Example </title>
<style>
. even {Background-color:orange}
. odd {Background-color:yellow}
</style>
<body>
<jsp:usebean id= "Pageddata" class= "Pageddata"
<display:table id= "pres" Name= "${pageddata.data}"
sort= "List" pagesize= "Ten" defaultsort= "3"
<display:caption> United States Presidents </display:caption>
<display:setproperty name= "Basic.show.header" value= "true"/>
<display:column property= "FirstName" title= "Name"
Sortable= "true"/>
<display:column property= "LastName" title= "Last Name"
Sortable= "true"/>
<display:column property= "term" title= "term of Office"
Sortable= "true"/>
</display:table>
</body>
To open a page in a browser:



See, the effect is really good:

To use the Display tag library, you need to download it here:

Http://displaytag.sourceforge.net

Put the Display.jar file in the Web-inf/lib.

Attention:

El is used here, so the Jstl.jar and Standard.jar libraries need to be in lib.

Display.jar relies on 2.0 or more of the Jakarta Commons Lang Library, Commons-lang-2.0.jar and Jakarta Commons collections Libraries, Commons-collections.jar.

They were in:

Http://jakarta.apache.org/commons and http://jakarta.apache.org/commons/collections/

Download, and then copy the corresponding jar file to the web-inf/lib.

Simple introduction of usage, in fact, do not need me to say, look at the JSP file also basic understand.

<display:table id= "pres" Name= "${pageddata.data}"
sort= "List" pagesize= "Ten" defaultsort= "3"

The ID is the variable to be used later. Name is the collection data that you want to show. List indicates that the entire list is sorted. pagesize represents the number of times each page is to be displayed. DEFAULTSORT says it starts with the first few columns, and note that this is counted at 1.

<display:caption> United States Presidents </display:caption>
The string in the middle of the isplay:caption tag is the caption that is used to put it above the table.

<display:column property= "FirstName" title= "Name"
Sortable= "true"/>
The Display:column tag specifies the properties for each column.

For more use, see the DOC documentation for the display tag library.

Related Article

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.