分享web开发知识

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

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

JSTL基础 SimpleTagSupport 自定义标签 标签体scriptless

发布时间:2023-09-06 01:29责任编辑:顾先生关键词:script

礼悟:
     好好学习合思考,尊师重道存感恩。叶见寻根三返一,江河湖海同一体。
          虚怀若谷良心主,愿行无悔给最苦。读书锻炼强身心,诚劝且行且珍惜。




       javaEE:7           
       javaSE:1.8
          JSTL:1.2.2  
      server:tomcat 8.5
    browser:Chrome/Firefox
             os:windows7 x64
            ide:MyEclipse 2017

 

项目结构

jizuiku-tags.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"> ???<tlib-version>1.0</tlib-version> ?<short-name>jzk</short-name> ?<uri>http://www.jizuiku.com/tags/1.0</uri> ???????<!-- 配置标签 --> ?<tag> ?<name>MyDecorationTag</name><!-- 指定当前标签的名称 --> ?<tag-class>com.jizuiku.tag.MyDecorationSimpleTagSupport</tag-class><!-- 当前标签的处理类 --> ?<body-content>scriptless</body-content><!-- 指定标签体的类型 EL表达式 --> ?</tag> ?</taglib>

MyDecorationSimpleTagSupport.java

package com.jizuiku.tag;import java.io.IOException;import java.io.Writer;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;/** * 装饰一下标签中的内容,来个框框呀什么的 * ?* @author 博客园-给最苦 ?* @version V2017.12.04 */public class MyDecorationSimpleTagSupport extends SimpleTagSupport{@Overridepublic void doTag() throws JspException, IOException {// TODO Auto-generated method stubWriter out = this.getJspContext().getOut();// 这个从前是这样写的,输出的效果也对// out.write("<div class=‘commonDiv‘>");// 但是 给最苦 感觉 class的值还是用双引号包起来好// 所以,使用了 转义。。。还真成了// 这只是一个小插曲,嘿嘿。。。编程的乐趣out.write("<div class=\"commonDiv\">");// 将EL表达式的执行结果给out流this.getJspBody().invoke(out);out.write("</div>");}}

MyJsp.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@ taglib prefix="j" uri="/WEB-INF/tlds/jizuiku-tags.tld"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP ‘MyJsp.jsp‘ starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><style type="text/css">.commonDiv {border: 3px dotted #ff006e; /*上下左右的边框线样式相同*/}</style></head><body><!-- 普通字符串 --><j:MyDecorationTag>hello world</j:MyDecorationTag><br><!-- EL表达式 --><c:set var="pageContext_name" value="pageContext_给最苦" scope="page"></c:set><j:MyDecorationTag>${pageScope.pageContext_name }</j:MyDecorationTag></body></html>

浏览器查看

该页面的源代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="http://localhost:8081/Day13/"><title>My JSP ‘MyJsp.jsp‘ starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><style type="text/css">.commonDiv {border: 3px dotted #ff006e; /*上下左右的边框线样式相同*/}</style><script>"undefined"==typeof CODE_LIVE&&(!function(e){var t={nonSecure:"56731",secure:"56736"},c={nonSecure:"http://",secure:"https://"},r={nonSecure:"127.0.0.1",secure:"gapdebug.local.genuitec.com"},n="https:"===window.location.protocol?"secure":"nonSecure";script=e.createElement("script"),script.type="text/javascript",script.async=!0,script.src=c[n]+r[n]+":"+t[n]+"/codelive-assets/bundle.js",e.getElementsByTagName("head")[0].appendChild(script)}(document),CODE_LIVE=!0);</script></head><body data-genuitec-lp-enabled="false" data-genuitec-file-id="wc1-0" data-genuitec-path="/Day13/WebRoot/MyJsp.jsp"><!-- 普通字符串 --><div class="commonDiv">hello world</div><br data-genuitec-lp-enabled="false" data-genuitec-file-id="wc1-0" data-genuitec-path="/Day13/WebRoot/MyJsp.jsp"><!-- EL表达式 --><div class="commonDiv">pageContext_给最苦</div></body></html>

学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。

JSTL基础 SimpleTagSupport 自定义标签 标签体scriptless

原文地址:http://www.cnblogs.com/jizuiku/p/7994002.html

知识推荐

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