JSP tag文件配置 自定义标签简单示例

本文代码转载自

《Servlet、JSP和Spring MVC初学指南》

前文是关于使用tld文件进行自定义标签内容配置的方法,下面对使用tag文件进行
配置的方法展开介绍。(从下面的例子中会看到 tag所给出的表示方法 仅仅是jsp语法对应
的文件分割描述)

先给出一个利用自定义标签给出 字符char转换的实例

tag文件

<%@ attribute name="input" required="true" %>
<%!
    private String encodingHtmlTag(String tag){
        if(tag == null){
            return null;
        }

        int length = tag.length();
        StringBuilder encodedTag = new StringBuilder(2 * length);
        for(int i = 0;i < length; i++){
            char c = tag.charAt(i);
            if (c == '<'){
                encodedTag.append("<");
            }
            else if(c == '>'){
                encodedTag.append(">");
            }
            else if(c == '&'){
                encodedTag.append("&");
            }
            else if(c == '"'){
                encodedTag.append(""");
            }
            else if(c == ' '){
                encodedTag.append(" ");
            }
            else {
                encodedTag.append(c);
            }
        }
        return encodedTag.toString();
    }
%>


<%= encodingHtmlTag(input)%>
<% setInput("<br/>java test");%>
<%= getInput()%>

可以将整个文件看成一个javaBean 其中<%@ attribute name=""%>
用于配置Bean属性 使用了这种定义后“自然地”可以使用setter getter方法(编辑器一般是无法识别的)

相应jsp文件为

<%@ taglib prefix="easy" tagdir="/WEB-INF/tags" %>
<easy:encode input="<br/> means changing line"/>


从而完成文字打印。

下面的例子给出如何在tag文件中包含(include)另一些内容,如普通的html文件
及进行jsp函数输出的tagf文件

被包含的文件内容
included.html
<table>
<tr>
    <td><b>Menu</b></td>
</tr>
<tr>
    <td>CDs</td>
</tr>
<tr>
    <td>DVDs</td>
</tr>
<tr>
    <td>Others</td>
</tr>
</table>

included.tagf
<%
    out.print("Hello from included tagf");
%>

tag文件

This tag file shows the use of the include directive.
The first include directive demostrates how you can include
a static resource called included html.
<br/>
Here is the content of included html.
<%@ include file="included.html"%>
<br/>
<br/>
The second include directive includes another dynamic resource:
included tagf
<br/>
<%@include file="included.tagf" %>

jsp文件

<%@ taglib prefix="easy" tagdir="/WEB-INF/tags" %>
<easy:includeDemoTag/>

下面来看一个使用<jsp:invoke>对于tag中的fragment(片段)进行调用渲染的例子。


相应的进行内容替换的例子见;
http://www.iteedu.com/webtech/j2ee/jspservletdiary/41.php
http://www.cnblogs.com/javaee6/p/3714753.html
后者在使用了tag后将index.jsp完全作为了页面渲染的配置文件,这为多个jsp页面进行
相似形式的页面配置提供了方便。

invokeDemoTag.tag
<%@ attribute name="productDetails" fragment="true" %>
<%@ variable name-given="productName" %>
<%@ variable name-given="description" %>
<%@ variable name-given="price" %>

<%
        jspContext.setAttribute("productName", "Pelesonic DVD Player");
        jspContext.setAttribute("description", "Dolby Digital output coaxial digital-audio jack," +
                " 5600 lines horizontal resolution-image digest viewing");
        jspContext.setAttribute("price", "65");
%>

<jsp:invoke fragment="productDetails"/>
仅仅是初始化相关属性。
其中<%@ variable> 的设定 使得在tag文件中初始化的变量可以被 下面jsp文件调用。

invokeTest.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="easy" tagdir="/WEB-INF/tags" %>
<html>
<head>
    <title>Product Details</title>
</head>
<body>
    <easy:invokeDemo>
        <jsp:attribute name = "productDetails">
            <table width="220" border="1">
                <tr>
                    <td><b>Product Name</b></td>
                    <td>${productName}</td>
                </tr>
                <tr>
                    <td><b>Description</b></td>
                    <td>${description}</td>
                </tr>
                <tr>
                    <td><b>Price</b></td>
                    <td>${price}</td>
                </tr>
            </table>
        </jsp:attribute>
    </easy:invokeDemo>

</body>
</html>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值