💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 如何访问自定义标签的主体 > 原文: [https://beginnersbook.com/2014/01/how-to-access-body-of-custom-tags-in-jsp-tutorial/](https://beginnersbook.com/2014/01/how-to-access-body-of-custom-tags-in-jsp-tutorial/) 在上一个教程中,我们学习了[如何在 JSP](https://beginnersbook.com/2014/01/jsp-custom-tags-with-example-jsp-tutorial/) 中创建和使用自定义标签。在本教程中,我们将了解如何访问自定义标签的主体。对于例如如果我们的自定义标签是`xyz`,那么我们将学习访问`<prefix: xyz>`和`</prefix:xyz>`之间的内容 ```html <prefix: xyz> Body of custom tag: This is what we will access in the below example </prefix:xyz> ``` **示例:** 在此示例或自定义标签中,将`String`附加到其自己的主体并显示结果。 **标签处理程序类**:`Details.java` ```html package beginnersbook.com; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class Details extends SimpleTagSupport { //StringWriter object StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException { getJspBody().invoke(sw); JspWriter out = getJspContext().getOut(); out.println(sw.toString()+"Appended Custom Tag Message"); } } ``` **TLD 文件**:`message.tld`,请记住将此文件放在`WEB-INF`文件夹中。 ```html <taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>My Custom Tag: MyMsg</short-name> <tag> <name>MyMsg</name> <tag-class>beginnersbook.com.Details</tag-class> <body-content>scriptless</body-content> </tag> </taglib> ``` **JSP 页面**:`index.jsp` ```html <%@ taglib prefix="myprefix" uri="WEB-INF/message.tld"%> <html> <head> <title>Accessing Custom Tag Body Example</title> </head> <body> <myprefix:MyMsg> Test String </myprefix:MyMsg> </body> </html> ``` **输出:** ```html Test String Appended Custom Tag Message ```