【问题标题】:Not able to call custom tag jsp无法调用自定义标签jsp
【发布时间】:2014-01-12 10:22:20
【问题描述】:

我无法调用我在 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 .

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

【问题讨论】:

  • 你在web.xml文件中添加标签声明了吗? Stack Question 会帮助你。

标签: java jsp jsp-tags custom-tags


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 2019-11-16
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多