JSP Custom tags with example – JSP Tutorial
2015-09-01 13:39
676 查看
User-defined tags are known as custom tags. In this tutorial we will see how to create a custom tag and use it in JSP.
To create a custom tag we need three things:
1) Tag handler class: In this class we specify what our custom tag will do when it is used in a JSP page.
2) TLD file: Tag descriptor file where we will specify our tag name, tag handler class and tag attributes.
3) JSP page: A JSP page where we will be using our custom tag.
Note:
message.tld
Note:
Choose any prefix and specify it in
Custom tag is called like this:
Output:
To create a custom tag we need three things:
1) Tag handler class: In this class we specify what our custom tag will do when it is used in a JSP page.
2) TLD file: Tag descriptor file where we will specify our tag name, tag handler class and tag attributes.
3) JSP page: A JSP page where we will be using our custom tag.
Example:
In the below example we are creating a custom tagMyMsgwhich will display the message “
This is my own custom tag” when used in a JSP page.
Tag handler class:
A tag handler class should implementTag/
IterationTag/
BodyTaginterface or it can also extend
TagSupport/
BodyTagSupport/
SimpleTagSupportclass. All the classes that support custom tags are present inside
javax.servlet.jsp.tagext. In the below we are extending the class
SimpleTagSupport.
Details.java
package beginnersbook.com; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class Details extends SimpleTagSupport { public void doTag() throws JspException, IOException { /*This is just to display a message, when * we will use our custom tag. This message * would be displayed */ JspWriter out = getJspContext().getOut(); out.println("This is my own custom tag"); } }
TLD File
This file should present at the location:Project Name/WebContent/WEB-INF/and it should have a
.tldextension.
Note:
<name> tag: custom tag name. In this example we have given it as
MyMsg
<tag-class>tag: Fully qualified class name. Our tag handler class
Details.javais in package
beginnersbook.comso we have given the value as
beginnersbook.com.Details.
message.tld
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>My Custom Tag</short-name> <tag> <name>MyMsg</name> <tag-class>beginnersbook.com.Details</tag-class> <body-content>empty</body-content> </tag> </taglib>
Using custom tag in JSP:
Above we have created a custom tag namedMyMsg. Here we will be using it.
Note:
taglibdirective should have the TLD file path in
urifield. Above we have created the
message.tldfile so we have given the path of that file.
Choose any prefix and specify it in
taglibdirective’s
prefixfield. Here we have specified it as
myprefix.
Custom tag is called like this:
<prefix:tagName/>. Our prefix is myprefix and tag name is
MyMsgso we have called it as
<myprefix:MyMsg/>in the below JSP page.
<%@ taglib prefix="myprefix" uri="WEB-INF/message.tld"%> <html> <head> <title>Custom Tags in JSP Example</title> </head> <body> <myprefix:MyMsg/> </body> </html>
Output:
This is my own custom tag
相关文章推荐
- 一些很经典的JavaScript的问题
- Jsp分页实例---假分页
- 通过JSON数据加载服务器端图片
- servlet 和JSP
- 关于JSP页面的分页技术简单实现和原理
- js javascript:void(0) 真正含义
- JavaScript DOM学习笔记之标签属性
- 关于JSP页面的验证码简单实现
- js实现具有高亮显示效果的多级菜单代码
- js日期时间函数
- JavaScript中日期各种问题处理,包括常用方法和转换方法
- rotate.js实现图片旋转 (chrome,IE,firefox都可以实现)
- 【.NET深呼吸】如何反序列化动态JSON
- js设置cookie
- Javascript操作Cookie
- JavaScript中this
- html js 校验文本框 设置span颜色字体
- JS 没有块级作用域
- C# - JSON详解
- RSA使用javascript加密JAVA解密