基本自定义标记不起作用

时间:2013-09-08 02:13:04

标签: jsp java-ee taglib custom-tags

我已经玩了几个小时的代码了,我无法确定为什么我的标签似乎什么都不做。

lab08.jspx只输出在lab08J.html的Set Body字段中输入的内容,似乎完全忽略了friend条目。 代码如下。

lab08J.html

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.w3.org/1999/xhtml
                      http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd">
  <head><title>lab08J.html</title><head>
  <body>
    <form action="lab08J.jspx">
        Set Friend:<input type="text" name="friend"/>
        <p>Set Body:<input type="text" name="body"/>
        <input type="submit"/>
    </form>
  </body>
</html>

lab08J.jspx

<html xmlns:j="http://ets.com/tags/j"
  xmlns:jsp="http://java.sun.com/JSP/Page">
  <jsp:output omit-xml-declaration="true" />
  <jsp:directive.page contentType="text/html" />
  <head><title>lab08.jspx</title></head>
  <body><j:myTag friend="${param.friend}">${param.body}</j:myTag>
  </body>
</html>

的web.xml

<?xml version="1.0" ?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
</web-app>

myTags.tld

<taglib xmlns="http://java.sun.com/xml/ns/j2ee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 
  version="2.0">

  <tlib-version>1.0</tlib-version>
  <short-name>lab08Jtaglib</short-name>
  <uri>http://ets.com/tags/j</uri>
  <tag>
    <name>myTag</name>
    <tag-class>webcert.ch08.lab08J.MyTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
      <name>friend</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
</taglib>

MyTag.java

package webcert.ch08.lab08J;
import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.IterationTag;


public class MyTag extends BodyTagSupport{

private PageContext pageContext;
private BodyTagSupport parent;
private String friend;
private BodyContent bc = getBodyContent();

public void setPageContext(PageContext pageContext){
    super.setPageContext(pageContext);
    log("setPageContext()");
}
public void setParent(BodyTagSupport parent){
    super.setParent(parent);
    log("setParent()");
}
public void setFriend(String friend)throws Exception{
    this.friend = friend;
    log("setFriend() as " + friend);
    JspWriter out = pageContext.getOut();
    out.write("setFriend as " + friend);
}
public int doStartTag(){
    log("doStartTag()");
    return BodyTagSupport.EVAL_BODY_BUFFERED;

}
public void setBodyContent(BodyContent bc){
    super.setBodyContent(bc);
    log("setBodyContent()");
}
public void doInitBody(){
    log("doInitBody()");
}
public int doAfterBody(){
    log("doAfterBody()");
    try{
        bc.write("Meet my friend " + friend);
    }
    catch(IOException ioe){
        ioe.printStackTrace();
    }
    return IterationTag.EVAL_BODY_AGAIN;
}
public int doEndTag(){
    log("doEndTag()");
    try{
        bc.writeOut(bc.getEnclosingWriter());
    }
    catch(IOException ioe){
        ioe.printStackTrace();
    }
    return BodyTagSupport.EVAL_PAGE;
}
public void release(){
    System.out.println("release()");
    super.release();
}
protected void log(String s){
    try {
        bc.write(s);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

1 个答案:

答案 0 :(得分:0)

你的MyTag.java有些问题。我试图解决它,它应该工作。

public class MyTag extends BodyTagSupport {
    private String friend;

    @Override
    public int doAfterBody() throws JspException {
        log("doAfterBody()");

        try {
            BodyContent bodycontent = getBodyContent();
            String body = bodycontent.getString();
            JspWriter out = bodycontent.getEnclosingWriter();
            if (body != null) {
                out.print(body.toUpperCase());
            }
        } catch (IOException ioe) {
            throw new JspException("Error:" + ioe.getMessage());
        }

        log("Meet my friend " + friend);
        return BodyTagSupport.SKIP_BODY;
    }

    @Override
    public int doEndTag() {
        log("doEndTag()");
        return BodyTagSupport.EVAL_PAGE;
    }

    @Override
    public void doInitBody() {
        log("doInitBody()");
    }

    @Override
    public int doStartTag() {
        log("doStartTag()");
        return BodyTagSupport.EVAL_BODY_BUFFERED;

    }

    protected void log(String s) {
        System.out.println(s);
    }

    @Override
    public void release() {
        log("release()");
        super.release();
    }

    @Override
    public void setBodyContent(BodyContent bc) {
        super.setBodyContent(bc);
        log("setBodyContent()");
    }

    public void setFriend(String friend) throws Exception {
        this.friend = friend;
        log("setFriend() as " + friend);
        JspWriter out = pageContext.getOut();
        out.write("setFriend as " + friend);
    }

    @Override
    public void setPageContext(PageContext pageContext) {
        super.setPageContext(pageContext);
        log("setPageContext()");
    }

    public void setParent(BodyTagSupport parent) {
        super.setParent(parent);
        log("setParent()");
    }
}