顯示Struts的標簽庫是一個開放源碼套件的定制標記提供高層次的網絡呈現方式將工作在一個MVC模型該庫提供了一個重要的功能同時還易於使用displaytag標簽可以處理列顯示排序分頁裁剪分組出口智能連接和一個可定制的XHTML樣式表中的裝飾
在下面的示例中我們將看到如何獲得收入數據顯示標簽並做分頁和排序我們將使用Eclipse作為IDE在我們的例子中
第步創建Eclipse動態Web項目和JAR文件拷貝
啟動Eclipse轉到文件 >新建 >項目 >動態Web項目
以下是列表中所需的JAR文件被添加到您的項目的Java類路徑下載displaytag標簽從JAR文件
第
步
創建行動
表格和Bean類
一旦項目被創建
創建
個java文件ForbesData的
UserAction和UserForm中包net
viralpatel
struts
displaytag
復制下面的內容到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 & family )) userListadd(new ForbesData( Lawrence Ellison )) userListadd(new ForbesData( Ingvar Kamprad & 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 & 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
復制以下內容轉換成相應的文件
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上運行
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28758.html