自定义标签

来源:互联网 发布:光学数据介质 编辑:程序博客网 时间:2024/06/03 17:28
 

自定义标签主要用于移除Jsp页面中的java代码。

Tag接口

自定义标签功能扩展

自定义标签除了可以移除Jsp页面中的java代码外,它还可以用于完成一些页面逻辑,例如:

通过自定义标签可以控制jsp页面某一部分内容是否执行。

例如:<c:if>标签

通过自定义标签可以控制标签后的jsp页面是否执行。

通过自定义标签可以控制jsp页面某一部分内容重复执行。

例如:<c:foreach>标签

通过自定义标签可以修改jsp页面内容输出

tld文件中的四种标签体类型

EMPTY  JSP  scriptless  tagdepentend

简单标签共定义了5个方法:

setJspContext方法

用于把JSP页面的pageContext对象传递给标签处理器对象

setParent方法

用于把父标签处理器对象传递给当前标签处理器对象

getParent方法

用于获得当前标签的父标签处理器对象

setJspBody方法

用于把代表标签体的JspFragment对象传递给标签处理器对象

doTag方法

用于完成所有的标签逻辑,包括输出、迭代、修改标签体内容等。在doTag方法中可以抛出javax.servlet.jsp.SkipPageException异常,用于通知WEB容器不再执行JSP页面中位于结束标记后面的内容,这等效于在传统标签的doEndTag方法中返回Tag.SKIP_PAGE常量的情况

移除jsp页面中的java代码,只需要完成两个步骤:

编写一个实现Tag接口的Java类,并覆盖doStartTag方法,把jsp页面中的java代码写到doStartTag方法中。

编写标签库描述符(tld)文件,在tld文件中对自定义标签进行描述。

完成以上操作,即可在JSP页面中导入和使用自定义标签

Java代码

package com.hbsi.web.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.tagext.TagSupport;

//标签处理器----把jsp页面中嵌入java代码拿出来放到该类的doStatrtTag中

//向输出用户的ip地址

public class CusTagDemo1 extends TagSupport {

         @Override

         public int doStartTag() throws JspException {

                   // TODO Auto-generated method stub

                   HttpServletRequest request = (HttpServletRequest) this.pageContext

                                     .getRequest();

                   JspWriter jw = pageContext.getOut();

                   String IP = request.getRemoteAddr();

                   try {

                            jw.println(IP);

                   } catch (IOException e) {

                            // TODO Auto-generated catch block

                            throw new RuntimeException();

                   }

                   return super.doStartTag();

         }

 

}

Jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

 <%@taglib uri="http://www.hbsi.edu.cn" prefix="hbsi"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'TagDemo1.jsp' starting page</title>

   

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css"href="styles.css">

    -->

 

  </head>

 

  <body>

 

    您的IP是:

    <hbsi:viewIP></hbsi:viewIP>

  </body>

</html>

Tld代码

<?xml version="1.0" encoding="UTF-8" ?>

<!--

 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 theApache 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.

-->

 

<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_0.xsd"

    version="2.0">

    <description>A tag library exercising SimpleTag handlers.</description>

    <tlib-version>1.0</tlib-version>

    <short-name>hbsi</short-name>

    <uri>http://www.hbsi.edu.cn</uri>

    <tag>

    <name>viewIP</name>

    <tag-class>com.hbsi.web.tag.CusTagDemo1</tag-class>

    <body-content>empty</body-content>

</tag>

</taglib>