熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

在Struts中使用displaytag標簽的排序

2013-11-23 20:37:17  來源: Java開源技術 

  

  顯示Struts的標簽庫是一個開放源碼套件的定制標記提供高層次的網絡呈現方式將工作在一個MVC模型該庫提供了一個重要的功能同時還易於使用displaytag標簽可以處理列顯示排序分頁裁剪分組出口智能連接和一個可定制的XHTML樣式表中的裝飾
  
  在下面的示例中我們將看到如何獲得收入數據顯示標簽並做分頁和排序我們將使用Eclipse作為IDE在我們的例子中
  
  第創建Eclipse動態Web項目和JAR文件拷貝
  
  啟動Eclipse轉到文件 >新建 >項目 >動態Web項目

  Struts的動態Web項目

  以下是列表中所需的JAR文件被添加到您的項目的Java類路徑下載displaytag標簽從JAR文件

  displaytag標簽jar文件列表

  第創建行動表格和Bean類
  
  一旦項目被創建創建個java文件ForbesData的UserAction和UserForm中包netviralpatelstrutsdisplaytag

  
Struts的displaytag標簽的新Java的

  復制下面的內容到ForbesDatajava文件
  
  package netviralpatelstrutsdisplaytag; import javautilArrayList; 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) { thisrank = rank; thisname = name; thisage = age; Worth = netWorth; } public ArrayList<ForbesData> loadData() { ArrayList<ForbesData> userList = new ArrayList<ForbesData>() userListadd(new ForbesData( William Gates III )) userListadd(new ForbesData( Warren Buffett )) userListadd(new ForbesData( Carlos Slim Helu &amp; family )) userListadd(new ForbesData( Lawrence Ellison )) userListadd(new ForbesData( Ingvar Kamprad &amp; family )) userListadd(new ForbesData( Karl Albrecht )) userListadd(new ForbesData( Mukesh Ambani )) userListadd(new ForbesData( Lakshmi Mittal )) userListadd(new ForbesData( Theo Albrecht )) userListadd(new ForbesData( Amancio Ortega )) userListadd(new ForbesData( Jim Walton )) userListadd(new ForbesData( Alice Walton )) userListadd(new ForbesData( Christy Walton &amp; family )) userListadd(new ForbesData( S Robson Walton )) userListadd(new ForbesData( Bernard Arnault )) userListadd(new ForbesData( Li Kashing )) userListadd(new ForbesData( Michael Bloomberg )) userListadd(new ForbesData( Stefan Persson )) userListadd(new ForbesData( Charles Koch )) userListadd(new ForbesData( David Koch )) userListadd(new ForbesData( Liliane Bettencourt )) userListadd(new ForbesData( Prince Alwaleed Bin Talal Alsaud )) return userList; } public int getRank() { return rank; } public void setRank(int rank) { thisrank = rank; } public String getName() { return name; } public void setName(String name) { thisname = name; } public int getAge() { return age; } public void setAge(int age) { thisage = age; } public double getNetWorth() { return netWorth; } public void setNetWorth(double netWorth) { Worth = netWorth; } }
  
  復制以下內容UserFormjava上
  
  package netviralpatelstrutsdisplaytag; import javautilArrayList; public class UserForm extends orgapachestrutsactionActionForm { private ArrayList<ForbesData> forbesList; public ArrayList<ForbesData> getForbesList() { return forbesList; } public void setForbesList(ArrayList<ForbesData> forbesList) { thisforbesList = forbesList; } }
  
  復制以下內容UserActionjava上
  
  package netviralpatelstrutsdisplaytag; import javaxservlethttpHttpServletRequest; import javaxservlethttpHttpServletResponse; import orgapachestrutsactionAction; import orgapachestrutsactionActionForm; import orgapachestrutsactionActionForward; import orgapachestrutsactionActionMapping; 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() userFormsetForbesList(actorDataloadData()) return mappingfindForward(SUCCESS) } }

  第創建JSPstrutsconfigxml和webxml文件
  
  在WebContent文件夾和strutsconfigxml和webxml中的WebContent / WEBINF文件夾中創建indexjsp和userjsp

  的struts-displaytag標簽的網頁,XML,JSP

  復制以下內容轉換成相應的文件
  
  indexjsp的<%@page contentType=text/html%> <%@page pageEncoding=UTF%> //<jsp:forward page=userActiondo/> userjsp<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN /TR/html/loosedtd> <%@taglib uri= prefix=display %> <html> <head> <meta httpequiv=ContentType content=text/html; charset=UTF> <title>The Worlds Billionaires </title> <link href=css/stylecss rel=stylesheet type=text/css /> </head> <body> <h>The Worlds Billionaires Forbes List <display:table export=true id=data name=sessionScopeUserFormforbesList requestURI=/userActiondo 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> </html> strutsconfigxml中<?xml version= encoding=UTF ?> <!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd> <strutsconfig> <formbeans> <formbean name=UserForm type=netviralpatelstrutsdisplaytagUserForm/> </formbeans> <globalexceptions> </globalexceptions> <globalforwards> <forward name=welcome path=/Welcomedo/> </globalforwards> <actionmappings> <action input=/ name=UserForm path=/userAction scope=session type=netviralpatelstrutsdisplaytagUserAction> <forward name=success path=/userjsp /> </action> <action path=/Welcome forward=/welcomeStrutsjsp/> </actionmappings> <messageresources parameter=com/vaannila/ApplicationResource/> </strutsconfig> webxml中<?xml version= encoding=UTF?> <webapp version= xmlns= xmlns:xsi=//XMLSchemainstance xsi:schemaLocation= app__xsd> <servlet> <servletname>action</servletname> <servletclass> orgapachestrutsactionActionServlet </servletclass> <initparam> <paramname>config</paramname> <paramvalue>/WEBINF/strutsconfigxml</paramvalue> </initparam> <initparam> <paramname>debug</paramname> <paramvalue></paramvalue> </initparam> <initparam> <paramname>detail</paramname> <paramvalue></paramvalue> </initparam> <loadonstartup></loadonstartup> </servlet> <servletmapping> <servletname>action</servletname> <urlpattern>*do</urlpattern> </servletmapping> <sessionconfig> <sessiontimeout></sessiontimeout> </sessionconfig> <welcomefilelist> <welcomefile>indexjsp</welcomefile> </welcomefilelist> </webapp>
  
  第執行項目
  
  我們已經完成了這個項目現在執行的項目在Eclipse中創建一個WAR文件並在Tomcat上運行

  Struts的displaytag標簽的例子


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28758.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.