public class ShowIp extends SimpleTagSupport {/** * 以下屏蔽的代码在SimpleTagSupport代码中已经做了!这里不需要重复再做! *//*private JspContext context;*//** * 传入pageContext *//*@Overridepublic void setJspContext(JspContext pc) { this.context = pc;}*/@Overridepublic void doTag() throws JspException, IOException { PageContext pageContext=(PageContext)this.getJspContext(); ServletRequest request = pageContext.getRequest(); String ip=request.getRemoteHost(); JspWriter out = pageContext.getOut(); out.write("使用自定义标签展示客户ip地址"+ip); List<String> a=null;}}在web项目的WEB-INF目录下建立mytaglib.tld文件,这个tld叫标签库的声明文件。(参考核心标签库的tld文件)
<?xml version="1.0" encoding="UTF-8" ?><taglib 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-jsptaglibrary_2_1.xsd"version="2.1"><description>A tag library exercising SimpleTag handlers.</description><!-- 标签库的版本 --><tlib-version>1.0</tlib-version><!-- 标签库前缀 --><short-name>rlovep</short-name> <!-- tld文件的唯一标记 --> <uri>http://rlovep.com</uri> <!-- 定义标签,标签要放在方法前面 --> <tag> <!-- 标签名 --> <name>showIp</name> <!-- 标签处理类 --> <tag-class>com.rlovep.tags.ShowIp</tag-class> <body-content>empty</body-content> </tag> <tag></taglib>在jsp页面的头部导入自定义标签库:url为你在tld中写的url,前缀也是你在tld文件中定义的
<%@ taglib uri="http://rlovep.com" prefix="rlovep" %>在jsp中使用自定义标签
<%-- 测试简单的自定义标签,标签体(我是你)不显示 --%><rlovep:showIp>我是你</rlovep:showIp>3.自定义标签的执行过程
<rlovep:showIp>我是你</rlovep:showIp>但要文字显示需要修改处理类和tld文件:
JspContext jspContext2 = this.getJspContext();//显示标签体的两种方法//方法1直接调用//jspBody.invoke(null);//方法2通过输出到out//jspBody.invoke(jspContext2.getOut());修改tld文件:
<tag> <!-- 标签名 --> <name>showIp</name> <!-- 标签处理类 --> <tag-class>com.rlovep.tags.ShowIp</tag-class> <!-- 输出标签体的内容格式标签体不可以写jsp的java代码 --> <body-content>scriptless</body-content> </tag>现在你可以将标签体的内容显示了;
<%-- 标签提会显示 --%><rlovep:showIp>我是你</rlovep:showIp>输出标签体的内容格式:
<!-- 测试带属性的标签,标签体显示通过类处理 --><rlovep:AttributeTags name="peace" value="12345定义属性步骤如下:
添加俩个属性://声明属性的成员变量private Integer value;private String name;并为两个成员属性写setter方法;public void setValue(Integer value)public void setName(String name)在标签库文件tld注明此标签和属性:
<!-- 标签名 --> <name>AttributeTags</name> <!-- 标签处理类 --> <tag-class>com.rlovep.tags.AttributeTags</tag-class> <!-- 输出标签体的内容格式标签体不可以写jsp的java代码 --> <body-content>scriptless</body-content> <!-- 配置属性name --> <attribute> <name>name</name> <!-- 是否必填 --> <required>true</required> <!-- 是否支持EL表达式 --> <rtexprvalue>true</rtexprvalue> </attribute> <!-- 配置属性value --> <attribute> <name>value</name> <!-- 是否必填 --> <required>true</required> <!-- 是否支持EL表达式 --> <rtexprvalue>true</rtexprvalue> </attribute> </tag>现在就可以用带属性的标签了
6.带有子标签的自定义标签:
就像核心标签库的choose标签一样我们也可以定义嵌套的自定义标签,这部分我们主要讲解自己创建一个类似核心标签库的choose标签。步骤如下:
建立处理类,处理类还是与前面一样的方法。需要介绍的是用到了一个getParent()方法,从名字上就可以知道是为了获得父标签,对就是获得父标签类;
建立三个处理类文件: ChooseTag,OtherWiseTag,whenTag
//ChooseTag类:public class ChooseTag extends SimpleTagSupport{//此去时变量不是标签属性,由when标签更改;othewise获得;private boolean flag; public boolean isFlag() { return flag;}public void setFlag(boolean flag) { this.flag = flag;}@Overridepublic void doTag() throws JspException, IOException { // Choose标签作用显示标签体,以及作为其他两个标签的父标签; getJspBody().invoke(null);}}//whenTag类public class whenTag extends SimpleTagSupport{//增加test属性private boolean test;public boolean isTest() { return test;}public void setTest(boolean test) { this.test = test;}@Overridepublic void doTag() throws JspException, IOException { //如果标签属性为true,显示标签体 if(test){getJspBody().invoke(null); } //设置父标签给otherwise用 ChooseTag parent=null; if(getParent() instanceof ChooseTag){parent=(ChooseTag)getParent();parent.setFlag(test); }}}//OtherWiseTag类:public class OtherWiseTag extends SimpleTagSupport {@Overridepublic void doTag() throws JspException, IOException { boolean test=true; //获取父标签的test,由他的上一个when设置 if(getParent() instanceof ChooseTag) {//获取父标签的test,由他的上一个when设置ChooseTag parent=(ChooseTag)getParent();test=parent.isFlag(); } if(!test){getJspBody().invoke(null); }}}编写tld文件:与其他的标签定义一模一样
<!-- 定义标签,choose--> <tag> <!-- 标签名 --> <name>choose</name> <!-- 标签处理类 --> <tag-class>com.rlovep.tags.ChooseTag</tag-class> <!-- 输出标签体的内容格式标签体不可以写jsp的java代码 --> <body-content>scriptless</body-content> </tag> <!-- 定义标签,when--><tag> <!-- 标签名 when --> <name>When</name> <!-- 标签处理类 --> <tag-class>com.rlovep.tags.whenTag</tag-class> <!-- 输出标签体的内容格式标签体不可以写jsp的java代码 --> <body-content>scriptless</body-content> <!-- 配置属性name --> <attribute> <name>test</name> <!-- 是否必填 --> <required>true</required> <!-- 是否支持EL表达式 --> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <!-- 定义标签,Otherwise--> <tag> <!-- 标签名 --> <name>otherwise</name> <!-- 标签处理类 --> <tag-class>com.rlovep.tags.OtherWiseTag</tag-class> <!-- 输出标签体的内容格式标签体不可以写jsp的java代码 --> <body-content>scriptless</body-content> </tag>使用带子标签的标签:与使用其他标签稍微有些不同,需要嵌套
<!-- 测试choose --><rlovep:choose><rlovep:When test="${10<5 }"> 条件成立执行when</rlovep:When><rlovep:otherwise> 条件不成立执行otherwise</rlovep:otherwise></rlovep:choose>自定义标签就介绍到这里;
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><!-- 配置空指针异常 --> <error-page><exception-type>java.lang.NullPointerException</exception-type><location>/error.jsp</location> </error-page> <!-- 配置505错误页面 --> <error-page> <error-code>500</error-code> <location>/common/500.jsp</location> </error-page> <!-- 配置404错误页面 --> <error-page> <error-code>404</error-code> <location>/common/404.html</location> </error-page></web-app>JSP入门就介绍到这里,希望对大家的学习有所帮助。