在Struts中使用displaytag標簽的排序/分頁數據 – JAVA編程語言程序開發技術文章

顯示Struts的標簽庫是一個開放源碼套件的定制標記,提供高層次的網絡呈現方式,將工作在一個MVC模型。該庫提供瞭一個重要的功能,同時還易於使用。displaytag標簽可以處理列顯示,排序,分頁,裁剪,分組,出口,智能連接和一個可定制的XHTML樣式表中的裝飾。

在下面的示例中,我們將看到如何獲得收入數據顯示標簽,並做分頁和排序。我們將使用Eclipse作為IDE在我們的例子中。

第1步:創建Eclipse動態Web項目和JAR文件拷貝
啟動Eclipse,轉到文件 – >新建 – >項目 – >動態Web項目
displaytag標簽jar文件列表

 第2步:創建行動,表格和Bean類
一旦項目被創建,創建3個java文件ForbesData的,UserAction和UserForm中包net.viralpatel.struts.displaytag。

Struts的displaytag標簽的新Java的
復制下面的內容到ForbesData.java文件。

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;
 }
}
復制以下內容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;
 }
}
復制以下內容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);
    }
     
}
第3步:創建JSP,struts-config.xml和web.xml文件
在WebContent文件夾和struts-config.xml和web.xml中的WebContent / WEB-INF文件夾中創建index.jsp和user.jsp。

的struts-displaytag標簽的網頁,XML,JSP
復制以下內容轉換成相應的文件。

index.jsp的
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
//原文由 www.software8.co 站長百科譯
<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 Billionaires 2009</title>
        <link href="css/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <h2>The World's Billionaires 2009 – Forbes List</h2>
        <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中
<?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>
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>
第4步:執行項目
我們已經完成瞭這個項目。現在執行的項目在Eclipse中創建一個WAR文件,並在Tomcat上運行。

 Struts的displaytag標簽的例子

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *