接上例:http://blog.csdn.net/kunshan_shenbin/archive/2008/11/14/3300058.aspx
这回演示一下T3中比较难用的控件:Table组件(包括翻页组件)的用法:
如下图所示建立工程:
jar包和上例一致。
代码如下:
Global.java
- package mo.org.cpttm.phonebook;
- public class Global {
- public PhoneBookTable phoneBookTable;
- public PhoneBook phoneBook;
- public Global() {
- phoneBookTable = new PhoneBookTable();
- phoneBookTable.insert(new PhoneBookEntry(0, new Name("Alan", "Turing"), "111111"));
- phoneBookTable.insert(new PhoneBookEntry(1, new Name("Bill", "Gates"), "111222"));
- phoneBookTable.insert(new PhoneBookEntry(2, new Name("Martin", "Fowler"), "654321"));
- phoneBookTable.insert(new PhoneBookEntry(3, new Name("Kent", "Beck"), "999001"));
- phoneBookTable.insert(new PhoneBookEntry(4, new Name("Howard", "Ship"), "554433"));
- phoneBookTable.insert(new PhoneBookEntry(5, new Name("Linus", "Torvalds"), "888777"));
- phoneBookTable.insert(new PhoneBookEntry(6, new Name("Ward", "Cunningham"), "222222"));
- phoneBookTable.insert(new PhoneBookEntry(7, new Name("James", "Gosling"), "333333"));
- phoneBookTable.insert(new PhoneBookEntry(8, new Name("Erich", "Gamma"), "4444444"));
- phoneBookTable.insert(new PhoneBookEntry(9, new Name("Grady", "Booch"), "5555555"));
- phoneBookTable.insert(new PhoneBookEntry(10, new Name("Bruce", "Eckel"), "6666666"));
- phoneBookTable.insert(new PhoneBookEntry(11, new Name("Richard", "Stallman"), "777777"));
- phoneBook = new PhoneBook(phoneBookTable);
- }
- }
Home.java
- package mo.org.cpttm.phonebook;
- import java.util.*;
- import org.apache.tapestry.*;
- import org.apache.tapestry.contrib.table.components.*;
- import org.apache.tapestry.contrib.table.model.*;
- import org.apache.tapestry.contrib.table.model.simple.*;
- import org.apache.tapestry.html.*;
- public class Home extends BasePage {
- public List getSource() {
- return ((Global) getGlobal()).phoneBook.getEntries();
- }
- public IBasicTableModel getModel() {
- final PhoneBook phoneBook = ((Global) getGlobal()).phoneBook;
- return new IBasicTableModel() {
- public int getRowCount() {
- return phoneBook.getNoEntries();
- }
- public Iterator getCurrentPageRows(int nFirst, int nPageSize,
- ITableColumn objSortColumn, boolean bSortOrder) {
- String sortColumnId = objSortColumn == null ? null
- : objSortColumn.getColumnName();
- return phoneBook.getSomeEntries(nFirst, nPageSize,
- sortColumnId,
- bSortOrder == ITableSortingState.SORT_ASCENDING)
- .iterator();
- }
- };
- }
- public void onShowDetails(IRequestCycle cycle) {
- Integer id = (Integer) cycle.getServiceParameters()[0];
- System.out.println("Showing details for " + id.intValue());
- }
- public void onDelete(IRequestCycle cycle) {
- TableView table = (TableView) getComponent("table");
- TableRows rows = (TableRows)getComponent("rows");
- int id = ((PhoneBookEntry) rows.getTableRow()).getId();
- System.out.println("Deleting " + id);
- PhoneBook phoneBook = ((Global) getGlobal()).phoneBook;
- phoneBook.deleteEntry(id);
- SimpleTableModel tableModel = ((SimpleTableModel) table.getTableModel());
- tableModel.setDataModel(new SimpleListTableDataModel((List)table.getBinding("source").getObject()));
- tableModel.tableDataChanged(null);
- table.fireObservedStateChange();
- }
- }
Name.java
- package mo.org.cpttm.phonebook;
- import java.io.*;
- public class Name implements Serializable {
- private String firstName;
- private String lastName;
- public Name(String firstName, String lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
- public String getFirstName() {
- return firstName;
- }
- public String getLastName() {
- return lastName;
- }
- }
PhoneBook.java
- package mo.org.cpttm.phonebook;
- import java.util.*;
- public class PhoneBook {
- private PhoneBookTable table;
- public PhoneBook(PhoneBookTable table) {
- this.table = table;
- }
- public List getEntries() {
- List result = new ArrayList();
- ResultSet rs = table.selectAll();
- while (rs.next()) {
- result.add(rs.readEntry());
- }
- return result;
- }
- public int getNoEntries() {
- return table.selectCount();
- }
- public List getSomeEntries(
- int startIdx,
- int noEntries,
- String sortColumnId,
- boolean isAscending) {
- ResultSet rs = table.selectLimit(
- startIdx,
- noEntries,
- sortColumnId,
- isAscending);
- List result = new ArrayList();
- while (rs.next()) {
- result.add(rs.readEntry());
- }
- return result;
- }
- public void deleteEntry(int id) {
- table.delete(id);
- }
- }
PhoneBookEntry.java
- package mo.org.cpttm.phonebook;
- import java.io.*;
- public class PhoneBookEntry implements Serializable {
- private int id;
- private Name name;
- private String telNo;
- public PhoneBookEntry(int id, Name name, String telNo) {
- this.id = id;
- this.name = name;
- this.telNo = telNo;
- }
- public Name getName() {
- return name;
- }
- public int getId() {
- return id;
- }
- public String getTelNo() {
- return telNo;
- }
- public String getFirstName() {
- return name.getFirstName();
- }
- }
PhoneBookEntryComparator.java
- package mo.org.cpttm.phonebook;
- import java.util.*;
- public class PhoneBookEntryComparator implements Comparator {
- private String columnName;
- public PhoneBookEntryComparator(String columnName) {
- this.columnName = columnName;
- }
- public int compare(Object obj1, Object obj2) {
- PhoneBookEntry entry1 = (PhoneBookEntry) obj1;
- PhoneBookEntry entry2 = (PhoneBookEntry) obj2;
- if (columnName.equals("id")) {
- return entry1.getId()-entry2.getId();
- } else if (columnName.equals("firstName")) {
- return entry1.getFirstName().compareTo(entry2.getFirstName());
- } else {
- //Similar for other columns.
- return 0;
- }
- }
- }
PhoneBookTable.java
- package mo.org.cpttm.phonebook;
- import java.util.*;
- public class PhoneBookTable {
- private List entries;
- public PhoneBookTable() {
- entries = new ArrayList();
- }
- public void insert(PhoneBookEntry entry) {
- entries.add(entry);
- }
- public ResultSet selectAll() {
- System.out.println("Selecting all");
- return new ResultSet(entries);
- }
- public int selectCount() {
- return entries.size();
- }
- public ResultSet selectLimit(
- int startIdx,
- int noRecords,
- String columnName,
- boolean isAscending) {
- System.out.println("Selecting with limit");
- List sortedEntries = new ArrayList(entries);
- if (columnName != null) {
- Collections.sort(sortedEntries, new PhoneBookEntryComparator(
- columnName));
- if (!isAscending) {
- Collections.reverse(sortedEntries);
- }
- }
- List subList = sortedEntries.subList(startIdx, startIdx + noRecords);
- return new ResultSet(subList);
- }
- public void delete(int id) {
- for (Iterator iter = entries.iterator(); iter.hasNext();) {
- PhoneBookEntry entry = (PhoneBookEntry) iter.next();
- if (entry.getId()==id) {
- iter.remove();
- return;
- }
- }
- }
- }
ResultSet.java
- package mo.org.cpttm.phonebook;
- import java.util.*;
- public class ResultSet {
- private List entriesInResultSet;
- private int currentIdx;
- public ResultSet(List entriesInResultSet) {
- this.entriesInResultSet = entriesInResultSet;
- this.currentIdx = -1;
- }
- public boolean next() {
- if (currentIdx < entriesInResultSet.size() - 1) {
- currentIdx++;
- return true;
- } else {
- return false;
- }
- }
- public PhoneBookEntry readEntry() {
- PhoneBookEntry entry = (PhoneBookEntry) entriesInResultSet.get(currentIdx);
- System.out.println("Reading entry with id " + entry.getId());
- return entry;
- }
- }
PhoneBook.css
- tr.odd {background-color: RoyalBlue}
- tr.even {background-color: GreenYellow}
- th.title {background-color: Pink}
Home_zh_TW.properties
- firstName=?
Home.html
- <html jwcid="@Shell" title="Phone Book" stylesheet="ognl:assets.phoneBook">
- <head jwcid="$remove$">
- <title>Phone Book</title>
- <link rel="stylesheet" text="text/css" href="../css/PhoneBook.css"/>
- </head>
- <body>
- <span jwcid="table">
- <table border="1">
- <tr><span jwcid="@Contrib:TableColumns" class="title"/></tr>
- <tr jwcid="rows@Contrib:TableRows" class="ognl:beans.evenOdd.next"><td jwcid="@Contrib:TableValues"/></tr>
- </table>
- <span jwcid="@Contrib:TablePages"/>
- </span>
- <table border="1" jwcid="$remove$">
- <tr><th>ID</th><th>First name</th><th>Last name</th><th>Tel #</th><th>Delete</th></tr>
- <tr class="odd">
- <td>1</td>
- <td>Britney</td>
- <td>Spears</td>
- <td>376926</td>
- <td>
- <form>
- <input type="submit" value="Delete"/>
- </form>
- </td>
- </tr>
- <tr class="even"><td>2</td><td>Elton</td><td>John</td><td>285984</td><td>Delete</td></tr>
- <tr class="odd"><td>3</td><td>David</td><td>Letterman</td><td>877357</td><td>Delete</td></tr>
- </table>
- <span jwcid="deleteColumnValue">
- <form jwcid="deleteForm">
- <input type="submit" value="Delete"/>
- </form>
- </span>
- <span jwcid="firstNameColumnValue">
- <a href="" jwcid="firstNameLink"><span jwcid="firstName">Britney</span></a>
- </span>
- </body>
- </html>
Home.page
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE page-specification PUBLIC
- "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
- "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
- <!-- generated by Spindle, http://spindle.sourceforge.net -->
- <page-specification class="mo.org.cpttm.phonebook.Home">
- <context-asset name="phoneBook" path="/css/PhoneBook.css"/>
- <bean name="evenOdd" class="org.apache.tapestry.bean.EvenOdd">
- <set-property name="even" expression="false"/>
- </bean>
- <component id="table" type="Contrib:TableView">
- <binding name="source" expression="source"/>
- <binding name="tableSessionStateManager" expression="new org.apache.tapestry.contrib.table.model.common.FullTableSessionStateManager()"/>
- <static-binding
- name="columns"
- value="id, firstName, lastName:name.lastName, telNo, !delete"/>
- <static-binding name="pageSize" value="3"/>
- </component>
- <component id="deleteColumnValue" type="Block"/>
- <component id="deleteForm" type="Form">
- <binding name="listener" expression="listeners.onDelete"/>
- <binding name="direct" expression="false"/>
- </component>
- <component id="firstNameColumnValue" type="Block"/>
- <component id="firstNameLink" type="DirectLink">
- <binding name="listener" expression="listeners.onShowDetails"/>
- <binding name="parameters" expression="components.rows.tableRow.id"/>
- </component>
- <component id="firstName" type="Insert">
- <binding name="value" expression="components.rows.tableRow.firstName"/>
- </component>
- </page-specification>
Home.properties
- firstName=First name
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE application PUBLIC
- "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
- "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
- <!-- generated by Spindle, http://spindle.sourceforge.net -->
- <application name="PhoneBook" engine-class="org.apache.tapestry.engine.BaseEngine">
- <property name="org.apache.tapestry.global-class" value="mo.org.cpttm.phonebook.Global"/>
- <page name="Home" specification-path="Home.page"/>
- <library id="Contrib" specification-path="/org/apache/tapestry/contrib/Contrib.library"/>
- </application>
web.xml
- <?xml version="1.0"?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/TR/xmlschema-1/"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
- <display-name>PhoneBook</display-name>
- <servlet>
- <servlet-name>PhoneBook</servlet-name>
- <servlet-class>org.apache.tapestry.ApplicationServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>PhoneBook</servlet-name>
- <url-pattern>/app</url-pattern>
- </servlet-mapping>
- </web-app>