How to access body of Custom tags in JSP tutorial
2015-06-27 01:56
555 查看
In the last tutorial we learnt how to create and use custom tags in JSP. In this tutorial we will see how to access the body of custom tag. For e.g. If our custom tag is
xyzthen we would learn to access the content between
<prefix: xyz>and
</prefix:xyz>
<prefix: xyz> Body of custom tag: This is what we will access in the below example </prefix:xyz>
Example:
In this example or custom tag will append a String to its own body and will display the result.
Tag handler class:
Details.java
package beginnersbook.com; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class Details extends SimpleTagSupport { //StringWriter object StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException { getJspBody().invoke(sw); JspWriter out = getJspContext().getOut(); out.println(sw.toString()+"Appended Custom Tag Message"); } }
TLD file:
message.tld
Remember to have this file in WEB-INF folder.
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>My Custom Tag: MyMsg</short-name> <tag> <name>MyMsg</name> <tag-class>beginnersbook.com.Details</tag-class> <body-content>scriptless</body-content> </tag> </taglib>
JSP Page:
index.jsp
<%@ taglib prefix="myprefix" uri="WEB-INF/message.tld"%> <html> <head> <title>Accessing Custom Tag Body Example</title> </head> <body> <myprefix:MyMsg> Test String </myprefix:MyMsg> </body> </html>
Output:
Test String Appended Custom Tag Message
相关文章推荐
- JSP Custom tags with example – JSP Tutorial
- 使用Volley解析json
- Json的序列化与反序列化以及乱入的k_BackingField
- 千位分隔符(js 实现)
- QA 基于开发的 JavaScript 修改测试数据和代码查看返回值的小例子
- JavaScript基础知识整理(1)数组
- 《JavaScript知识深入探秘》系列技术文章整理收藏
- [LeetCode][JavaScript]Maximum Gap
- Javascript进度条
- js的类型转换
- js跨越小结
- 那些逐步掌握的JavaScript('use strict')
- setInterval 和 setTimeout 的区别 JS学习笔记2015-6-26(第67天)
- 20150623 javascript实现的简单刷贴
- 《webgl入门指南》学习笔记二之three.js创建mesh
- JS ajax 后台交互
- JS Library Set
- HTML5 Localstorage JS
- js,jq获取浏览器宽度,高度等值(多浏览器)
- echop JS Library