需求是有两个自定义的标签<table>和<column>
<table>有个items属性,负责导入数据
package hello; import java.util.HashMap; import java.util.Map; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; public class Column extends TagSupport { private String property; private String label; public int doStartTag() throws JspException { if (!(this.getParent() instanceof Table)) { throw new JspException("Column must be inside table."); } Map<String, String> column = new HashMap<String, String>(); column.put("label", label); column.put("property", property); Table table = (Table) this.getParent(); table.getColumns().add(column); return SKIP_BODY; } public int doEndTag() throws JspException { return EVAL_PAGE; } public void setLabel(String label) { this.label = label; } public void setProperty(String property) { this.property = property; } }
<column>有property属性负责数据的setter和getter,label负责字段
package hello; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; public class Table extends BodyTagSupport { private List<Map<String, String>> columns = new ArrayList<Map<String, String>>(); private List<Person> personList; private String url; @Override public int doStartTag() throws JspException { columns.clear(); return super.doStartTag(); } @Override @SuppressWarnings("unchecked") public int doAfterBody() throws JspException { try { BodyContent bc = this.getBodyContent(); JspWriter out = bc.getEnclosingWriter(); HttpServletRequest request = (HttpServletRequest) pageContext .getRequest(); String orderName = request.getParameter("orderName"); String orderType = request.getParameter("orderType"); orderType = "desc".equals(orderType) ? "desc" : "asc"; out.println("<table id=theObjTable "); out.println("class=list_table border="3";>"); out.println("<tr class=tr_title>"); for (int i = 0; i < columns.size(); i++) { Map<String, String> column = columns.get(i); String label = column.get("label"); String property = column.get("property"); label = label == null ? property : label;// 没有列名的用属性做列名 out.println("<td id='_id_td_" + property + "'>"); out.println(label); out.println("</td>"); } out.println("</tr>"); if (personList != null) { for (int i = 0; i < personList.size(); i++) { out.println("<tr class=tr_data>"); Person person = personList.get(i); for (int j = 0; j < columns.size(); j++) { Map<String, String> colmun = columns.get(j); String property = colmun.get("property"); String getterStyle = toGetterStyle(property); String method = "get" + getterStyle; try { Method m = person.getClass().getDeclaredMethod( method); String value = (String) m.invoke(person); out.println("<td>" + value + "</td>"); } catch (Exception e) { e.printStackTrace(); } } } } } catch (Exception e) { } return 0; } public String toGetterStyle(String column) { // TODO Auto-generated method stub if (column.length() == 1) return column.toUpperCase(); char ch = column.charAt(0); return Character.toUpperCase(ch) + column.substring(1); } public List<Map<String, String>> getColumns() { return this.columns; } public void setItems(List<Person> personList) { this.personList = personList; } }
思路:
在<table>设一个List<Map<String, String>> 来存放字段
每个<column>都有一个Map<String, String>存放一个字段,在doEndTag方法中
调用getParent获得<table>对象的getColmun方法获得上面的list,add这个Map
一个循环,把list的字段提出来
一个循环,把column的property加上“get“拼凑方法的字符串,
Method m = person.getClass().getDeclaredMethod( method);
String value = (String) m.invoke(person);
字符串不能作为方法只能反射调用方法