简体   繁体   English

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

[英]error in mapping custom tag in jsp

When i try map custom tag, i am getting Error: 500 with a message File "/customTag" not found 当我尝试映射自定义标记时,我收到Error: 500File "/customTag" not found消息File "/customTag" not found

Here is my attempt: myTag.tld 这是我的尝试: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 page jsp页面

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

When i am trying to access .tld file using file name then everything is fine 当我尝试使用文件名访问.tld文件时,一切都很好

In TLD file: Add short-name and change uri from customTag to /customTag 在TLD文件中:添加短名称并将uri从customTag更改为/ customTag

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

In JSP 在JSP中

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

I am not JEE expert but I just created my own tld based on your example and example from Head First: Servlets and JSP and it seems that: 我不是JEE专家,但我根据你的例子和Head First的例子创建了我自己的tld :Servlets和JSP它似乎:

  • in uri you need to specify name that you will use in <%@taglib uri="..." so if it is <uri>customTag</uri> then you need to use it as <%@taglib uri="customTag" uri你需要指定你将在<%@taglib uri="..."使用的名称,所以如果它是<uri>customTag</uri>那么你需要将它用作<%@taglib uri="customTag"
  • your tag doesn't specify body-content , which for your example can be set to empty 您的标记未指定body-content ,您的示例可以将其设置为empty

So try with this tld which you can place in /WEB-INF/tlds/myTag.tld 所以试试这个你可以放在/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>

For implementation you can use 对于实现,您可以使用

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;
    }
}

and for demonstration you can use your earlier JSP 为了演示,您可以使用早期的JSP

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

Don't forget to republish your project after updating it. 更新后不要忘记重新发布项目。

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

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