- 浏览: 416412 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (255)
- Android (53)
- java (57)
- javascript (7)
- linux (19)
- springside3 (6)
- spring (2)
- struts2 (11)
- hibernate (2)
- jsp&servlet (15)
- jquery (1)
- ExtJs (5)
- freemarker (1)
- apache (5)
- mysql (3)
- tomcat (3)
- eclipse&maven (23)
- 电脑小技巧 (1)
- 配置安装 (3)
- 开源框架 (2)
- 设计模式 (2)
- 架构 (2)
- ajax (1)
- 正则表达式 (7)
- 测试 (2)
- 装修 (1)
- 不错的软件 (4)
- http协议 (2)
- 网络 (2)
- windows (2)
- nodejs (1)
最新评论
-
yhyx:
好
JAVA URI URL区别 -
dingbuoyi:
我文章很早以前写的啊 估计软件版本早更新了 你要自己研究一下
windows下Sublime Text 2开发 Nodejs -
di1984HIT:
写的很好,学习了
【转帖】IP网段的计算和划分 -
农民柏柏:
感谢分享
【转】Android实现人人网点击“+”弹出效果 -
lianwanf:
大神,求源码,很想要那jar包.官方的不懂下载啊.谢谢啊. ...
开源框架ignition[二]
原帖:http://zetcode.com/tutorials/jeetutorials/customjsptags/
Custom JSP tags
In this part of the JEE tutorials we will talk about custom tags.
A custom tag is a user-defined JSP language element. It is an extension to the JSP language. Custom tags are reusable software components. Custom tags are used to handle common functionality. They also separate programming code from the content. They make the JSP pages look uniform. This way the JSP pages are more maintainable.
Custom tags can be created using:
Tag handlers
Tag files
Tag handlers are java classes, that implement the custom tag. A tag file is a source file containing JSP code that is translated into a simple tag handler by the web container. Same as with JSPs and serlvets.
Tag handlers can be made available to a web application in two basic ways. The classes implementing the tag handlers can be stored in an unpacked form in the WEB-INF/classes/ subdirectory of the web application. Alternatively, if the library is distributed as a JAR, it is stored in the WEB-INF/lib/ directory of the web application.
Empty custom tag
When we started with JavaServer pages, we introduced a simple example, that showed the current date. In the following example, we put the java code into the tag handler and thus, separate the code from the content.
Each custom tag implemented with a tag handler must be declared in a special xml file called tag library descriptor (TLD). The TLD file maps custom tags to their corresponding simple tag handler implementation classes.
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="d" uri="http://zetcode.com/tlds/date" %>
<html>
<head>
<title>Custom tags</title>
<style>
* { font-size: 12px; font-family: Verdana }
</style>
</head>
<body>
<h2>Date</h2>
<b>Today's Date: </b> <d:DateTag />
</body>
</html>
This is the jsp file, that will output the current date.
<%@taglib prefix="d" uri="http://zetcode.com/tlds/date" %>
The taglib directive will enable us to use the custom tag in this jsp page. The uri parameter is a unique identifier for the tag library. In the previous versions of the JSP technology, developers had to edit the web.xml file. Today this is not necessary. The container will automatically map the uri with the coresponding TLD. The uri must be unique within the application. The taglib directive also specifies the prefix, used in our custom tag.
<b>Today's Date: </b> <d:DateTag />
Here we use our custom tag. This tag displays current date and time. The DateTag is the name of the custom tag, specified in the date.tld file. We have a tag with empty body, so there is ending tag.
date.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>d</short-name>
<uri>http://zetcode.com/tlds/date</uri>
<tag>
<name>DateTag</name>
<tag-class>com.zetcode.DateTagHandler</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
This is the tag library descritor, for our example. In the tag element, we provide the name of the tag, the java class, that implements the tag. We also specify, that our tag has no body. We placed the date.tld file into the WEB-INF/tlds directory.
DateTagHandler.java
package com.zetcode;
import java.util.Date;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
public class DateTagHandler extends SimpleTagSupport {
public void doTag() throws JspException {
JspWriter out=getJspContext().getOut();
try {
out.println(new Date());
} catch (java.io.IOException ex) {
throw new JspException(ex.getMessage());
}
}
}
This is the implementation of the tag halder for our custom tag.
out.println(new Date());
We print the current date.
Figure: A custom tag
Tag file
The other way of creating custom tags is using the tag files. The idea is identical to how jsp files are transformed to servlets. Similarly, the tag files are first transformed into the tag handlers. And then compiled.
In the next example, we will use custom tags to indicate mandatory and non mandatory fields in a html form. Our custom tag will also have an attribute.
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<html>
<head>
<title>Tag File</title>
<style>
* { font-size: 12px; font-family: Verdana }
input, textarea { border: 1px solid #ccc }
</style>
</head>
<body>
<div style="width:400px">
<center>
<form>
<table>
<tr>
<td><t:field text="Name" mandatory="yes" /></td>
<td><input type="text" name="from"></td>
</tr>
<tr>
<tr>
<td><t:field text="Email" mandatory="no" /></td>
<td><input type="text" name="to"></td>
</tr>
<tr>
<td><t:field text="Message" mandatory="yes" /></td>
<td><textarea cols="25" rows="8" name="message"></textarea></td>
</tr>
</table>
<br>
<input type="submit" value="submit">
</form>
</center>
</div>
</body>
</html>
This is the jsp file, where we use our custom tag.
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
The prefix attribute defines the prefix that distinguishes tags defined by a given tag library from other tag libraries. The tagdir attribute identifies the location of the tag library. The value of the attribute must start with /WEB-INF/tags/
<td><t:field text="Name" mandatory="yes" /></td>
The custom tag creates a text field in the html form. It is a mandatory field, so we will see an asterix.
field.tag
<%@tag description="normal or mandatory fields" pageEncoding="UTF-8"%>
<%@attribute name="mandatory" required="true"%>
<%@attribute name="text" required="true"%>
<%
if ("yes".equals(mandatory)) {
out.println(text + "*");
} else {
out.println(text);
}
%>
The tag fiel field.tag is created using the jsp syntax. We placed the field.tag file into the WEB-INF/tags directory. If a tag is implemented as a tag file and ispackaged in WEB-INF/tags/ or a subdirectory, a TLD will be generated automatically by the web container.
Figure: TagFile project
<%@attribute name="mandatory" required="true"%>
This directive creates an attribute for our custom tag. The attribute name is mandatory and it is not optional. We must provide it, when we use the custom tag.
if ("yes".equals(mandatory)) {
out.println(text + "*");
} else {
out.println(text);
}
Mandatory fields will have an asterix.
Figure: TagFile
Random numbers
If we need a custom tag, we might look, if it wasn't already created by someone. Say we want to generate random numbers using custom tags. There is already a library to achieve this. The random tag library from the Jakarta Project. From their web http://jakarta.apache.org/taglibs/ , we download the latest random tag library. The name of the jar is taglibs-random.jar. We put the jar file into the WEB-INF/lib directory.
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/taglibs/random-1.0" prefix="rand" %>
<html>
<head>
<title>Random</title>
<style>
* { font-size: 12px; font-family: Verdana }
</style>
</head>
<body>
<h2>Random numbers</h2>
</body>
<% for (int i = 0; i < 100; i++) {%>
<% if (i % 10 == 0) {
out.println("<br>");
} %>
<rand:number id="random1" range="1-100"/>
<jsp:getProperty name="random1" property="random"/>
<% } %>
</html>
In this example, we display 100 random numbers.
<%@ taglib uri="http://jakarta.apache.org/taglibs/random-1.0" prefix="rand" %>
We declare, that we use the random tag library in our jsp page. The uri is a unique identifier for the tag library. The container tries to match it against any <taglib-uri> elements in the application’s web.xml file or the <uri> element of TLDs in JAR files in /WEB-INF/lib/ or TLDs under WEB-INF.
In our case, the Resin AS will look inside the taglibs-random.jar at the META-INF/tablib.tld file for the uri.
<taglib>
<taglib-uri>
http://jakarta.apache.org/taglibs/random-1.0
<taglib-uri>
<taglib-location>
/WEB-INF/tlds/taglibs-random.tld
<taglib-location>
</taglib>
For older containers, we must edit the web.xml file. We must provide the uri and the taglib location. For newer containers, we need not to copy the taglibs-random.tld. The TLD is already available in the jar file and the contaner will look it up automatically.
<rand:number id="random1" range="1-100"/>
<jsp:getProperty name="random1" property="random"/>
Here we create and display a random number in range from 1 .. 100.
引用
Custom JSP tags
In this part of the JEE tutorials we will talk about custom tags.
A custom tag is a user-defined JSP language element. It is an extension to the JSP language. Custom tags are reusable software components. Custom tags are used to handle common functionality. They also separate programming code from the content. They make the JSP pages look uniform. This way the JSP pages are more maintainable.
Custom tags can be created using:
Tag handlers
Tag files
Tag handlers are java classes, that implement the custom tag. A tag file is a source file containing JSP code that is translated into a simple tag handler by the web container. Same as with JSPs and serlvets.
Tag handlers can be made available to a web application in two basic ways. The classes implementing the tag handlers can be stored in an unpacked form in the WEB-INF/classes/ subdirectory of the web application. Alternatively, if the library is distributed as a JAR, it is stored in the WEB-INF/lib/ directory of the web application.
Empty custom tag
When we started with JavaServer pages, we introduced a simple example, that showed the current date. In the following example, we put the java code into the tag handler and thus, separate the code from the content.
Each custom tag implemented with a tag handler must be declared in a special xml file called tag library descriptor (TLD). The TLD file maps custom tags to their corresponding simple tag handler implementation classes.
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="d" uri="http://zetcode.com/tlds/date" %>
<html>
<head>
<title>Custom tags</title>
<style>
* { font-size: 12px; font-family: Verdana }
</style>
</head>
<body>
<h2>Date</h2>
<b>Today's Date: </b> <d:DateTag />
</body>
</html>
This is the jsp file, that will output the current date.
<%@taglib prefix="d" uri="http://zetcode.com/tlds/date" %>
The taglib directive will enable us to use the custom tag in this jsp page. The uri parameter is a unique identifier for the tag library. In the previous versions of the JSP technology, developers had to edit the web.xml file. Today this is not necessary. The container will automatically map the uri with the coresponding TLD. The uri must be unique within the application. The taglib directive also specifies the prefix, used in our custom tag.
<b>Today's Date: </b> <d:DateTag />
Here we use our custom tag. This tag displays current date and time. The DateTag is the name of the custom tag, specified in the date.tld file. We have a tag with empty body, so there is ending tag.
date.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>d</short-name>
<uri>http://zetcode.com/tlds/date</uri>
<tag>
<name>DateTag</name>
<tag-class>com.zetcode.DateTagHandler</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
This is the tag library descritor, for our example. In the tag element, we provide the name of the tag, the java class, that implements the tag. We also specify, that our tag has no body. We placed the date.tld file into the WEB-INF/tlds directory.
DateTagHandler.java
package com.zetcode;
import java.util.Date;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
public class DateTagHandler extends SimpleTagSupport {
public void doTag() throws JspException {
JspWriter out=getJspContext().getOut();
try {
out.println(new Date());
} catch (java.io.IOException ex) {
throw new JspException(ex.getMessage());
}
}
}
This is the implementation of the tag halder for our custom tag.
out.println(new Date());
We print the current date.
Figure: A custom tag
Tag file
The other way of creating custom tags is using the tag files. The idea is identical to how jsp files are transformed to servlets. Similarly, the tag files are first transformed into the tag handlers. And then compiled.
In the next example, we will use custom tags to indicate mandatory and non mandatory fields in a html form. Our custom tag will also have an attribute.
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<html>
<head>
<title>Tag File</title>
<style>
* { font-size: 12px; font-family: Verdana }
input, textarea { border: 1px solid #ccc }
</style>
</head>
<body>
<div style="width:400px">
<center>
<form>
<table>
<tr>
<td><t:field text="Name" mandatory="yes" /></td>
<td><input type="text" name="from"></td>
</tr>
<tr>
<tr>
<td><t:field text="Email" mandatory="no" /></td>
<td><input type="text" name="to"></td>
</tr>
<tr>
<td><t:field text="Message" mandatory="yes" /></td>
<td><textarea cols="25" rows="8" name="message"></textarea></td>
</tr>
</table>
<br>
<input type="submit" value="submit">
</form>
</center>
</div>
</body>
</html>
This is the jsp file, where we use our custom tag.
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
The prefix attribute defines the prefix that distinguishes tags defined by a given tag library from other tag libraries. The tagdir attribute identifies the location of the tag library. The value of the attribute must start with /WEB-INF/tags/
<td><t:field text="Name" mandatory="yes" /></td>
The custom tag creates a text field in the html form. It is a mandatory field, so we will see an asterix.
field.tag
<%@tag description="normal or mandatory fields" pageEncoding="UTF-8"%>
<%@attribute name="mandatory" required="true"%>
<%@attribute name="text" required="true"%>
<%
if ("yes".equals(mandatory)) {
out.println(text + "*");
} else {
out.println(text);
}
%>
The tag fiel field.tag is created using the jsp syntax. We placed the field.tag file into the WEB-INF/tags directory. If a tag is implemented as a tag file and ispackaged in WEB-INF/tags/ or a subdirectory, a TLD will be generated automatically by the web container.
Figure: TagFile project
<%@attribute name="mandatory" required="true"%>
This directive creates an attribute for our custom tag. The attribute name is mandatory and it is not optional. We must provide it, when we use the custom tag.
if ("yes".equals(mandatory)) {
out.println(text + "*");
} else {
out.println(text);
}
Mandatory fields will have an asterix.
Figure: TagFile
Random numbers
If we need a custom tag, we might look, if it wasn't already created by someone. Say we want to generate random numbers using custom tags. There is already a library to achieve this. The random tag library from the Jakarta Project. From their web http://jakarta.apache.org/taglibs/ , we download the latest random tag library. The name of the jar is taglibs-random.jar. We put the jar file into the WEB-INF/lib directory.
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/taglibs/random-1.0" prefix="rand" %>
<html>
<head>
<title>Random</title>
<style>
* { font-size: 12px; font-family: Verdana }
</style>
</head>
<body>
<h2>Random numbers</h2>
</body>
<% for (int i = 0; i < 100; i++) {%>
<% if (i % 10 == 0) {
out.println("<br>");
} %>
<rand:number id="random1" range="1-100"/>
<jsp:getProperty name="random1" property="random"/>
<% } %>
</html>
In this example, we display 100 random numbers.
<%@ taglib uri="http://jakarta.apache.org/taglibs/random-1.0" prefix="rand" %>
We declare, that we use the random tag library in our jsp page. The uri is a unique identifier for the tag library. The container tries to match it against any <taglib-uri> elements in the application’s web.xml file or the <uri> element of TLDs in JAR files in /WEB-INF/lib/ or TLDs under WEB-INF.
In our case, the Resin AS will look inside the taglibs-random.jar at the META-INF/tablib.tld file for the uri.
<taglib>
<taglib-uri>
http://jakarta.apache.org/taglibs/random-1.0
<taglib-uri>
<taglib-location>
/WEB-INF/tlds/taglibs-random.tld
<taglib-location>
</taglib>
For older containers, we must edit the web.xml file. We must provide the uri and the taglib location. For newer containers, we need not to copy the taglibs-random.tld. The TLD is already available in the jar file and the contaner will look it up automatically.
<rand:number id="random1" range="1-100"/>
<jsp:getProperty name="random1" property="random"/>
Here we create and display a random number in range from 1 .. 100.
发表评论
-
servlet 提交参数测试
2011-01-04 15:37 9661 上传txt 客户端:浏览器 enctype:multip ... -
jsp避免乱码
2010-12-26 13:04 780方法 1 保证 JSP文件本身格式 <%@ page l ... -
JSP 显示服务器上的文件/图片
2010-12-26 12:42 96191 显示服务器上的图片 <?xml version= ... -
JSP上传文件(使用smartUpload)
2010-12-26 10:21 14911 使用smartUpload <?xml vers ... -
JSP显示图片/XML
2010-12-25 12:31 15591 显示图片 <%@page import=&quo ... -
servlet的init方法
2010-12-23 15:34 1137init方法是在Servlet实例化之后执行的,并且只执行一次 ... -
setContentType和setCharacterEncoding
2010-12-23 15:25 1134引用 request.setCharacterEncoding ... -
jsp 内置对象
2010-12-20 17:56 789JSP对象在SERVLET中 //jsp七大隐示对象 ... -
Session详解
2010-12-06 09:01 2272http://leeldy.blog.163.com/blog ... -
jstl入门(一)
2010-11-30 22:25 850<?xml version="1.0&qu ... -
非常重要的SERVLET2.4的变化
2010-11-19 21:45 887另外,过滤器只有在使用浏览器请求一个页面的时候才能生效。所以, ... -
完美返回上一页
2010-11-19 21:34 1369有时候很多页面调用同一个页面 如果做返回上一页 首先 那个被调 ... -
jsp 路径总结
2010-11-19 21:30 18191、 System.out.println(request.g ... -
request对象
2010-11-19 21:27 955request.getParameterValues与re ...
相关推荐
在实际开发中,有时我们需要根据项目需求实现特定的功能,比如动态生成SQL或者进行复杂的业务逻辑处理,这时MyBatis的自定义标签功能就显得尤为重要。 自定义标签是MyBatis框架的一大特色,它允许我们扩展XML映射...
在Struts2中,自定义标签是提高代码可读性和可维护性的重要工具。本文将深入探讨Struts2自定义标签的实现机制、优点以及如何在实际项目中进行应用。 一、Struts2自定义标签的概念 自定义标签是JSP的一种扩展,允许...
在JavaServer Pages (JSP) 技术中,自定义标签是扩展JSP功能的关键手段,它允许开发者创建可重用的组件,提高代码的可维护性和可读性。本实例将深入探讨如何实现一个简单的JSP自定义标签。 首先,自定义标签的实现...
在ThinkPHP中,自定义标签(Tag Lib)是一种强大的特性,允许开发者扩展模板引擎的功能,使其更符合项目需求。下面将详细解释如何在ThinkPHP 3.0版本中实现自定义标签及其使用方法。 1. **自定义标签的作用** ...
一个“自定义标签”是开发者为了扩展JSP(JavaServer Pages)功能而创建的,它允许我们封装复杂逻辑并将其作为可重用的组件。本项目提供了一个用于分页的自定义标签,适用于那些需要在网页上进行数据分页展示的应用...
本篇文章将详细阐述如何在CKEditor 4.0中进行自定义标签操作,并提供经过修改的编辑器JAR包供下载。 CKEditor 4.0是一款开源的JavaScript富文本编辑器,它支持多种浏览器环境,提供丰富的功能和高度可定制性。...
在Java Server Pages (JSP) 2.0版本中,自定义标签和自定义标签函数极大地扩展了JSP开发的灵活性和可重用性。这些特性允许开发者创建可复用的组件,使代码更加清晰,易于维护。本文将深入探讨JSP 2.0中的自定义标签...
在Spring框架中,自定义标签是一项非常实用的功能,它允许我们创建符合XML语法的自定义元素,以便在配置文件中更方便地表达业务逻辑。在"spring自定义标签例子"这个项目中,我们可以深入理解这一特性,特别是通过...
本案例通过JavaScript来实现自定义标签的功能。具体而言,它通过遍历文档中的内容,将自定义的标签转换为标准的HTML标签,并应用相应的样式。下面我们将详细介绍其实现细节。 #### 自定义标签代码解析 ```...
在JavaServer Pages (JSP) 技术中,自定义标签是提高代码可重用性和模块化的重要手段。自定义标签允许开发者创建自己的组件,这些组件可以像HTML标签一样在页面上使用,使得代码更加清晰易读。本教程将深入探讨JSP...
在 Struts 中,自定义标签是提供一种灵活的方式,使得视图层(通常为 JSP 页面)能够更好地与业务逻辑交互,从而提高代码的可读性和可维护性。本文将深入探讨如何在 Struts 中实现自定义标签。 一、自定义标签概述 ...
在JSP(JavaServer Pages)开发中,自定义标签是一种扩展JSP功能的强大工具,它允许开发者创建可重用的代码组件,以提高代码的可读性和可维护性。本篇学习笔记将深入探讨JSP自定义标签的相关概念、创建方法以及实际...
首先,创建自定义标签的目的是为了增加语义化,使网页内容更易于机器理解,同时提高人类阅读的清晰度。例如,可以定义`<article>`、`<sidebar>`等标签来明确内容区块的类型。自定义标签遵循驼峰式命名规则,如`...
在Java Web开发中,自定义标签(Custom Tags)是一个强大的工具,它允许开发者创建可重用的组件,提高代码的可读性和维护性。自定义标签是JSP标准标签库(JSTL)的一种扩展,它能将复杂的业务逻辑封装起来,使JSP...
Java自定义标签是JavaServer Pages (JSP)技术的一个强大特性,它允许开发者扩展JSP的标准标签库,创建自己的定制化标签,以提高代码的可读性和可维护性。自定义标签可以封装复杂的业务逻辑或者视图呈现,使得页面...
JSP(JavaServer Pages)自定义标签是JSP技术中一个重要的特性,允许开发者创建可重用的、自定义的组件,从而提高代码的可读性和可维护性。本文将深入讲解JSP自定义标签的相关概念、格式、处理过程以及创建和使用...
使用jsp自定义标签的功能实现权限的控制。(如果用户没有某个模块的删除权限,就不现实这个删除按钮) 在整个项目中所有的页面都可以引入自定义的标签去做到权限的控制。 自定义标签文件 删除 可以控制页面中的每...
在这个"Freemarker自定义标签简单案例"中,我们将深入探讨如何利用Freemarker进行模板设计,并结合自定义标签来增强模板的功能。 首先,让我们理解Freemarker的基础知识。Freemarker是一个与语言无关的模板引擎,它...
JSP自定义标签是用户定义的JSP语言元素,可以看成是一种通过标签处理器生成基于XML脚本的方法。自定义标签在功能上和逻辑上都与JavaBean类似,都是一组可重用的组件代码。相较于JavaBean,自定义标签可以使Web开发者...