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通过“依赖注入”写进去
理解TagSupport类的生命周期及使用示例
本文详细解析了TagSupport类的生命周期,并通过实际案例展示了如何配置TLD文件、实现依赖注入以及在JSP页面中使用该类来获取本机的主机名。
341

被折叠的 条评论
为什么被折叠?



