package component;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;import readfromxml.ReadXML;public class CustomList extends TagSupport{
private List<List> toplist = new ArrayList<List>(); // 自定义列表标题
private String path="";
private String tabClass="";
private String titletrClass="";
private String thClass="";
private String tdimgClass="";
private String tdClass="";
private String width="";
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}


public void setToplist(List<List> toplist) {
this.toplist = toplist;
}
public List getToplist() {
return this.toplist;
}

@SuppressWarnings("unchecked")
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
ForEach parent=(ForEach)findAncestorWithClass(this,ForEach.class);
StringBuffer buf = new StringBuffer();
String configpath=path+"config\\Basic.xml";
buf.append("<table border='0' cellpadding='0' cellspacing='0' class='"+tabClass+"' width='"+width+"'>\n");

//内容title
buf.append("<tr class='"+titletrClass+"'>");
buf.append("<td width='20'> </td><td class='"+thClass+"'>&nbsp;</td>");
//buf.append("<td bgcolor='#EFFFD9' align='center' width='30' class='fontstyle'>序号</td>");

for(int i=0; i<toplist.size(); i++){
buf.append("<th class='"+thClass+"' width='");
buf.append(toplist.get(i).get(2));
buf.append("'>");
buf.append(toplist.get(i).get(0));
buf.append("</th>");
}

buf.append("<td class='"+tdClass+"' width='30'> </td></tr>");

/////内容列表
@SuppressWarnings("unused") 
List<List> replpics=ReadXML.getNodesAttrValues(configpath,"//root/replace","text");

List<List> currentPrint=(List)parent.getCurrentColl();
/* for(int i=0; i<currentPrint.size(); i++){    
for(int j=0; j<((List)currentPrint.get(i)).size(); j++){
if(!((List)toplist.get(j)).get(1).equals("0")){
buf.append("<a class='lei' href='");
        buf.append(((List)((List)currentPrint.get(i)).get(j)).get(1)); //例如:歌曲名的链接
        buf.append("' target='_blank'>");
        if(((List)((List)currentPrint.get(i)).get(j)).get(0)==null)
         buf.append("无");
        else if(((List)((List)currentPrint.get(i)).get(j)).get(0).equals("下载")&&downpic!=null)
         buf.append("<img src='"+downpic+"' alt='"+((List)((List)currentPrint.get(i)).get(j)).get(0)+"' border='0'>");
        else
         buf.append(((List)((List)currentPrint.get(i)).get(j)).get(0)); //例如:歌曲名       
        
        buf.append("</a>");
}

else
if(((List)((List)currentPrint.get(i)).get(j)).get(0)==null)
         buf.append("无");
        else if(((List)((List)currentPrint.get(i)).get(j)).get(0).equals("下载")&&downpic!=null)
buf.append("<img src='"+downpic+"' alt='"+((List)((List)currentPrint.get(i)).get(j)).get(0)+"' border='0'>");
else
buf.append(((List)((List)currentPrint.get(i)).get(j)).get(0)); //例如:歌曲名



buf.append("</td>");
}         buf.append("</tr>");          
}*/
for(int i=0; i<currentPrint.size(); i++){
//输出序号
buf.append("<tr><td> </td>");    
buf.append("<td width='24' class='"+tdClass+"'><div align='center'>"+(i+1)+".</div></td>");
for(int j=0; j<currentPrint.get(i).size(); j++){
//第i条记录的第j个属性
List<String> recordele=(List)currentPrint.get(i).get(j);
buf.append("<td class='");
int replace=-1;
for(int t=0;t<replpics.size();t++)
if(replpics.get(t).get(0).equals(recordele.get(0)))
replace=t;
//插入置换图片,其中图片src=replpics.get(replace).get(1)
if(replace!=-1){
buf.append(tdimgClass+"'>");
//无链接
if(toplist.get(j).get(1).equals("0"))
buf.append("<img src='"+replpics.get(replace).get(1)+"' alt='"+toplist.get(j).get(0)+"' border='0'>");
else
buf.append("<a href='"+recordele.get(1)+"'><img src='"+replpics.get(replace).get(1)+"' alt='"+toplist.get(j).get(0)+"' border='0'></a>");
}


//输出文字
else{
buf.append(tdClass+"'>");
//没有链接
if(toplist.get(j).get(1).equals("0"))
buf.append(recordele.get(0));
else
buf.append("<a href='"+recordele.get(1)+"'>"+recordele.get(0)+"</a>");
}
buf.append("</td>");
}
buf.append("<td></td></tr>");
}
buf.append("</table>");

try {
pageContext.getOut().print(buf);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return SKIP_BODY;
} public int doEndTag() throws JspException {
// TODO Auto-generated method stub
return EVAL_PAGE;
} public void release() {
// TODO Auto-generated method stub

}
public String getTabClass() {
return tabClass;
}
public void setTabClass(String tabClass) {
this.tabClass = tabClass;
}
public String getTdClass() {
return tdClass;
}
public void setTdClass(String tdClass) {
this.tdClass = tdClass;
}
public String getTdimgClass() {
return tdimgClass;
}
public void setTdimgClass(String tdimgClass) {
this.tdimgClass = tdimgClass;
}
public String getThClass() {
return thClass;
}
public void setThClass(String thClass) {
this.thClass = thClass;
}
public String getTitletrClass() {
return titletrClass;
}
public void setTitletrClass(String titletrClass) {
this.titletrClass = titletrClass;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}}提示
NameList是个tempList集合
tempLIst有三项:   第一项指定该列显示的名称:
                 第二项指定该列是不是有连接(1为有连接,0为无连接)
                 第三项指定该列的宽度ValueList也是tempList集合
templist有两项 
                第一项指定该行显示对应的字符串
                第二项指定该行对应的连接(如果name List对应的列是1)

解决方案 »

  1.   

    能看完是不简单的,我没本事看完。
    不过,我也说说:
    这里写的像是Servlet,但又不是继承HttpServlet,
    像JavaBean,但又有JSP页面代码,这是怎么搞的,一个字。“乱”PS:建议看一个struts精通MVC.
      

  2.   

    写的一个自定义标签.
    在jsp页面输出html代码.
    xml定义都没写出来,还有传进去的几个值没有加以说明.
      

  3.   

    看一下struts
    并多了解MVC.
    哈哈哈哈