分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

jsp自定义标签

发布时间:2023-09-06 01:10责任编辑:赖小花关键词:jsjsp

环境:

项目加上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页面

页面效果

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还是同样

结果:

至于跳转到页面的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就可以直接导自己的库,不用下载。

随便挑一个就行了。

还有如果碰到这个异常:

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

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

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

jsp自定义标签

原文地址:http://www.cnblogs.com/lq625424841/p/7511467.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved