JSP标签开发--详解
来源:互联网 发布:黑客引导页html源码 编辑:程序博客网 时间:2024/06/15 20:55
标签开发之几大步骤:
1,开发标签类,继承TagSupport类,
- package org.lxh.tagdemo;
- import javax.servlet.jsp.*;
- import javax.servlet.jsp.tagext.*;
- public class HelloTag extends TagSupport{
- public int doStartTag()throws JspException{
- JspWriter out =super.pageContext.getOut();
- try{
- out.println("<h1>hello World!!</h1>");
- }catch(Exception e){}
- return TagSupport.SKIP_BODY;
- }
- };
注意点 :编译的时候可能报错,请注意 tomcat路径下,jsp-api.jar的路径问题,把此文件夹放到 java环境下,jdk/jre/lib/ext 包下面,
2,定义一个标签的描述文件,标签的描述文件的后缀 "*.tld",而且此文件也符合XML语法规则。
- <?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_1.xsd"
- version="2.1">
- <tlib-version>1.0<tlib-version> <!--表示标签库的版本-->
- <short-name>firsttag</short-name> <!--为标签库在TLD中的描述名称-->
- <tag>
- <name>hello</name>
- <tag-class>org.lxh.tagdemo.HelloTag</tag-class>
- <body-content>empty</body-content>
- </tag>
- </taglib>
3,在JSP文件之中使用此标签。
4,标签组成部分
4.1>.标签处理类: HelloTag.java;
4.2>.标签描述文件:hellotag.tlc
4.3>.JSP页面: 通过<%@ taglib%>定义标签.
4.4>(可选)在web.xml文件中配置映射名称.
********************************************************************************
1,定义有属性的标签
示例:自定义日期格式化操作
- package org.lxh.tagdemo;
- import java.text.*;
- import java.util.*;
- import javax.servlet.jsp.*;
- import javax.servlet.jsp.tagext.*;
- public class DateTag extends TagSupport{
- private String format; //设置属性的时候可以通过setter完成
- public int doStartTag()throws JspException{
- SimpleDateFormat sdf = new SimpleDateFormat(this.format);
- //表示进行格式化的日期显示操作
- try{
- super.pageContext.getOut().write(sdf.format(new Date()));
- }catch (Exception e){
- e.printStackTrace();
- }
- return TagSupport.SKIP_BODY;
- }
- public void setFormat(String format){
- this.format = format;
- }
- public String getFormat(){
- return this.format;
- }
- };
2,编写 *.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_1.xsd"
- version="2.1">
- <tlib-version>1.0<tlib-version> <!--表示标签库的版本-->
- <short-name>datetag</short-name> <!--为标签库在TLD中的描述名称-->
- <tag>
- <name>date</name>
- <tag-class>org.lxh.tagdemo.DateTag</tag-class>
- <body-content>empty</body-content>
- <attribute> <!-- 定义属性 -->
- <name>format</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue><!-- 支持表达式输出 -->
- </attribute>
- </tag>
- </taglib>
3,为了方便的使用,直接在web.xml文件之中定义此标签库.
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version="2.5">
- <display-name>Welcome to Tomcat</display-name>
- <description>
- Welcome to Tomcat
- </description>
- <jsp-config>
- <taglib>
- <taglib-uri>mldn_date</taglib-uri>
- <taglib-location>/WEB-INF/datetag.tld</taglib-location>
- </taglib>
- </jsp-config>
- </web-app>
************************************************************************************
TagSupport类的简单介绍:
1,
doStartTag()
2,
int
doAfterBody()
Default processing for a body.
<!-- Generated by javadoc (build 1.6.0_20) on Fri Jun 04 05:41:16 PDT 2010 -->
<noscript></noscript>SKIP_BODY
static final int SKIP_BODY :
表示表前提内容会被忽略,并且将执行权转交给 doEndTag()方法.
<!-- Generated by javadoc (build 1.6.0_20) on Fri Jun 04 05:41:16 PDT 2010 -->
<noscript></noscript>EVAL_BODY_INCLUDE
static final int EVAL_BODY_INCLUDE
表示重复执行标签体的内容,会重复条用doAfterBody()方法,一直循环执行下去,直到doAfterBody()方法返回SKIP_BODY为止。
<!-- Generated by javadoc (build 1.6.0_20) on Fri Jun 04 05:41:16 PDT 2010 -->
<noscript></noscript>doEndTag
int doEndTag() throws JspException
static int
SKIP_PAGE
表示JSP页面应该立刻停止执行,并将所有的输出立刻回传到浏览器上
static int
EVAL_PAGE
表示JSP可以正常的运行完毕。
1.1 编写 *.java 文件:
- package org.lxh.tagdemo;
- import java.text.*;
- import java.util.*;
- import java.io.*;
- import javax.servlet.jsp.*;
- import javax.servlet.jsp.tagext.*;
- public class AttributeTag extends TagSupport{
- private String name;
- private String scope;
- public int doStartTag() throws JspException{
- //判断属性是否存在
- Object value=null;
- if("page".equals(this.scope)){ //是否是page范围
- value = super.pageContext.getAttribute(this.name,PageContext.PAGE_SCOPE);
- }
- if("request".equals(this.scope)){ //是否是page范围
- value = super.pageContext.getAttribute(this.name,PageContext.REQUEST_SCOPE);
- }
- if("session".equals(this.scope)){ //是否是page范围
- value = super.pageContext.getAttribute(this.name,PageContext.SESSION_SCOPE);
- }
- if("application".equals(this.scope)){ //是否是page范围
- value = super.pageContext.getAttribute(this.name,PageContext.APPLICATION_SCOPE);
- }
- if(value == null){
- return TagSupport.SKIP_BODY; //没有属性不执行标签体
- } else {
- return TagSupport.EVAL_BODY_INCLUDE ; //执行标签体
- }
- }
- public void setName(String name){
- this.name = name;
- }
- public void setScope(String scope){
- this.scope =scope;
- }
- public String getName(){
- return this.name;
- }
- public String getScope(){
- return this.scope;
- }
- };
1.2, 编写 *.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_1.xsd"
- version="2.1">
- <tlib-version>1.0</tlib-version>
- <short-name>mldntag</short-name>
- <tag>
- <name>present</name>
- <tag-class>org.lxh.tagdemo.AttributeTag</tag-class>
- <body-content>JSP</body-content>
- <attribute>
- <name>name</name>
- <required>true</required>
- <rtexprvalue>true<rtexprvalue>
- </attribute>
- <attribute>
- <name>scope</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- </taglib>
1.3,在 WEB-INF/web.xml 文件里面配置标签属性,
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version="2.5">
- <display-name>Welcome to Tomcat</display-name>
- <description>
- Welcome to Tomcat
- </description>
- <jsp-config>
- <taglib>
- <taglib-uri>mldn_date</taglib-uri>
- <taglib-location>/WEB-INF/datetag.tld</taglib-location>
- </taglib>
- <taglib>
- <taglib-uri>mldn_page</taglib-uri>
- <taglib-location>/WEB-INF/mldntag.tld</taglib-location>
- </taglib>
- </jsp-config>
- </web-app>
1.4,开始写页面
- <%@ page contentType="text/html" pageEncoding="GBK"%>
- <%@ taglib prefix="mytag" uri="mldn_page"%>
- <html>
- <head>
- <title>www.MLDNJAVA.cn</title>
- </head>
- <body>
- <h1>
- <%
- String scope = "session";
- session.setAttribute("username","zhangsan");
- %>
- <mytag:present name="username" scope="<%=scope%>">
- <h2><%=scope%>范围存在属性,内容是:"${sessionScope.username}"</h2>
- </mytag:present>
- <mytag:present name="allusers" scope="request">
- <h2><%=scope%>范围存在属性,内容是:"${requestScope.allusers}"</h2>
- </mytag:present>
- </h1>
- </body>
- </html>
<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
BodyTagSupport : 是TagSupport类的子类,通过集成BodyTagSupport类实现的标签可以直接处理标签体内容的数
BodyTagSupport类的定义如下:
public class BodyTagSupport extends TagSupport implements BodyTag
标签的开发在实际的开发中使用的很少,主要有第三方的提供的一些插件(重点 )
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
JSP 2.0 之后提供了 SimpleTagSupport 类,直接覆写里面的doTag()方法即可完成。
- JSP标签开发--详解
- 菜鸟学Android(四十四):jsp标签技术简介及jsp标签的开发详解
- jsp---JSTL标签详解
- JSP自定义标签详解
- 详解自定义JSP标签
- JSP标签详解
- JSP: JSTL标签详解
- JSP自定义标签详解
- JSP动作标签详解
- jsp 自定义标签详解
- JSP标签详解
- JSP自定义标签开发+TLD文件元素详解+实例
- JSP自定义标签开发+TLD文件元素详解
- JSP自定义标签开发+TLD文件元素详解
- JSP自定义标签开发+TLD文件元素详解
- JSP自定义标签开发+TLD文件元素详解
- JSP自定义标签开发+TLD文件元素详解
- jsp自定义标签开发
- Android视图绘制流程完全解析,带你一步步深入了解View(二)
- log file sync 诊断脚本
- Java二叉树(三)--非递归的中序_先序_后序遍历二叉树
- RxJava&RxAndroid学习
- 浮层引导页Activity
- JSP标签开发--详解
- GCD 深入理解(二)
- BMDThread控件动态创建多线程示例
- zookeeper启动失败,zkServer.sh status 出错
- MongoDB常用命令
- linux signal 处理
- Android视图状态及重绘流程分析,带你一步步深入了解View(三)
- ios socket 实现简单的聊天系统
- leetcode记录 104. Maximum Depth of Binary Tree