2013-09-08 4 views
0

Я играю с этим кодом в течение нескольких часов, и я не могу определить, почему мой тег, кажется, ничего не делает.Основной пользовательский тег не работает

lab08.jspx просто выводит все, что вводится в поле Set Body в lab08J.html, по-видимому игнорируя запись 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(); 
    } 
} 
} 

ответ

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()"); 
    } 
}
Смежные вопросы