第9章 简单标签 9.1 简单标签知识 在Jsp2.0中定义SimpleTag 接口用于替换传统的标签开发的接口,它只有一个方法doTag() 用于取代传统的doStartTag()、doEndTag()、doAfterBody() 等方法。 1、 JSP引擎调用标签处理器对象的setJspContext(),将JSP页面的pageContext()对象传递给标签处理器对象。 2、 JSP引擎调用setParent(),将父标签处理器对象传递给标签处理器对象。 3、 如果调用标签设置属性,容器将每个属性对应的setter()方法把属性值传递给标签处理器对象。 4、 如果有标签体,容器将标签体封装为JspFragment对象,然后调用setJspBody()将JspFragment对象传递给标签处理器对象。 5、 调用doTag()方法执行标签逻辑。 9.1.1 SimpleTag接口 SimpleTag接口定义方法: 1、 setJspContext() Java代码 1. public void setJspContext(JspContext context); public void setJspContext(JspContext context); JSP页面的pageContext对象传递给标签处理器对象。 2、 setParent() Java代码 1. public void setParent(JspTag parent); public void setParent(JspTag parent); // 把父标签处理器对象传递给当前标签处理器对象。 3、 getParent() Java代码 1. public JspTag getParent(); public JspTag getParent(); // 获取当前父标签处理器对象。 4、 setJspBody() Java代码 1. public void setJspBody(JspFragment jspBody); public void setJspBody(JspFragment jspBody); // 把代表标签体的JspFragment对象传递给标签处理器对象。 5、 doTag() Java代码 1. public void doTag(); public void doTag(); // 用于完成标签处理逻辑。 /*9.1.2 JspFragment类 JSP引擎处理标签体内容时会把标签体内容用一个JspFragement对象表示,并调用标签体处理器对象的setJspBody()把 JspFragement对象传递给标签处理器对象,标签开发者根据需要调用JspFragement对象的方法,决定是否执行和输出标签体。*/ 1、 getJspContext() Java代码 1. public abstract JspContext getJspContext(); public abstract JspContext getJspContext(); //用于返回调用页面的JspContext对象。 2、 invoke() Java代码 1. public abstract void invoke(Writer out); public abstract void invoke(Writer out); 用于执行JspFragment对象代表的JSP代码片段。 9.1.3 SimpleTagSupport类 SimpleTagSupport类是SimpleTag接口的实现。 //9.2 简单标签实例 Java代码 1. public class SimpleTagDemo extends SimpleTagSupport { 2. private String username; 3. 4. public void setUsername(String username) { 5. this.username = username; 6. } 7. @Override 8. public void doTag() throws JspException, IOException { 9. if ("wangtong".equals(username)) { 10. JspFragment fragment = this.getJspBody(); 11. fragment.invoke(null); 12. } else { 13. getJspContext().getOut().write("用户名不正确"); 14. throw new SkipPageException(); 15. } 16. }} public class SimpleTagDemo extends SimpleTagSupport { private String username; public void setUsername(String username) { this.username = username; } @Override public void doTag() throws JspException, IOException { if ("wangtong".equals(username)) { JspFragment fragment = this.getJspBody(); fragment.invoke(null); } else { getJspContext().getOut().write("用户名不正确"); throw new SkipPageException(); } }} //Xml代码 1. <tag> 2. <name>simpleTag</name> 3. <tag-class>tag.simpletag.SimpleTagDemo</tag-class> 4. <body-content>scriptless</body-content> 5. <attribute> 6. <name>username</name> 7. <required>true</required> 8. <rtexprvalue>true</rtexprvalue> 9. </attribute> 10. </tag> <tag> <name>simpleTag</name> <tag-class>tag.simpletag.SimpleTagDemo</tag-class> <body-content>scriptless</body-content> <attribute> <name>username</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> //Html代码 1. <wangtong:simpleTag username="wangtong"> 2. username 3. </wangtong:simpleTag> <wangtong:simpleTag username="wangtong"> username </wangtong:simpleTag> //9.3 迭代数组 Java代码 1. public class SimpleIterateTag extends SimpleTagSupport { 2. private String name; 3. private String items[]; 4. 5. public void setName(String name) { 6. this.name = name; 7. } 8. 9. public void setItems(String[] items) { 10. this.items = items; 11. } 12. 13. @Override 14. public void doTag() throws JspException, IOException { 15. for (String i : items) { 16. getJspContext().setAttribute(name, i); 17. getJspBody().invoke(null); 18. }}} public class SimpleIterateTag extends SimpleTagSupport { private String name; private String items[]; public void setName(String name) { this.name = name; } public void setItems(String[] items) { this.items = items; } @Override public void doTag() throws JspException, IOException { for (String i : items) { getJspContext().setAttribute(name, i); getJspBody().invoke(null); }}} //Xml代码 1. <tag> 2. <name>simpleIterateTag</name> 3. <tag-class>tag.simpletag.SimpleIterateTag</tag-class> 4. <body-content>scriptless</body-content> 5. <attribute> 6. <name>name</name> 7. <required>true</required> 8. </attribute> 9. <attribute> 10. <name>items</name> 11. <required>true</required> 12. <rtexprvalue>true</rtexprvalue> 13. </attribute> 14. </tag> <tag> <name>simpleIterateTag</name> <tag-class>tag.simpletag.SimpleIterateTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>name</name> <required>true</required> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> //Html代码 1. <% 2. String[] books = { "book1", "book2", "book3" }; 3. %> 4. <wangtong:simpleIterateTag items="<%=books%>" name="bookname"> 5. ${bookname} 6. </wangtong:simpleIterateTag> <% String[] books = { "book1", "book2", "book3" }; %> <wangtong:simpleIterateTag items="<%=books%>" name="bookname"> ${bookname} </wangtong:simpleIterateTag> //9.4 修改标签体内容 //Java代码 1. public class ReadFileTag extends SimpleTagSupport { 2. private String src; 3. 4. public void setSrc(String src) { 5. this.src = src; 6. } 7. 8. @Override 9. public void doTag() throws JspException, IOException { 10. InputStream in; 11. BufferedReader br; 12. try { 13. in = ((PageContext) getJspContext()).getServletContext() 14. .getResourceAsStream(src); 15. br = new BufferedReader(new InputStreamReader(in)); 16. String line = br.readLine(); 17. while (line != null) { 18. this.getJspContext().getOut().write("line" + "/r/n"); 19. line = br.readLine(); 20. } 21. br.close(); 22. } catch (Exception e) { 23. e.getMessage(); 24. }}} 25. 26. public class HtmlFilterTag extends SimpleTagSupport { 27. 28. @Override 29. public void doTag() throws JspException, IOException { 30. JspFragment fragment = this.getJspBody(); 31. StringWriter writer = new StringWriter(); 32. fragment.invoke(writer); 33. String newBody = filter(writer.toString()); 34. this.getJspContext().getOut().write(newBody); 35. } 36. 37. private String filter(String message) { 38. if (message == null) { 39. return null; 40. } 41. char content[] = new char[message.length()]; 42. message.getChars(0, message.length(), content, 0); 43. StringBuffer result = new StringBuffer(content.length + 50); 44. for (int i = 0; i < content.length; i++) { 45. switch (content[i]) { 46. case '<': 47. result.append("<"); 48. break; 49. case '>': 50. result.append(">"); 51. break; 52. case '&': 53. result.append("&"); 54. break; 55. case '"': 56. result.append("""); 57. break; 58. default: 59. result.append(content[i]); 60. } 61. } 62. return result.toString(); 63. }} public class ReadFileTag extends SimpleTagSupport { private String src; public void setSrc(String src) { this.src = src; } @Override public void doTag() throws JspException, IOException { InputStream in; BufferedReader br; try { in = ((PageContext) getJspContext()).getServletContext() .getResourceAsStream(src); br = new BufferedReader(new InputStreamReader(in)); String line = br.readLine(); while (line != null) { this.getJspContext().getOut().write("line" + "/r/n"); line = br.readLine(); } br.close(); } catch (Exception e) { e.getMessage(); }}} public class HtmlFilterTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { JspFragment fragment = this.getJspBody(); StringWriter writer = new StringWriter(); fragment.invoke(writer); String newBody = filter(writer.toString()); this.getJspContext().getOut().write(newBody); } private String filter(String message) { if (message == null) { return null; } char content[] = new char[message.length()]; message.getChars(0, message.length(), content, 0); StringBuffer result = new StringBuffer(content.length + 50); for (int i = 0; i < content.length; i++) { switch (content[i]) { case '<': result.append("<"); break; case '>': result.append(">"); break; case '&': result.append("&"); break; case '"': result.append("""); break; default: result.append(content[i]); } } return result.toString(); }} //Xml代码 1. <tag> 2. <name>simpleHtmlFilter</name> 3. <tag-class>tag.simpletag.ReadFileTag</tag-class> 4. <body-content>scriptless</body-content> 5. </tag> 6. <tag> 7. <name>simpleReadFile</name> 8. <tag-class>tag.simpletag.HtmlFilterTag</tag-class> 9. <body-content>empty</body-content> 10. <attribute> 11. <name>src</name> 12. <required>true</required> 13. </attribute> 14. </tag> <tag> <name>simpleHtmlFilter</name> <tag-class>tag.simpletag.ReadFileTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>simpleReadFile</name> <tag-class>tag.simpletag.HtmlFilterTag</tag-class> <body-content>empty</body-content> <attribute> <name>src</name> <required>true</required> </attribute> </tag> //Html代码 1. <wangtong:simpleHtmlFilter> 2. <wangtong:readfile src="/tag.jsp" mce_src="tag.jsp" /> 3. </wangtong:simpleHtmlFilter>