JSP简单标签的总结及案例
2015-07-04 23:04
537 查看
一、控制页面部分内容是否输出
1.控制页面部分内容输出
2.控制页面部分内容不输出
二、控制标签内容重复输出
三、修改标签体内容
四、控制整个页面是否输出
【开发带属性的标签】
1.在标签处理器编写每个属性对应的setter方法
2.在TLD文件中描述标签的属性
注意:属性的值只支持八种基本类型的转换,若是其他的类型,需要用到表达式
1、开发反盗链标签
2、开发for标签
3、开发if else标签
4、开发foreach标签
5、开发html转义标签
1.控制页面部分内容输出
public void doTag() throws JspException,IOException{ JspFragment jf=this.getJspBody(); jf.invokee(null);//或jf.invoke(this.getJspContext().getOut()); }
2.控制页面部分内容不输出
public void doTag() throws JspException,IOException{ }
二、控制标签内容重复输出
public void doTag() throws JspException,IOException{ JspFragment jf=this.getJspBody(); for(int i=0;i<5;i++){ jf.invoke(null); } }
三、修改标签体内容
pubilc void doTag() throws JspException,IOException{ JspFragment jf=this.getJspBody(); StringWriter sw=new StringWriter(); jf.invoke(sw); String content=sw.toString(); content=content.toUpperCase(); this.getJspContext().getOut().write(content); }
四、控制整个页面是否输出
public void doTag() throws JspException,IOException{ throw new SkipPageException(); }
【开发带属性的标签】
<sitcast:demo count="5"> </sitcast:demo>
1.在标签处理器编写每个属性对应的setter方法
public class SimpleTagDemo extends SimpleTagSupport{ private int count; public void setCount(int count){ this.count=count; } public void doTag() throws JspException,IOException{ JspFragment jf=this.getJspBody(); for(int i=0;i<count;i++){ jf.invoke(null); } } }
2.在TLD文件中描述标签的属性
<attribute> <name>count</name> <required>true</required> <rtexprvalue>true</rtexprvalue>//是否支持El和脚本表达式 </attribute>
注意:属性的值只支持八种基本类型的转换,若是其他的类型,需要用到表达式
1、开发反盗链标签
使用标签: <fix:referer site="http://localhost" page="/index.jsp"></fix:referer> 代码: package cn.itcast.web.tag.eaxmple; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.SkipPageException; import javax.servlet.jsp.tagext.SimpleTagSupport; public class RefererTag extends SimpleTagSupport { private String page;//要跳转的页面 private String site;//针对自家的哪个网站防盗链 public void setPage(String page) { this.page = page; } public void setSite(String site) { this.site = site; } @Override public void doTag() throws JspException, IOException { PageContext pageContext=(PageContext) this.getJspContext(); HttpServletRequest request=(HttpServletRequest) pageContext.getRequest(); HttpServletResponse response= (HttpServletResponse) pageContext.getResponse(); String referer=request.getHeader("referer"); if(referer==null || !referer.startsWith(site)){ String cp=request.getContextPath(); if(page.startsWith(cp)) { response.sendRedirect(page); }else if (page.startsWith("/")) { response.sendRedirect(cp+page); }else { response.sendRedirect(cp+"/"+page); } throw new SkipPageException(); } else super.doTag(); } }
2、开发for标签
使用标签: <fix:if test="${user==null }"> 未登陆. <br> </fix:if> 代码: public class IfTag extends SimpleTagSupport{ private boolean test; public void setTest(boolean test){ this.test=test; } public void doTag() throws JspException,IOException{ if(test){ this.getJspBody().invoke(null); } } }
3、开发if else标签
<fix:choose> <fix:when test="${user==null }"> 未登陆. <br> </fix:when> <fix:otherwith> welcome用户已经登录. <br> </fix:otherwith> </fix:choose> public class Choose extends SimpleTagSupport { private boolean isDo; public boolean isDo() { return isDo; } public void setDo(boolean isDo) { this.isDo = isDo; } @Override public void doTag() throws JspException, IOException { this.getJspBody().invoke(null); } } public class WhenTag extends SimpleTagSupport { private boolean test; public void setTest(boolean test) { this.test = test; } @Override public void doTag() throws JspException, IOException { Choose parent=(Choose) this.getParent(); if(test && !parent.isDo()) { this.getJspBody().invoke(null); parent.setDo(true); } } } public class OtherwithTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { Choose parent=(Choose) this.getParent(); if(!parent.isDo()) { this.getJspBody().invoke(null); parent.setDo(true); } } }
4、开发foreach标签
使用标签: <fix:choose> <fix:when test="${user==null }"> 未登陆. <br> </fix:when> <fix:otherwith> welcome用户已经登录. <br> </fix:otherwith> </fix:choose> 代码: public class Choose extends SimpleTagSupport { private boolean isDo; public boolean isDo() { return isDo; } public void setDo(boolean isDo) { this.isDo = isDo; } @Override public void doTag() throws JspException, IOException { this.getJspBody().invoke(null); } } public class WhenTag extends SimpleTagSupport { private boolean test; public void setTest(boolean test) { this.test = test; } @Override public void doTag() throws JspException, IOException { Choose parent=(Choose) this.getParent(); if(test && !parent.isDo()) { this.getJspBody().invoke(null); parent.setDo(true); } } } public class OtherwithTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { Choose parent=(Choose) this.getParent(); if(!parent.isDo()) { this.getJspBody().invoke(null); parent.setDo(true); } } }
5、开发html转义标签
使用标签: <fix:htmlFilter> <a href="a 发 a a">超链接的写法</a> </fix:htmlFilter> 代码: package cn.itcast.web.tag.example; import java.io.IOException; import java.io.StringWriter; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; public class HtmlFilter extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { JspFragment jf=this.getJspBody(); StringWriter sw=new StringWriter(); jf.invoke(sw); String content=sw.getBuffer().toString(); content=filter(content); this.getJspContext().getOut().write(content); } /*C:\\Tomcat 7.0\\webapps\\examples\\WEB-INF\\classes\\util\\HTMLFilter.java*/ public static String filter(String message) { if (message == null) return (null); char content[] = new char[message.length()]; message.getChars(0, message.length(), content, 0); StringBuilder result = new StringBuilder(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()); } }
相关文章推荐
- JSP/PHP基于Ajax的分页功能实现
- 开发阶段Jetty运行Jsp报错且响应空白
- Apache Web让JSP“动”起来
- JSP 多条SQL语句同时执行的方法
- 在jsp页面中响应速度提高的7种方法分享
- (jsp/html)网页上嵌入播放器(常用播放器代码整理)
- JSP避免Form重复提交的三种方案
- jsp 标准标签库简析
- jsp 常用标签的使用
- JSP小知识简单介绍
- jsp页面中的代码执行加载顺序介绍
- jsp遍历文件夹下的文件的代码
- jsp fckeditor 上传中文图片乱码问题的解决方法
- JSP转发和重定向的区别分析
- jsp 连接sql server 2008 连接不上的解决方法
- jsp是什么文件 如何打开jsp格式的文件
- 五种 JSP页面跳转方法详解
- 分享JSP中文乱码解决方法