0

我无法调用我在 JSP 中创建的自定义标记。

标签描述符库的相关部分:

<!-- username tag -->
<tag>
    <name>username</name>
    <tag-class>abc.xyz.UserNameTagHandler</tag-class>
    <body-content>empty</body-content>
</tag> 

Java类是:

public class UserNameTagHandler extends TagSupport {            
    public int doTag() throws JspException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)pageContext.getRequest();
        String username = httpServletRequest.getRemoteUser();
        if (username==null) {
            username = "Guest";
        } else {
            username = username.replaceFirst("@.*$", "");
        }
        JspWriter jspWriter = pageContext.getOut();
        try {
            jspWriter.print(username);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return SKIP_BODY;
    }
}

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/miscHelperToolCusLib.tld" 
prefix="misc"%>
<html>
  <body>
    <div>
        <h1 class="header">Welcome <misc:username />.</h1>
    </div>
  </body>
<html>

我在页面上看到:Welcome .

知道出了什么问题。任何帮助表示赞赏。

4

1 回答 1

1

最后我自己解决了。如果你从那里扩展,TagSupport你必须实现doStartTag()function 而不是doTag().

于 2013-12-24T11:04:08.637 回答