• 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再也拿不到了。

  • 相关阅读:
    人工智能,人工神经网络,机器学习,深度学习,卷积神经网络 时间线与内在联系(转载)
    基于spark logicplan的表血缘关系解析实现
    spark ml pipeline构建机器学习任务
    常用特征离散化方法
    spark sql插入表时的文件个数研究
    Spark累加器(Accumulator)
    java中使用URLClassLoader访问外部jar包的java类
    Scala里面的排序函数的使用
    Spark获取DataFrame中列的几种姿势--col,$,column,apply
    spark.sql.shuffle.partitions和spark.default.parallelism的区别
  • 原文地址:https://www.cnblogs.com/lq625424841/p/7511467.html
Copyright © 2020-2023  润新知