jsp中的自定义标记没有响应

时间:2014-03-10 10:47:27

标签: java jsp

我在jsp中使用自定义标签时遇到问题。 应用程序运行良好,但标签没有响应。 我的代码是:

这是我的index.jsp,这是有效的。 * 的index.jsp *

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/MyTagLib" prefix="mtl"%>
<!DOCTYPE html>
<html>
   <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
       your code is:<mtl:MyTagClass input="GoodMorning" start="1" end="6"/>
    </body>
</html>

这是Tag库 * MyTagLib.tld *

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>mtl</short-name>
  <uri>/WEB-INF/MyTagLib</uri>
   <tag>
    <name>MyTagClass</name>
    <tag-class>MyPack.MyTagClass</tag-class>
    <body-content>empty</body-content>
    <attribute>
      <name>input</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
     <type>java.lang.String</type>
    </attribute>
    <attribute>
       <name>start</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>java.lang.String</type>
    </attribute>
    <attribute>
      <name>end</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      <type>java.lang.String</type>
    </attribute>
  </tag>
</taglib>

以下是标记处理程序 * MyTagClass.java *

package MyPack;

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

public class MyTagClass extends SimpleTagSupport {
    private String input=null;
    private String start=null;
    private String end=null;
  public MyTagClass(){}
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();

        try {
            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
                out.println("input.substring(start,end)");
           }
} catch (java.io.IOException ex) {
           throw new JspException("Error in MyTagClass tag", ex);
       }
    }

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

    public void setStart(String start) {
        this.start = start;
    }

    public void setEnd(String end) {
        this.end = end;
    }

}

n dis是web.xml * 的web.xml *

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <jsp-config>
        <taglib>
             <taglib-uri>/WEB-INF/MyTagLib</taglib-uri>
            <taglib-location>/WEB-INF/MyTagLib.tld</taglib-location>
        </taglib>
    </jsp-config>
</web-app>

请帮助

1 个答案:

答案 0 :(得分:0)

我建议参考以下链接,更好地了解客户标签实施过程 Demo