深入体验JavaWeb开发内幕——使用简单标签控制页面逻辑案例
来源:互联网 发布:英国留学哪种gpa算法 编辑:程序博客网 时间:2024/05/16 12:06
使用自定义简单标签我们可以将相应的控制页面逻辑的代码封装在标签中,在使用时只需引用标签,便可达到与使用java代码同样的效果。
来看几个具体的案例:
一个tld标签库描述文件:
<?xml version="1.0"encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>A taglibrary exercising SimpleTag handlers.</description> <tlib-version>1.0</tlib-version> <short-name>c</short-name> <uri>http://www.hbsi.examplec</uri> <tag> <description>OutputsHello, World</description> <name>if</name> <tag-class>www.hbsi.web.example.IFSimpleTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>flag</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <description>OutputsHello, World</description> <name>choice</name> <tag-class>www.hbsi.web.example.CElseSimpleTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <description>OutputsHello, World</description> <name>If</name> <tag-class>www.hbsi.web.example.CIFSimpleTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <description>OutputsHello, World</description> <name>Else</name> <tag-class>www.hbsi.web.example.CElseSimpleTag</tag-class> <body-content>scriptless</body-content> </tag> </taglib>
相关的自定义标签处理器类及相应的jsp引用文件:
1、 开发<c:if>标签
publicclass IFSimpleTag extends SimpleTagSupport{ privatebooleanflag ; publicvoid setFlag(boolean flag) { this.flag = flag;} @Overridepublicvoid doTag() throws JspException, IOException { if(flag){ JspFragment jf= this.getJspBody(); jf.invoke(null); }}}
Jsp引用文件:
<%@taglib uri="http://www.hbsi.examplec" prefix ="c" %><body> <c:if flag="true"> 当结果为真时我才会输出!<br/> </c:if> 不管结果是什么我都会输出!<br/></body>
效果:
2、 开发<c:if><c:else>标签
if子类
publicclass CIFSimpleTag1 extends SimpleTagSupport { privatebooleantest ;publicvoid setTest(boolean test) { this.test = test;}@Overridepublicvoid doTag() throws JspException, IOException { IFElseSimpleTag cie = (IFElseSimpleTag) this.getParent(); if(test && !cie.isFlag()){ this.getJspBody().invoke(null);; cie.setFlag(true); }}}
else子类
publicclass CElseSimpleTag1 extends SimpleTagSupport {@Overridepublicvoid doTag() throws JspException, IOException { IFElseSimpleTag cie = (IFElseSimpleTag) this.getParent();// if(!cie.isFlag()){ if(!cie.isFlag()){ this.getJspBody().invoke(null); cie.setFlag(true); }} }
父类:
publicclass IFElseSimpleTag extends SimpleTagSupport { privatebooleanflag; publicboolean isFlag() { returnflag;} publicvoid setFlag(boolean flag) { this.flag = flag;} @Overridepublicvoid doTag() throws JspException, IOException { this.getJspBody().invoke(null);}}
Jsp引用文件
<%@ taglib uri="http://www.hbsi.c" prefix="c"%><body> <c:choice> <c:if test ="<%=1==2 %>"> 当结果为真时我才会输出!<br/> </c:if> <c:else> 当结果为假时我才会输出!<br/> </c:else> </c:choice> </body>
效果:
3、开发迭代标签
publicclass ForEachSimpleTag2 extends SimpleTagSupport { private Collection collection; private String var ; private Object items; publicvoid setItems(Objectitems) { this.items = items; if(itemsinstanceof Collection){ collection = (Collection) items; } if(itemsinstanceof Map){ Map map = (Map) items; collection = map.entrySet(); } if(items.getClass().isArray()){ collection = new ArrayList(); for(int i=0;i<Array.getLength(items);i++){ collection.add(Array.get(items, i)) ; } }} publicvoid setVar(String var) { this.var = var;}@Overridepublicvoid doTag() throws JspException, IOException { Iterator it = collection.iterator(); while(it.hasNext()){ Object object = it.next(); this.getJspContext().setAttribute(var, object); JspFragment jf= this.getJspBody(); jf.invoke(null); } }}
Jsp引用文件
<%@ taglib uri="http://www.hbsi.c"prefix ="c" %> <body> <% List list = new ArrayList(); list.add("aa"); list.add("bb"); list.add("cc"); request.setAttribute("list",list); %> <c:ForEach items="${list}" var="l"> ${l} </c:ForEach> <hr/> <% Map map = new LinkedHashMap(); map.put("1","aaa"); map.put("2","aaa"); map.put("3","aaa"); request.setAttribute("map",map); %> <c:ForEach items="${map}" var="me"> ${me.key}----------${me.value} </c:ForEach> <hr/> <% String str [] = {"aaaaa","bbbbbb","ccccccc"}; request.setAttribute("str",str); %> <c:ForEach items="${str}" var="str"> ${str} </c:ForEach> <hr/> <% int i [] = {1,3,4}; request.setAttribute("in",i); %> <c:ForEach items="${in}" var="in"> ${in} </c:ForEach> <hr/> </body>效果如图: 4、开发html转义标签publicclass HTMLFilter extends SimpleTagSupport { @Override publicvoid doTag() throws JspException,IOException { JspFragment jf = this.getJspBody(); StringWriter sw = newStringWriter(); jf.invoke(sw); String content = sw.toString(); String contents = filter(content); this.getJspContext().getOut().write(contents); } public Stringfilter(String message) { if (message == null) return (null); char content[] = newchar[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()); }}
例3的效果图:
Jsp引用文件
<%@ taglib uri="http://www.hbsi.c"prefix="c"%><body> <c:filter> 将文件中的HTML原作用除去! <a href = "#">Hello!</a> </c:filter></body>
效果如图:
好了赶快自己试试吧!
- 深入体验JavaWeb开发内幕——使用简单标签控制页面逻辑案例
- 深入体验JavaWeb开发内幕——使用简单标签实现传统标签开发实现的几个功能
- 深入体验JavaWeb开发内幕——自定义简单标签的开发
- 深入体验JavaWeb开发内幕——开发带属性的简单标签
- 深入体验JavaWeb开发内幕——打包自定义标签库及自定义分页标签
- 深入体验JavaWeb开发内幕——JSP技术相关案例应用
- 深入体验javaWeb开发内幕——问件的上传与下载案例实现
- 深入体验JavaWeb开发内幕——简述JSP中的自定义标签叫你快速学会
- 深入体验JavaWeb开发内幕——Servlet入门
- 深入体验JavaWeb开发内幕——Cookie会话
- 深入体验JavaWeb开发内幕——关于Request对象
- 深入体验JavaWeb开发内幕——EL表达式之一
- 深入体验JavaWeb开发内幕——配置TomCat数据源
- 深入体验JavaWeb开发内幕——EL表达式之一
- 深入体验JavaWeb开发内幕——EL表达式之一
- 使用标签控制页面逻辑案例
- 深入体验JavaWeb开发内幕——使用Request对象设置防盗链
- 深入体验JavaWeb开发内幕——使用元数据简化JDBC代码
- dedecms相关知识
- The Function Invocation Pattern
- excel 去掉公式保留数值的方法
- 正则表达式匹配HTML内容
- c#面向对象基础
- 深入体验JavaWeb开发内幕——使用简单标签控制页面逻辑案例
- AWK-端对端时延-程序解读
- oracle 数据格式化
- [转]imagick CMKY转RGB CMKY too RGB
- ajax中的application/x-www-form-urlencoded中的使用
- 安卓自定义dialog文章
- ios6 不能激活,想刷回ios5 ,但是itunes无法显示iphone信息,怎么办
- Python基础05 缩进和选择
- android 获取View的高度