属性的未知属性类型(字符串) - 自定义标记 - JSP

时间:2011-07-08 18:55:07

标签: jsp tags

您好我正在尝试创建自定义标记,但我收到了下一个错误:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: /jsp/textInput.jsp(8,0) Unknown attribute type (String) for attribute person.
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:236)
    org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:1162)
    org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:819)
    org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1512)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2393)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2399)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:489)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343)
    org.apache.jasper.compiler.Validator.validate(Validator.java:1737)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:165)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:294)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:281)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.

我的代码如下:

taglib.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
              PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
              "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
    <jsp-version>1.1</jsp-version>
    <tlibversion>1.0</tlibversion>
    <shortname></shortname>
        <tag>
        <name>ShowTag</name>
        <tagclass>com.jjolt.ShowTag</tagclass>
                <attribute>
                   <name>person</name>
                   <required>true</required>
                   <rtexprvalue>true</rtexprvalue>
                </attribute>
    </tag>
</taglib>

Data.jsp

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<body>
<form name="frm" method="get" action="textInput.jsp">
<table align="center">

  <tr>
    <td>Insert your name </td>
    <td><input type="text" name="studentName"></td>
    <td><input type="submit" name="Done" value="Done"></td>
  </tr>

</table>
</form>
</body>
</html>

textInput.jsp

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@ taglib uri="/myTLD" prefix="mytag"%>
<%
String temp=request.getParameter("studentName");
%>


<mytag:ShowTag person= "<%=temp%>"/>

ShowTag.java

package com.jjolt;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;


public class ShowTag extends TagSupport {

        private String person;


    public int doEndTag() throws JspException {


        JspWriter out = pageContext.getOut();
        try {


                        out.println("Hello " + person);


        } catch (Exception e) {
        }
        return super.doEndTag();
    }

        public void setPerson(String person){

            this.person = person;

        }

        public String getPerson(String person){

            return (this.person);

        }
}

我需要某人的帮助!

非常感谢!

1 个答案:

答案 0 :(得分:1)

尝试将<type>java.lang.String</type>添加到person属性。

适合我的完整tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib              
    PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"              
    "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>

    <jsp-version>1.1</jsp-version>
    <tlibversion>1.0</tlibversion>
    <shortname>myTLD</shortname>
    <uri>http://www.fakeaddress.com</uri>
    <tag>
        <name>ShowTag</name>
        <tagclass>com.fake.ShowTag</tagclass>
        <attribute>
            <name>person</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <type>java.lang.String</type>
        </attribute>
    </tag>
</taglib>

使用tld文件的textInput.jsp文件

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@ taglib uri="http://www.fakeaddress.com" prefix="mytag"%>

<%String temp=request.getParameter("studentName");%>

<mytag:ShowTag person= "<%=temp%>"/>