📄 recursiontag.java
字号:
package com.lideedu.yame.utils;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import com.lideedu.yame.tree.dao.ProductDAO;
import com.lideedu.yame.tree.pojos.Product;
import com.lideedu.yame.tree.pojos.ProductCategory;
@SuppressWarnings("serial")
public class RecursionTag extends TagSupport {
private List list = null;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
for (Iterator iter = list.iterator(); iter.hasNext();) {
ProductCategory element = (ProductCategory) iter.next();
printTreeNode(out, element);
}
return TagSupport.SKIP_BODY;
}
/**
* 由MyEclipse重构的方法
* @param out
* @param element
*/
private void printTreeNode(JspWriter out, ProductCategory element) {
try {
out.println("<li>"+element.getCategoryName()+"<ul>");
} catch (IOException e) {
e.printStackTrace();
}
printAllCategory(element);
try {
out.println("</ul></li>");
} catch (IOException e) {
e.printStackTrace();
}
}
private void printAllCategory(ProductCategory pc){
JspWriter out = pageContext.getOut();
ProductDAO productDAO = new ProductDAO();
// 如果该类还拥有子类,则进行递归
if(pc.getSubCategories().size()>0){
Set set = pc.getSubCategories();
for (Iterator iter = set.iterator(); iter.hasNext();) {
ProductCategory element = (ProductCategory) iter.next();
printTreeNode(out, element);
}
// 打印出无子类的商品类别下的所有商品信息
}else if(productDAO.queryByCategoryId(pc.getCategoryId()) != null){
List list = productDAO.queryByCategoryId(pc.getCategoryId());
for (Iterator iter = list.iterator(); iter.hasNext();) {
Product element = (Product) iter.next();
try {
// 为具体商品添加超链接
out.println("<li><a href=queryProduct.do?productId=" + element.getProductId()+
" title=单击查看商品详细信息 target=mainFrame>"+element.getProductName()+"</a></li>");
} catch (IOException e) {
e.printStackTrace();
}
}
}
else
try {
out.println("<li><ul>"+pc.getCategoryName()+"</ul></li>");
} catch (IOException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -