JSP编写定制标签之管理标签主体

xiaoxiao2021-02-28  139

java代码

public class SelectElementTag extends SimpleTagSupport{ private String[] Num = {"1","2","3"}; public void doTag() throws IOException, JspException { JspContext jspContext = getJspContext(); JspWriter out = jspContext.getOut(); out.print("<select>\n"); for(int i=0; i<3; i++){ getJspContext().setAttribute("value", Num[i]); getJspContext().setAttribute("text", Num[i]); //通过调用invoke()方法执行片段(标签主体),并将所有输出内容导到指定的Writer //值为null时,输出的结果将会被导到与该片段相关的JspContext的getOut方法所返回的JspWriter getJspBody().invoke(null); } out.print("</select>\n"); } }

注册标签:

<tag> <name>select</name> <tag-class>tag.SelectElementTag</tag-class> <body-content>scriptless</body-content> </tag

关于各文件的详细介绍 请看前两篇博文

测试

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="/WEB-INF/mytags.tld" prefix="easy" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>First Tag</title> </head> <body> <easy:select> <option value="${value }">${text }</option> </easy:select> </body> </html>

结果:

转载请注明原文地址: https://www.6miu.com/read-72457.html

最新回复(0)