JSTL基础 SimpleTagSupport 自定义标签 标签体scriptless
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSTL基础 SimpleTagSupport 自定义标签 标签体scriptless相关的知识,希望对你有一定的参考价值。
礼悟:
好好学习合思考,尊师重道存感恩。叶见寻根三返一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼强身心,诚劝且行且珍惜。
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{ @Override public void doTag() throws JspException, IOException { // TODO Auto-generated method stub Writer 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的主要内容,如果未能解决你的问题,请参考以下文章
JSTL基础 SimpleTagSupport 自定义标签 标签体scriptless