当前位置:编程学习 > JAVA >>

在Struts中使用displaytag标签的排序/分页数据

显示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 & family", 69, 35));
  userList.add(new ForbesData(4, "Lawrence Ellison", 64, 22.5));
  userList.add(new ForbesData(5, "Ingvar Kamprad & 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 & 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" >
  &nbs

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,