本文代码转载自《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>
JSP tag文件配置 自定义标签简单示例
最新推荐文章于 2023-06-02 22:35:36 发布