繁体   English   中英

在jsp中映射自定义标记时出错

[英]error in mapping custom tag in jsp

当我尝试映射自定义标记时,我收到Error: 500File "/customTag" not found消息File "/customTag" not found

这是我的尝试:myTag.tld

<taglib version="2.0" xmlns="http://java.sun.com/xml/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    >
    <uri>customTag</uri>
    <tlib-version>1.0</tlib-version>
    <tag>
        <name>multiplier</name>
        <tag-class>myPack.MultiplierTag</tag-class>
        <attribute>
                <name>input</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

</taglib>

jsp页面

<%@taglib uri="/customTag" prefix="operator"%>
<%
    String input = request.getParameter("input");
%>
<operator:multiplier input="<%=input%>"></operator:multiplier>

当我尝试使用文件名访问.tld文件时,一切都很好

在TLD文件中:添加短名称并将uri从customTag更改为/ customTag

<short-name>operator</short-name>
<uri>/customTag</uri>

在JSP中

<@ taglib prefix =“operator”uri =“/ customTag”/>

我不是JEE专家,但我根据你的例子和Head First的例子创建了我自己的tld :Servlets和JSP它似乎:

  • uri你需要指定你将在<%@taglib uri="..."使用的名称,所以如果它是<uri>customTag</uri>那么你需要将它用作<%@taglib uri="customTag"
  • 您的标记未指定body-content ,您的示例可以将其设置为empty

所以试试这个你可以放在/WEB-INF/tlds/myTag.tld tld

<taglib version="2.0" xmlns="http://java.sun.com/xml/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
    <uri>customTag</uri>
    <tlib-version>1.0</tlib-version>
    <tag>
        <name>multiplier</name>
        <tag-class>myPack.MultiplierTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
                <name>input</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

对于实现,您可以使用

package myPack;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class MultiplierTag extends SimpleTagSupport {
    private String input;

    public void doTag() throws JspException, IOException {
        getJspContext().getOut().write("Hello " + input + " <br>");
    }

    public void setInput(String input) {
        this.input = input;
    }
}

为了演示,您可以使用早期的JSP

<%@taglib uri="customTag" prefix="operator"%>
<%
    String input = "World";//request.getParameter("input");
%>
<operator:multiplier input="<%=input%>"></operator:multiplier>

更新后不要忘记重新发布项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM