TagSupport类的生命周期及获取主机名的步骤

本文详细解析了TagSupport类的生命周期,并通过实际案例展示了如何配置TLD文件、实现依赖注入以及在JSP页面中使用该类来获取本机的主机名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TagSupport类的生命周期


获取本机的主机名:步骤
1.先建一类:LocalNameTag.java注意要写上“依赖注入”public void setCount(int count) {
		this.count = count;
	}
2.要配置好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 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.
-->

<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>
    <!-- 建议的prefix的名称 -->
    <short-name>wtd</short-name>
    <!-- uri地址 -->
    <uri>http://www.hbsi.com/ip-taglib</uri>
    
  
    <tag>
    	
        <!-- 标签描述 -->
        <description>定义一个ip输出的标签</description>
       	<!-- 标签名称 -->
        <name>name</name>
        <!-- 标签的处理类 -->
        <tag-class>www.youkuaiyun.com.blank.tag.LocalNameTag</tag-class>
        <!-- 标签中的内容 -->
      <!--   <body-content>scriptless</body-content> -->
         <body-content>scriptless</body-content>
         <!-- 配置属性 -->
         <attribute>
         <!-- 属性名称: -->
         <name>count</name>
         <!-- 是否是必须 -->
         <required>true</required>
         <!-- 是否是动态 值-->
         <rtexprvalue>false</rtexprvalue>
         </attribute>
    </tag>
</taglib>


3.index.jsp中用引入的tld文件(写上名字)<body>
   输出名 <wtd:name count="2">王铁登</wtd:name></body>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.hbsi.com/ip-taglib" prefix="wtd" %>
<%
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 'index.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>
   输出名:
  
  <wtd:name count="2">
  王铁登
  </wtd:name>
  </body>
</html>
这里的count=2通过“依赖注入”写进去


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值