分享web开发知识

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

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

JSP自定义标签的使用简化版

发布时间:2023-09-06 01:54责任编辑:苏小强关键词:暂无标签

在jsp中 如果不想页面中出现java代码 这个时候就需要使用到jsp的自定义标签库技术了

自定义标签库 能够有效的减少jsp中java代码的出现 使其更加自然的像html页面一样

如果要使用jsp自定义标签库技术顺着下列步骤做个demo就能理解个大概

首先在项目下WEB-INF下创建一个tld文件 用于说明这个自定义标签库

然后打开自己曾经导入过的jar包,并且保证该包在现行环境下能够使用

找到包中的WEB-INF文件可以看到一个c.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>JSTL 1.2 core library</description> ?<display-name>JSTL core</display-name> ?<tlib-version>1.2</tlib-version> ?<short-name>c</short-name> ?<uri>http://java.sun.com/jsp/jstl/core</uri>
</taglib>

这样的内容 从头选到uri的位置截取过来 然后在尾部加上</taglib>

申明 由于各个机子上环境可能不一致 此文件不建议直接从我这里copy 

然后可以看一个实例标签

<tag> ???<description> ???????Catches any Throwable that occurs in its body and optionally ???????exposes it. ???</description> ???<name>catch</name> ???<tag-class>org.apache.taglibs.standard.tag.common.core.CatchTag</tag-class> ???<body-content>JSP</body-content> ???<attribute> ???????<description>Name of the exported scoped variable for theexception thrown from a nested action. The type of thescoped variable is the type of the exception thrown. ???????</description> ???????<name>var</name> ???????<required>false</required> ???????<rtexprvalue>false</rtexprvalue> ???</attribute> ?</tag>

这是一个完整的标签中可用的分析

<tag> ????<!-- 标签描述--> ???<description></description> ????<!-- 标签名称--> ???<name>catch</name> ???<!-- -指向的类 也就是具体执行操作的类-> ???<tag-class>org.apache.taglibs.standard.tag.common.core.CatchTag</tag-class> ???<!-- 是否具有标签的格式--> ???<body-content>emty</body-content> ?</tag>

下面是我一个demo Tag.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>there are my tags of tag</description> ?????<tlib-version>1.2</tlib-version> ?????????<!-- 标签的短名称 --> ???<short-name>Tag</short-name> ?????????<!-- 绑定到这个uri上 在页面上要使用 --> ???<uri>http://www.cnblogs.com</uri> ???????????????<tag> ???????<description>view ip of client</description> ?????????<name>ViewIP</name> ?????????<tag-class>web.tag.ViewIpTag</tag-class> ?????????<body-content>empty</body-content> ??????</tag> ???</taglib> ?

然后是类中的代码

package web.tag;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.TagSupport;public class ViewIpTag extends TagSupport { ???@Override ???public int doStartTag() throws JspException { ???????????????HttpServletRequest request =(HttpServletRequest) this.pageContext.getRequest(); ???????????????JspWriter out=this.pageContext.getOut(); ???????????????String ip = request.getRemoteAddr(); ???????????????try { ???????????out.write(ip); ???????} catch (IOException e){ ???????????throw new RuntimeException(e); ???????} ???????????????????????return super.doStartTag(); ???}}

最后是在jsp页面的使用

<%@ taglib uri="http://www.cnblogs.com" prefix="Tag" %>
 你的ip是:<Tag:ViewIP/>

加入这两行代码 然后访问所在页面 就能看的本机Ip

JSP自定义标签的使用简化版

原文地址:https://www.cnblogs.com/ad-zhou/p/9032182.html

知识推荐

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