建立表单参数和Bean属性之间的关联





Using jsp:setProperty






Using jsp:setProperty




name="entry"
property="itemID"
value="<%=request.getParameter("itemID")%>" />

name="entry"
property="numItems"
param="numItems" />


name="entry"
property="discountCode"
param="discountCode" />






Item IDUnit PriceNumber OrderedTotal Price

$

$





package bean;

/** Simple bean to illustrate the various forms
* of jsp:setProperty.
*/

public class SaleEntry {
private String itemID = "unknown";
private double discountCode = 1.0;
private int numItems = 0;

public String getItemID() {
return(itemID);
}

public void setItemID(String itemID) {
if (itemID != null) {
this.itemID = itemID;
} else {
this.itemID = "unknown";
}
}

public double getDiscountCode() {
return(discountCode);
}

public void setDiscountCode(double discountCode) {
this.discountCode = discountCode;
}

public int getNumItems() {
return(numItems);
}

public void setNumItems(int numItems) {
this.numItems = numItems;
}

// Replace this with real database lookup.

public double getItemCost() {
double cost;
if (itemID.equals("a1234")) {
cost = 12.99*getDiscountCode();
} else {
cost = -9999;
}
return(roundToPennies(cost));
}

private double roundToPennies(double cost) {
return(Math.floor(cost*100)/100.0);
}

public double getTotalCost() {
return(getItemCost() * getNumItems());
}
}

你可能感兴趣的:(Servlet,/,JSP)