簡體   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