package com.wnlc.mytag;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;
import com.opensymphony.xwork2.util.ValueStack;
import com.sun.org.apache.commons.beanutils.BeanUtils;
import com.wnlc.exception.MyException;
@SuppressWarnings( { "unchecked", "serial" })
public class Table extends ComponentTagSupport {
private String id;
private String name;
private String property;
private String classname;
private String onclick;
private String style;
private String border;
private String colspan;
private String colspancount;
private String indexList;
private String index;
private String count;
private Iterator iterator;
@Override
public Component getBean(ValueStack arg0, HttpServletRequest arg1, HttpServletResponse arg2) {
return new TableTagBean(arg0);
}
@Override
protected void populateParams() {
TableTagBean t = (TableTagBean) component;
try {
BeanUtils.copyProperties(t, this);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
@Override
public int doStartTag() throws JspException {
super.doStartTag();
try {
Object lst = this.getStack().findValue(name);
if (lst instanceof List) {
iterator = ((List) lst).iterator();
} else if (lst instanceof Object) {
List list = new ArrayList();
list.add(lst);
iterator = list.iterator();
} else {
List list = new ArrayList();
iterator = list.iterator();
}
pageContext.getOut().write("<table ");
if (style != null && !"".equals(style)) {
pageContext.getOut().write(" style=\"" + style + "\"");
}
if (onclick != null && !"".equals(onclick)) {
pageContext.getOut().write(" onclick=\"" + onclick + "\"");
}
if (classname != null && !"".equals(classname)) {
pageContext.getOut().write(" class=\"" + classname + "\"");
}
if (border != null && !"".equals(border)) {
pageContext.getOut().write(" border=\"" + border + "\"");
}
if (id != null && !"".equals(id)) {
pageContext.getOut().write(" id=\"" + id + "\"");
}
pageContext.getOut().write("><tr>");
pageContext.setAttribute("head", "1");
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
try {
if (iterator.hasNext()) {
Object o = iterator.next();
this.getStack().set(indexList, o);
if (index != null) {
Object countNumber = this.getStack().findValue(index);
countNumber = countNumber == null ? "0" : countNumber;
this.getStack().set(index, countNumber);
}
pageContext.setAttribute("head", "0");
this.getStack().set(indexList, o);
if (index != null) {
Object countNumber = this.getStack().findValue(index);
countNumber = (Integer.parseInt((String) countNumber) + 1) + "";
this.getStack().set(index, countNumber);
}
pageContext.getOut().write("</tr><tr>");
return EVAL_BODY_AGAIN;
} else {
pageContext.getOut().write("</tr>");
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
@Override
public int doEndTag() throws JspException {
// super.doEndTag();
try {
pageContext.getOut().write("</table>");
if (colspan != null) {
if (id == null) {
throw new MyException("如果要用colspan属性需要对表格指定id属性!");
}
pageContext.getOut().write("<script type='text/javascript' defer='defer'>");
colspancount = colspancount == null ? "-1" : colspancount;
pageContext.getOut().write("colspan('" + id + "','" + colspan + "','" + colspancount + "');");
pageContext.getOut().write("</script>");
}
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public String getOnclick() {
return onclick;
}
public void setOnclick(String onclick) {
this.onclick = onclick;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getBorder() {
return border;
}
public void setBorder(String border) {
this.border = border;
}
public String getColspan() {
return colspan;
}
public void setColspan(String colspan) {
this.colspan = colspan;
}
public String getColspancount() {
return colspancount;
}
public void setColspancount(String colspancount) {
this.colspancount = colspancount;
}
public String getIndexList() {
return indexList;
}
public void setIndexList(String indexList) {
this.indexList = indexList;
}
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public Iterator getIterator() {
return iterator;
}
public void setIterator(Iterator iterator) {
this.iterator = iterator;
}
}
- 1
- 2
前往页