你好。
决定第一次尝试 custon taglib
java类
包 EN.Tags.Examples; 导入 javax.servlet.jsp.tagext.*; 导入 javax.servlet.jsp.*; 导入java.io.*; 公共类 CustomAttribute 扩展 SimpleTagSupport { 私人字符串消息; public void setMessage(String msg) { this.message = 味精; } StringWriter sw = new StringWriter(); 公共无效 doTag() 抛出 JspException、IOException { 如果(消息!=空){ /* 使用来自属性的消息 */ JspWriter out = getJspContext().getOut(); out.println("第一个自定义标签:"+ message ); } 别的 { /* 使用正文中的消息 */ getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); } } }
custom_tag_attribute.tld 位于 WEB-INF/jstl/custom_tag_attribute.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">
<description>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>CustomAttribute</short-name>
<tag>
<name>Hello</name>
<tag-class>RU.Tags.Examples.CustomAttribute</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>message</name>
</attribute>
</tag>
</taglib>
在 JSP 中调用自定义标签库
examples.jsp 位于项目的根文件夹newproject/examples.jsp
<%@ taglib uri="/WEB-INF/jstl/custom_tag_attribute.tld" prefix="CustomAttribute" %>
<CustomAttribute:Hello message="This is custom tag" />
一切都会好起来的,但是如果项目在根文件夹中工作,则此选项有效,例如 localhost: 8000/examples.jsp
如果以这种方式访问 http://localhost/newproject/examples.jsp ,则会出现错误
HTTP 状态 500 - 无法找到 URI 的标签库“CustomAttribute”:/WEB-INF/jstl/custom_tag_attribute.tld 类型异常报告 消息无法为 URI 找到标签库“CustomAttribute”:/WEB-INF/jstl/custom_tag_attribute.tld 说明 服务器遇到内部错误,无法完成此请求。 例外 org.apache.jasper.JasperException:无法找到 URI 的标签库“CustomAttribute”:/WEB-INF/jstl/custom_tag_attribute.tld
如何消除这个错误?
您可能需要在应用程序的 web.xml 中配置自定义标记的路径。
https://docs.oracle.com/cd/E11035_01/wls100/taglib/quickstart.html