jsp自定义标签

环境:

image项目加上tomcat运行时环境就行了。

1.可以定义一些自己命名的标签,具有特定的功能的标签体。

首先建立一个类对应标签的处理,继承SimpleTagSupport(ps:还有其他各种继承方式),重写doTag()方法

public class Token extends SimpleTagSupport {
    private static final long serialVersionUID = 1L;

    @Override
    public void doTag() throws JspException, IOException {
         String token = (System.currentTimeMillis() + new Random().nextInt(999999999)) + "";
         ((PageContext)this.getJspContext()).getSession().setAttribute("token", token);
            JspWriter out = ((PageContext)this.getJspContext()).getOut();
            out.print("<input type='hidden' value="+token+"></input>");
            out.print("你的token为:"+token);
    }   
}

再建立一个类搞多一个不同的标签,用来打印输出,有一个属性value,对应一个类属性

public class Print extends SimpleTagSupport {
    private String value; 

    @Override
    public void doTag() throws JspException, IOException {
        JspWriter out = ((PageContext)this.getJspContext()).getOut();
        out.print("hello world"+this.getValue());
    }


    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
    
}

其次建立一个tld文件,其中shor-name就是标签的头部。uri就是在jsp页面引用的一致,不写也可以,以web.xml为准

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>mytag</short-name> <!-- 头部    -->
    <uri>/mytag</uri>    <!--jsp引用时用的url --> 
    
    <tag>
        <name>token</name>
        <description>token标签</description>
        <body-content>empty</body-content> 
        <tag-class>Tag.Token</tag-class>
    </tag>
    <tag>
        <name>print</name>
        <description>打印功能的标签</description> 
        <!-- 下面的值不为空就是有标签体 -->
        <body-content></body-content>
        <tag-class>Tag.Print</tag-class>
        <attribute>
            <name>value</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue> <!-- 是否支持EL表达式 -->
        </attribute>
    </tag>
</taglib>

然后就是web.xml 加上下面配置,外面的jsp-config有时不用也可以,不知道什么情况。

<!-- 映射好自定义标签文件的路径  -->
<jsp-config>
    <taglib>
        <taglib-uri>/mytag</taglib-uri>
        <taglib-location>/MyTld.tld</taglib-location>
    </taglib>
</jsp-config>

最后就是jsp页面

image

页面效果

image

 

2.自定义标签方法

同样建立一个提供方法的类,跟我们常用的工具类一样,要静态方法,我这里简单一点就是判空和字典转换。我的所在的包是Tag包

public class Function {
    /** 判断字符串是否为空 */
    public static boolean isNull(String str){
        if(str ==null || "".equals(str)){
            return true;
        }else{
            return false;
        }
    }
    public static String change(String string){
        String str=null;
        switch (string) {
        case "R":
            str="无卡快捷";
            break;
        case "Q":
            str="QQ钱包";
            break;
        case "Z":
            str="支付宝支付";
            break;
        default:
            break;
        }
        return str;
        
    }
}

然后建立tld文件,头部拷贝就行了,这里的url就是jsp里面引用的,所以一定要写,不用配置web.xml

<?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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <description>自定义方法</description>
    <tlib-version>1.0</tlib-version>
    <short-name>fns</short-name>
    <url>fns/myfns</url>
    <function>
        <description>判断是否为空返回true or false</description>
        <name>isNull</name>
        <function-class>Tag.Function</function-class>
        <function-signature>
        java.lang.boolean isNull(java.lang.String)
        </function-signature>
        <example>${fns:isNull(str)}</example>
    </function>    
    <function>
        <description>字典转换</description>
        <name>change</name>
        <function-class>Tag.Function</function-class>
        <function-signature>
        java.lang.String change(java.lang.String)
        </function-signature>
        <example>${fns:change(str)}</example>
    </function>
    
</taglib>

jsp还是同样

image

结果:

image

至于跳转到页面的servlet我就不贴出来了,

 

3.补充一下使用jstl导报问题

直接下载jstl.jar和standard.jar加到web下的lib.

jsp头部导入

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

即可。

如果mycelipse就可以直接导自己的库,不用下载。

image

image

随便挑一个就行了。

还有如果碰到这个异常:

index.jsp (line: 9, column: 61) The JSP specification requires that an attribute name is preceded by whitespace

就是自己这个页面第九行少了个空格困了

还有重定向后所有的作用域内的参数都会丢失。用el再也拿不到了。

转载于:https://www.cnblogs.com/lq625424841/p/7511467.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值