我正在尝试创建自己的自定义标签,以便我可以根据属性集显示或隐藏标签之间的内容。
<mytag:isUserInRole role="admin">Show admin specific text</mytag:isUserInRole>`
基本上,如果登录用户有role==admin
,我想显示标签之间的文本。
我以为这很容易,但到目前为止我还没有运气。如果我在我的 tld 文件集中 <body-content>empty</body-content>
我得到这个异常:
“根据 TLD,标签 auth:isUserInRole 必须为空,但事实并非如此”
如果我设置<body-content />
我明白了:
无法编译 JSP 类 - 原因是:java.lang.NullPointerException 在 org.apache.jasper.compiler.Parser.parseBody(Parser.java:1857)
有人能看出我做错了什么吗?
她是我的 tld 文件相关标签:
<tag>
<name>isUserInRole</name>
<tag-class>no.mycompany.tag.AuthorizationTag</tag-class>
<body-content />
<attribute>
<name>role</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
这是我的 AuthorizationTag doStartTag 方法:
public int doStartTag() throws JspException{
System.out.println("Role"+role);
//Check that role is given and securitycontext is not null
if(role==null|| SecurityContextHolder.getContext()==null || SecurityContextHolder.getContext().getAuthentication()==null){
return Tag.SKIP_BODY;
}
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if(hasPermission(auth)){
return Tag.EVAL_BODY_INCLUDE;
} else {
return Tag.SKIP_BODY;
}
}
最佳答案
试试这个:<bodycontent>jsp</bodycontent>
,还要注意 <body-content>
之间的区别和<bodycontent>
关于java - 创建一个简单的自定义标签来显示或隐藏正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7245218/