JavaBeans可索引屬性介紹 ----------程---------- public void setItems(PropertyType[] arrayItems); // array setter for items property public PropertyType[] getItems(); // array getter for items property public void setItems(int index, PropertyType newItem); // indexed setter for items property public PropertyType getItems(int index); // indexed getter for items property ------------------------ ----------程---------- public class ArrayBean { // properties private java.util.Vector Items = new java.util.Vector(); // array setter for items property public void setItems(String[] newItems) { int arraySize = newItems.length; Items.clear(); for(int i=0; i < arraySize; i++) Items.addElement(newItems[i]); } // array getter for items property public String[] getItems() { String[] arrayItems = new String[Items.size()]; Items.copyInto(arrayItems); return arrayItems; } // indexed setter for items property public void setItems(int index, String newItem) { try { Items.add(index, newItem); } catch(ArrayIndexOutOfBoundsException ai) { Items.addElement(newItem); } } // indexed getter for items property public String getItems(int index) { return (String)Items.elementAt(index); } } ------------------------ ----------程---------- <% String[] values=request.getParameterValues("多重選取控制項名稱"); %> ------------------------ ----------程---------- <%@ page language="java" contentType="text/html;charset=Big5" %> <% String[] ArrayValues = new String[] {"value-a", "value-2", "value-3"}; %> Indexed Property Test

<%=arrayBean.getItems(0) %>

<%=arrayBean.getItems(1) %>

<%=arrayBean.getItems(2) %>

------------------------ ----------程---------- ------------------------ ----------程---------- ------------------------ ----------程---------- http://localhost:8080/runpc/IndexedProperty.jsp ------------------------ ----------程---------- http://localhost:8080/runpc/IndexedProperty.jsp?items=item-2&items=item-3&items=item-5 ------------------------ ----------程---------- ------------------------ ----------程---------- package clt.beans; import java.io.Serializable; import java.net.URLDecoder; import java.util.ArrayList; import java.util.StringTokenizer; public class eCartBean implements Serializable { private String action = null; private ArrayList itemsList = new ArrayList(); /* method to set the action: add:納入, remove:移除, or clear:清空 */ public synchronized void setAction(String newAction) { action = URLDecoder.decode(newAction); // 將經過URL編碼動作之中文字串參數還原回來 if(action.startsWith("清空")) { // 清空電子推車 itemsList.clear(); } } public ArrayList getItemsList() { return itemsList; } // public synchronized void setItemsSelected(String[] newItems) { if(newItems != null) { for(int i = 0; i < newItems.length; i++) { StringTokenizer token = new StringTokenizer(newItems[i], "^"); String code = (String)token.nextElement(); String name = (String)token.nextElement(); String quantity = (String)token.nextElement(); String[] arrayItem = new String[] {code, name, quantity}; if(action.startsWith("納入")) { // 納入電子推車 itemsList.add(arrayItem); } else if (action.startsWith("移除")) { // 移除這些物品 int index = itemsList.lastIndexOf(arrayItem); if(index != -1) { itemsList.remove(index); } } } // Next } } // public int getItemsCount() { return itemsList.size(); } // public String getCode(int index) { return ((String[])itemsList.get(index))[0]; } // public String getName(int index) { return ((String[])itemsList.get(index))[1]; } // public Integer getQuantity(int index) { return new Integer(((String[])itemsList.get(index))[2]); } // public String getCartXML() { int Count = itemsList.size(); StringBuffer buf = new StringBuffer(); String rootTag = "ROWSET"; String rowTag = "ROW"; buf.append('<'); buf.append(rootTag); buf.append(">\n"); for(int i=0; i < Count; i++) { buf.append('<'); buf.append(rowTag); buf.append(">\n"); buf.append(""); buf.append(getCode(i)); buf.append(""); buf.append(""); buf.append(getName(i)); buf.append(""); buf.append(""); buf.append(getQuantity(i)); buf.append("\n"); buf.append("\n"); } // Next buf.append("\n"); return (buf.toString()); } // public String getCartXML_tw() { int Count = itemsList.size(); StringBuffer buf = new StringBuffer(); String rootTag = "ROWSET"; String rowTag = "ROW"; buf.append('<'); buf.append(rootTag); buf.append(">\n"); for(int i=0; i < Count; i++) { buf.append('<'); buf.append(rowTag); buf.append(">\n"); buf.append("<藥品編號>"); buf.append(getCode(i)); buf.append(""); buf.append("<藥品名稱>"); buf.append(getName(i)); buf.append(""); buf.append("<數量>"); buf.append(getQuantity(i)); buf.append("\n"); buf.append("\n"); } // Next buf.append("\n"); return (buf.toString()); } } // end eCartBean ------------------------ ----------程---------- javac -d . eCartBean.java ------------------------ ----------程---------- ------------------------ ----------程---------- <%= cart.getCartXML_tw() %> 或 ------------------------ ----------程---------- <%@ page language="java" contentType="text/html;charset=Big5" %> 電子推車Bean元件測試

請選取下列試購藥品:

勾買用量現有數量藥品名稱藥品編號
100 MEBLIN A032002100
50 MEQUIT TAB. 5MG A038873100
30 MEKIN TABLETS 5MG "S.C." A039407100
60 DIMETINE TABLETS 4MG A039688100
10 MEQUITINE TABLETS 5MG"WINSTON" A042538100


<% int numItems = cart.getItemsCount(); %>

您的電子推車目前<%= (numItems > 0) ? "置入物品共有 " +numItems +" 項" : "沒有" %>物品

    <% for (int i = 0; i < numItems; i ++ ) { %>
  • 編號[<%=cart.getCode(i)%>]藥品,計 <%=cart.getQuantity(i)%> 項 <% } %>
------------------------ ----------程---------- http://localhost:8080/runpc/eCartBeanTest.jsp ------------------------ ----------程---------- e起購網站藥品訂貨系統

歡迎e起購



請選取下列購買藥品:

------------------------ ----------程----------
------------------------ ----------程----------
購買數量現有數量藥品名稱藥品編號
------------------------ ----------程----------

? = &
------------------------ ----------程---------- <%@ page contentType="text/html; charset=Big5" %> <%@taglib uri="http://jakarta.apache.org/taglibs/xsl-1.0" prefix="xsl" %> <%-- XSLT Transform --%> <%=orderXML%>