为JSP编写自定义标签(2)
2014-07-30 11:25
447 查看
嵌套的自定义标签
看完了源码,也修改了下并运行通过,理解有限。
taglib.tld配置
table标签的具体实现类,包括获取column的数据,显示内容等都在此实现类中。
column标签的实现类,相对简单的多。
还有Person.java,对应的style.css以及其他资源就不说了,不是学习点。
看完了源码,也修改了下并运行通过,理解有限。
taglib.tld配置
table标签的具体实现类,包括获取column的数据,显示内容等都在此实现类中。
package my; import java.io.IOException; 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 static final long serialVersionUID = 3358444196409845360L; // 存储列信息 private List<Map<String, String>> columns = new ArrayList<Map<String, String>>(); // 存储数据,可能为 集合类型的或者数组类型的 private Object items; // 取排序数据的 URL private String url; @Override public int doStartTag() throws JspException { columns.clear(); // 默认返回EVAL_BODY_BUFFERED return super.doStartTag(); } @Override @SuppressWarnings("unchecked") public int doAfterBody() throws JspException { try { // 获取标签体 BodyContent bc = 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 STYLE='table-layout:fixed;' >"); out.println(" <tr class=tr_title>"); out.println(" <script>var columns = []; </script>"); for(int i = 0; i < columns.size(); i++) { // 获取列信息 Map<String, String> column = columns.get(i); // 列头 String label = column.get("label"); // 该列对应的 Java Bean 的属性 String property = column.get("property"); label = label == null ? property : label; out.println("<td id='__id_td_" + property + "'>"); out.println("<font class='resizeDivClass'"); out.println(" onmousedown='MouseDownToResize(this);');"); out.println(" onmousemove='MouseMoveToResize(this);'"); out.println(" onmouseup='MouseUpToResize(this);'></font>"); out.println("<span onclick=\"sort('" + property + "'); \""); out.println(" style=\"cursor: pointer; \">"); out.println(label); if(property.equals(orderName)) { out.println("<img src='images/" + orderType + ".gif' border=0/>"); } out.println("</span>"); out.println("</td>"); out.println("<script>columns[columns.length] = '__id_td_" + property + "'; </script>"); } out.println(" </tr>"); if(items != null) { // 遍历所有的数据 for(Object obj : (Iterable) items) { out.println(" <tr class=tr_data>"); for(int i = 0; i < columns.size(); i++) { Map<String, String> column = columns.get(i); String property = column.get("property"); String getterStyle = toGetterStyle(property); try { String getter = "get" + getterStyle; String is = "is" + getterStyle; Method method = null; try { // 获取 getXxx() 形式的方法 method = obj.getClass().getMethod(getter); } catch (Exception e) { } if(method == null) { // 如果没有,获取 isXxx() 形式的方法 method = obj.getClass().getMethod(is); } method.setAccessible(true); // 获取属性值 Object value = method.invoke(obj); out.println("<td><span title='" + value + "'>" + value + "</span></td>"); } catch (Exception e) { throw new JspException(e); } } out.println(" </tr>"); } } out.println("</table>"); out.println("<script>"); out.println(" var orderName = '" + orderName + "'; "); out.println(" var orderType = '" + orderType + "'; "); out.println(" function sort(column){"); out.println(" if(orderName == column){"); out.println(" location='" + url + "?orderName=' + column + '&orderType=' + (orderType=='asc' ? 'desc' : 'asc'); "); out.println(" }"); out.println(" else{"); out.println(" location='" + url + "?orderName=' + column + '&orderType=' + orderType; "); out.println(" }"); out.println(" }"); out.println("</script>"); } catch (IOException ioe) { throw new JspException("Error: " + ioe.getMessage()); } return SKIP_BODY; } public Object getItems() { return items; } public void setItems(Object items) { this.items = items; } public String toGetterStyle(String column) { if(column.length() == 1) { return column.toUpperCase(); } char ch = column.charAt(0); // substring(int i) 返回i到末尾的这段字符串 return Character.toUpperCase(ch) + column.substring(1); } public List<Map<String, String>> getColumns() { return columns; } public void setColumns(List<Map<String, String>> columns) { this.columns = columns; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }
column标签的实现类,相对简单的多。
package my; 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 static final long serialVersionUID = 5119493903438602864L; private String property; private String label; private String type; 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); column.put("type", type); Table table = (Table) this.getParent(); table.getColumns().add(column); return SKIP_BODY; } public int doEndTag() throws JspException { return EVAL_PAGE; } public String getProperty() { return property; } public void setProperty(String property) { this.property = property; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
还有Person.java,对应的style.css以及其他资源就不说了,不是学习点。
相关文章推荐
- jsp自定义标签(taglib)编写的原理和实现【转载】
- jsp 自定义标签的编写
- jsp自定义标签(taglib)编写的原理和实现
- 编写JSP自定义标签
- JSP自定义标签编写
- 为JSP编写自定义标签(1)
- JSP自定义标签编写
- jsp自定义标签(taglib)编写的原理和实现
- JSP自定义标签的编写
- jsp自定义标签(taglib)编写的原理和实现
- jsp自定义标签的简单编写
- jsp自定义标签的编写
- (转)JSP自定义标签
- JSP自定义标签
- jsp自定义标签、EL自定义方法
- Java_JSP自定义标签的开发与应用
- JSP 自定义标签
- jsp自定义标签
- MyEclipse下编写JSP "Hello World"
- MyEclipse 编写 JSP 代码时很卡的解决办法