您的位置首页生活百科

在Struts中使用displaytag标签的排序

在Struts中使用displaytag标签的排序

的有关信息介绍如下:

在Struts中使用displaytag标签的排序

  

  显示Struts的标签库是一个开放源码套件的定制标记 提供高层次的网络呈现方式 将工作在一个MVC模型 该库提供了一个重要的功能 同时还易于使用 displaytag标签可以处理列显示 排序 分页 裁剪 分组 出口 智能连接和一个可定制的XHTML样式表中的装饰     在下面的示例中 我们将看到如何获得收入数据显示标签 并做分页和排序 我们将使用Eclipse作为IDE在我们的例子中     第 步 创建Eclipse动态Web项目和JAR文件拷贝    启动Eclipse 转到文件 >新建 >项目 >动态Web项目

  

  以下是列表中所需的JAR文件被添加到您的项目的Java类路径 下载displaytag标签从JAR文件

  

  

  复制下面的内容到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; Worth = netWorth; } public ArrayList<ForbesData> loadData() { ArrayList<ForbesData> userList = new ArrayList<ForbesData>() userList add(new ForbesData( William Gates III )) userList add(new ForbesData( Warren Buffett )) userList add(new ForbesData( Carlos Slim Helu &amp; family )) userList add(new ForbesData( Lawrence Ellison )) userList add(new ForbesData( Ingvar Kamprad &amp; family )) userList add(new ForbesData( Karl Albrecht )) userList add(new ForbesData( Mukesh Ambani )) userList add(new ForbesData( Lakshmi Mittal )) userList add(new ForbesData( Theo Albrecht )) userList add(new ForbesData( Amancio Ortega )) userList add(new ForbesData( Jim Walton )) userList add(new ForbesData( Alice Walton )) userList add(new ForbesData( Christy Walton &amp; family )) userList add(new ForbesData( S Robson Walton )) userList add(new ForbesData( Bernard Arnault )) userList add(new ForbesData( Li Ka shing )) userList add(new ForbesData( Michael Bloomberg )) userList add(new ForbesData( Stefan Persson )) userList add(new ForbesData( Charles Koch )) userList add(new ForbesData( David Koch )) userList add(new ForbesData( Liliane Bettencourt )) userList add(new ForbesData( Prince Alwaleed Bin Talal Alsaud )) 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) { Worth = netWorth; } }    复制以下内容UserForm java上    package net viralpatel struts displaytag; import java util ArrayList; public class UserForm extends 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 HttpServletRequest; import javax servlet HttpServletResponse; import apache struts action Action; import apache struts action ActionForm; import apache struts action ActionForward; import 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) } }

  第 步 创建JSP struts config xml和web xml文件    在WebContent文件夹和struts config xml和web xml中的WebContent / WEB INF文件夹中创建index jsp和user jsp

  

  复制以下内容转换成相应的文件     index jsp的<%@page contentType= text/ %> <%@page pageEncoding= UTF %> //<jsp:forward page= userAction do /> user jsp<!DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN /TR/ /loose dtd > <%@taglib uri= prefix= display %> <> <head> <meta equiv= Content Type content= text/; charset=UTF > <title>The World s Billionaires </title> <link rel="nofollow" href= css/style css rel= stylesheet type= text/css /> </head> <body> <h >The World s Billionaires Forbes List <display:table export= true id= data name= sessionScope UserForm forbesList requestURI= /userAction do pagesize= > <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> </> struts config xml中<?xml version= encoding= UTF ?> <!DOCTYPE struts config PUBLIC //Apache Sofare Foundation//DTD Struts Configuration //EN config_ _ 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= wele path= /Wele 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= /Wele forward= /weleStruts jsp /> </action mappings> <message resources parameter= /vaannila/ApplicationResource /> </struts config> web xml中<?xml version= encoding= UTF ?> <web app version= xmlns= xmlns:xsi=/ /XMLSchema instance xsi:schemaLocation= app_ _ xsd > <servlet> <servlet name>action</servlet name> <servlet class> 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> </param value> </init param> <init param> <param name>detail</param name> <param value> </param value> </init param> <load on startup> </load on startup> </servlet> <servlet mapping> <servlet name>action</servlet name> <url pattern>* do</url pattern> </servlet mapping> <session config> <session timeout> </session timeout> </session config> <wele file list> <wele file>index jsp</wele file> </wele file list> </web app>    第 步 执行项目    我们已经完成了这个项目 现在执行的项目在Eclipse中创建一个WAR文件 并在Tomcat上运行