jsp自定义标签、EL自定义方法

平时开发经常用到token,我们会通过ajax请求把数据在放到jsp页面的隐藏标签里面。这时自定义标签的好处就体现出来了。页面直接使用token标签,并且会获取得到后台的值。


这里写图片描述


建立tld约束配置

tld其作用一般是在web项目中。jstl、c标签等用于有效性判断、权限判断等方面,对前端的一些页面标签起到约束、限制的作用。

MyTag.tld

<?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,不写也可以,以web.xml为准 --> 

    <tag>
        <name>token</name>
        <description>token标签</description>
        <body-content>empty</body-content> 
        <tag-class>tag.Token</tag-class> <!-- 指向tag包的Token类,对应处理 -->
    </tag>
</taglib>   

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"
    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">
    <display-name>JspTagDefined</display-name>

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

</web-app>

Token的实现类

继承SimpleTagSupport

public class Token extends SimpleTagSupport {

    @Override
    public void doTag() throws JspException, IOException {
        //使用随机数模拟token
        String token = (System.currentTimeMillis() + new Random().nextInt(999999999)) + "";
        //放到session中,方便登录校验
        ((PageContext) this.getJspContext()).getSession().setAttribute("token", token);
        //写到隐藏的input里面
        JspWriter out = ((PageContext) this.getJspContext()).getOut();
        out.print("<input type='hidden' value=" + token + "></input>");
        out.print("你的token为:" + token);
    }

}

Jsp页面使用

引入映射到web.xml的路径

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!-- prefix前缀 urI对应web.xml里面的映射地址和tld文件的url  -->
<%@ taglib prefix="MyTag" uri="/MyTag"%>
<!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>
    <MyTag:token />

</body>
</html>

结果:

页面里面有隐藏的input标签
这里写图片描述

EL自定义方法

平时后台获取数据到页面上,经常会有空字符串或者空值。以及对很多的数据代号进行字典转化,一个个写c:if也行,每个页面都会重复一份,贼恶心。使用el自定义方法进行字典转化,后台只需要维护一份就可以了。

新建Function.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 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>


    <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>

<function-signature>标签:返回类型 方法名 参数类型

实际处理类Function

/** 
 * @author: 07
 * @date: 2017年9月12日 下午2:25:39 
 * @version 1.0
 * @parameter test
 * @return  
 */
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;

    }
}

页面调用:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="MyTag" uri="/MyTag"%>
<!-- prefix前缀 urI对应web.xml里面的映射地址和tld文件的url  -->
<%@ taglib prefix="fns" uri="/Function.tld"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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>
    <MyTag:token />
    <br>
    <span>文本判断内容:${fns:isNull('')}</span>
    <br>
    <span>字典转换:${fns:change('Q') }</span>
    <br>
    <c:out value="hello"></c:out>

</body>
</html>

结果:
这里写图片描述


细心的小伙伴们会发现,自定义标签的引入和自定义方法的引入为什么不一样,都是tld约束。

<%@ taglib prefix="MyTag" uri="/MyTag"%>
<!-- prefix前缀 urI对应web.xml里面的映射地址和tld文件的url  -->
<%@ taglib prefix="fns" uri="/Function.tld"%>

后者相当于直接引用本地项目中的tld,前者通过web再映射到MyTag.tld,只是写法上的差异。
后者相当省事,不用注册。但是碰到要本项目提供给其他项目使用约束的时候就必须得映射了。

后面附上代码:github地址

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值