Sorting/paging data using displaytag labels in Struts

Source: Internet
Author: User

The display Struts tag library is a custom tag of the open source code suite. It provides a high-level network presentation mode and works in an MVC model. This library provides an important feature and is easy to use. Displaytag can process Column Display, sorting, paging, cropping, grouping, egress, smart connection, and Decoration in a customizable XHTML style sheet.

In the following example, we will see how to obtain the revenue data display tag and perform paging and sorting. We will use Eclipse as the IDE in our example.

Step 2: Create an Eclipse dynamic Web project and copy JAR files
Start Eclipse and go to File> New> Project> dynamic Web project

Step 2: create actions, tables, and Bean classes
Once the project is created, create three java files ForbesData. The net. viralpatel. struts. displaytag in UserAction and UserForm.


Copy the following content to the ForbesData. java file.

Package net. viralpatel. struts. displaytag;

Import java. util. ArrayList;

Public class ForbesData {
Private int rank;
Private String name;
Private int age;
Private double netWorth;

Public ForbesData (){

}
 
Public ForbesData (int rank, String name, int age, double netWorth ){
This. rank = rank;
This. name = name;
This. age = age;
This. netWorth = netWorth;
}
Public ArrayList <ForbesData> loadData (){
ArrayList <ForbesData> userList = new ArrayList <ForbesData> ();
UserList. add (new ForbesData (1, "William Gates III", 53, 40.0 ));
UserList. add (new ForbesData (2, "Warren Buffett", 78, 37 ));
UserList. add (new ForbesData (3, "Carlos Slim Helu & amp; family", 69, 35 ));
UserList. add (new ForbesData (4, "Lawrence Ellison", 64, 22.5 ));
UserList. add (new ForbesData (5, "Ingvar Kamprad & amp; family", 83, 22 ));
UserList. add (new ForbesData (6, "Karl Albrecht", 89, 21.5 ));
UserList. add (new ForbesData (7, "Mukesh Ambani", 51, 19.5 ));
UserList. add (new ForbesData (8, "Lakshmi Mittal", 58, 19.3 ));
UserList. add (new ForbesData (9, "Theo Albrecht", 87, 18.8 ));
UserList. add (new ForbesData (10, "Amancio Ortega", 73, 18.3 ));
UserList. add (new ForbesData (11, "Jim Walton", 61, 17.8 ));
UserList. add (new ForbesData (12, "Alice Walton", 59, 17.6 ));
UserList. add (new ForbesData (12, "Christy Walton & amp; family", 54, 17.6 ));
UserList. add (new ForbesData (12, "S Robson Walton", 65, 17.6 ));
UserList. add (new ForbesData (15, "Bernard Arnault", 60, 16.5 ));
UserList. add (new ForbesData (16, "Li Ka-shing", 80, 16.2 ));
UserList. add (new ForbesData (17, "Michael Bloomberg", 67, 16 ));
UserList. add (new ForbesData (18, "Stefan Persson", 61, 14.5 ));
UserList. add (new ForbesData (19, "Charles Koch", 73, 14 ));
UserList. add (new ForbesData (19, "David Koch", 68, 14 ));
UserList. add (new ForbesData (21, "Liliane Bettencourt", 86, 13.4 ));
UserList. add (new ForbesData (22, "Prince Alwaleed Bin Talal Alsaud", 54, 13.3 ));
Return userList;
}
Public int getRank (){
Return rank;
}
Public void setRank (int rank ){
This. rank = rank;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getAge (){
Return age;
}
Public void setAge (int age ){
This. age = age;
}
Public double getNetWorth (){
Return netWorth;
}
Public void setNetWorth (double netWorth ){
This. netWorth = netWorth;
}
}
Copy the following content on UserForm. java

Package net. viralpatel. struts. displaytag;

Import java. util. ArrayList;

Public class UserForm extends org. apache. struts. action. ActionForm {

Private ArrayList <ForbesData> forbesList;

Public ArrayList <ForbesData> getForbesList (){
Return forbesList;
}

Public void setForbesList (ArrayList <ForbesData> forbesList ){
This. forbesList = forbesList;
}
}
Copy the following content on UserAction. java

Package net. viralpatel. struts. displaytag;

Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;

Import org. apache. struts. action. Action;
Import org. apache. struts. action. ActionForm;
Import org. apache. struts. action. ActionForward;
Import org. apache. struts. action. ActionMapping;

Public class UserAction extends Action {
 
Private final static String SUCCESS = "success ";
 
Public ActionForward execute (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
UserForm userForm = (UserForm) form;
ForbesData actorData = new ForbesData ();
UserForm. setForbesList (actorData. loadData ());
Return mapping. findForward (SUCCESS );
}

}
Step 2: Create JSP, struts-config.xml, and web. xml files
Create index. jsp and user. jsp in the WebContent folder and the WebContent/struts-config.xml folder in the WEB-INF and web. xml.


Copy the following content to convert it to the corresponding file.

Index. jsp
<% @ Page contentType = "text/html" %>
<% @ Page pageEncoding = "UTF-8" %>
// Original text translated by www. software8.co webmaster
<Jsp: forward page = "userAction. do"/>
User. jsp
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"
Http://www.w3.org/TR/html4/loose.dtd>
<% @ Taglib uri = "http://displaytag.sf.net" prefix = "display" %>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> The World's billion00002009 </title>
<Link href = "css/style.css" rel = "stylesheet" type = "text/css"/>
</Head>
<Body>
<H2> The World's billionths 2009-Forbes List <Display: table export = "true" id = "data"
Name = "sessionScope. UserForm. forbesList"
RequestURI = "/userAction. do" pagesize = "10">
<Display: column property = "rank" title = "Rank" sortable = "true"/>
<Display: column property = "name" title = "Name" sortable = "true"/>
<Display: column property = "age" title = "Age" sortable = "true"/>
<Display: column property = "netWorth" title = "Net worth ($ BIL )"
Sortable = "true"/>
</Display: table>
</Body>
</Html>
Struts-config.xml in
<? Xml version = "1.0" encoding = "UTF-8"?>

<! DOCTYPE struts-config PUBLIC
"-// Apache Software Foundation // DTD Struts Configuration 1.2 // EN"
Http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd>


<Struts-config>
<Form-beans>
<Form-bean name = "UserForm"
Type = "net. viralpatel. struts. displaytag. UserForm"/>
</Form-beans>

<Global-exceptions>

</Global-exceptions>

<Global-forwards>
<Forward name = "welcome" path = "/Welcome. do"/>
</Global-forwards>

<Action-mappings>
<Action input = "/" name = "UserForm" path = "/userAction"
Scope = "session" type = "net. viralpatel. struts. displaytag. UserAction">
<Forward name = "success" path = "/user. jsp"/>
</Action>
<Action path = "/Welcome" forward = "/welcomeStruts. jsp"/>
</Action-mappings>

<Message-resources parameter = "com/vaannila/ApplicationResource"/>
 
</Struts-config>
In web. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<Servlet>
<Servlet-name> action </servlet-name>
<Servlet-class>
Org. apache. struts. action. ActionServlet
</Servlet-class>
<Init-param>
<Param-name> config </param-name>
<Param-value>/WEB-INF/struts-config.xml </param-value>
</Init-param>
<Init-param>
<Param-name> debug </param-name>
<Param-value> 2 </param-value>
</Init-param>
<Init-param>
<Param-name> detail </param-name>
<Param-value> 2 </param-value>
</Init-param>
<Load-on-startup> 2 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-name> action </servlet-name>
<Url-pattern> *. do </url-pattern>
</Servlet-mapping>
<Session-config>
<Session-timeout> 30 </session-timeout>
</Session-config>
<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>
Step 2: execute the project
We have completed this project. The current project creates a WAR file in Eclipse and runs on Tomcat.

 

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.