自定义标签

1. 自定义标签,输出到页面上(可用于css、scripte等标签简写)

首先:定义标签处理类

package com.inspur.test.taglabel;
import java.io.IOException;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class testTagLabel extends TagSupport {
	private String name = "lili";
	private String pwd = "lucky";
	public int  doStartTag (){
		try {
			JspWriter out = this.pageContext.getOut();
			out.println("===============lili+++lucky============");
			out.println("<br>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
}

 其次,定义标签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 web-jsptaglibrary_2_0.xsd"
	version="2.0">
	<tlib-version>1.0</tlib-version>
	<jsp-version>2.0</jsp-version>
	<short-name>jsplabel</short-name>
	<uri>/tags/tagtest</uri>
	<tag>
	    <name>showUserInfo</name>
	    <tag-class>com.inspur.test.taglabel.testTagLabel</tag-class>
	    <body-content>empty</body-content>
	    <attribute>
	        <name>user</name>
	         <required>false</required>
	         <rtexprvalue>true</rtexprvalue>
	         <type>java.lang.String</type>
	    </attribute>
	    <attribute>
			<name>pwd</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
			<type>java.lang.String</type>
			</attribute>
	 </tag>
</taglib>

 然后,在web-inf下的web.xml里配置映射

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>testJsp</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <jsp-config>
		<taglib>
			<taglib-uri>/tags/tagtest</taglib-uri>
			<taglib-location>/WEB-INF/tagtest.tld</taglib-location>
		</taglib>
	</jsp-config>
</web-app>

 最后,在jsp里使用:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="jsplabel" uri="/tags/tagtest"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsplabel:showUserInfo/>
</body>
</html>

 输出结果:

===============lili+++lucky============ 

如果是<script .........>====>可以被解析成引入js,简单方便

 

 

2. 自定义函数标签

首先:定义标签处理类,一定注意,定义的函数必须是public static的,切记

package com.inspur.test.taglabel;

public class TestFunLabel {
	public static String  getUserName (){
		return "=============name==lili========";
	}
}

 

 其次,定义函数标签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 web-jsptaglibrary_2_0.xsd"
	version="2.0">
	<tlib-version>1.0</tlib-version>
	<jsp-version>2.0</jsp-version>
	<short-name>jsplabel</short-name>
	<uri>/tags/testfun</uri>
	<function>
		<name>getUserName</name>
		<function-class>com.inspur.test.taglabel.TestFunLabel</function-class>
		<function-signature>java.lang.String  getUserName()</function-signature>   
    </function>
</taglib>

  然后,在web-inf下的web.xml里配置映射

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>testJsp</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<jsp-config>
		<taglib>
			<taglib-uri>/tags/testfun</taglib-uri>
			<taglib-location>/WEB-INF/tlds/funtest.tld</taglib-location>
		</taglib>
	</jsp-config>
	
</web-app>

 

  最后,在jsp里使用:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fn" uri="/tags/testfun"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${fn:getUserName()}
</body>
</html>

 输出结果:‘

 

 =============name==lili========

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值