📄 cartitemstag.java
字号:
package wls8unleashed.testing;
import java.io.IOException;
import java.util.Vector;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.IterationTag;
public class CartItemsTag extends BodyTagSupport
{
private int count;
private ShoppingCart cart;
private Vector items;
public CartItemsTag()
{
}
/**
* Called by the container when the start tag is processed, but
* before any body contents have been read
*/
public int doStartTag() throws JspException
{
// returning EVAL_BODY_BUFFERED will cause
// doInitBody() to be invoked before the first
// pass through the body of the tag
return BodyTagSupport.EVAL_BODY_BUFFERED;
}
/**
* Called when the closing tag is read
*/
public int doEndTag() throws JspException
{
return BodyTagSupport.EVAL_PAGE;
}
/**
* Initializes the values that will be dynamically
* inserted into the body of the tag
*/
public void doInitBody() throws JspException
{
count = 0;
cart = (ShoppingCart)pageContext.findAttribute
(ShoppingCart.class.getName());
items = cart.getItems();
// initialize scripting variables for display on page
setScriptingVariables();
}
/**
* Called after each successive processing of the body contents
*/
public int doAfterBody() throws JspException
{
// then increment counter
count++;
// and decide to continue iterating or not
if (count < items.size())
{
setScriptingVariables();
return BodyTagSupport.EVAL_BODY_AGAIN;
}
else
{
// after all iterations are complete, send the whole thing
try
{
System.out.println(bodyContent.getString());
bodyContent.writeOut(bodyContent.getEnclosingWriter());
}
catch (IOException e)
{
}
// and continue on with the rest of the page
return BodyTagSupport.SKIP_BODY;
}
}
private void setScriptingVariables()
{
if (count >= items.size())
return;
CartItem item = (CartItem)items.get(count);
pageContext.setAttribute("item_name",
new String(item.getName()));
pageContext.setAttribute("item_quantity",
new Integer(item.getQuantity()));
pageContext.setAttribute("item_unit_price",
new Float(item.getUnitPrice()));
pageContext.setAttribute("item_price",
new Float(item.getUnitPrice() * item.getQuantity()));
}
/**
* Called when this tag exits to free any resources
*/
public void release()
{
cart = null;
items = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -