Creating a simple custom tag to show or hide body
Im trying to create my own custom tag so i can display or hide the content between the tag depending on an attribute set.
<开发者_Python百科;mytag:isUserInRole role="admin">Show admin specific text</mytag:isUserInRole>`
Basicly i want to show the text between the tags if the logged in user has role==admin
.
I thought it would be easy, but so far i've had no luck. If i in my tld-file set <body-content>empty</body-content>
I get this exception:
"According to TLD, tag auth:isUserInRole must be empty, but is not"
If i set <body-content />
i get this one:
Unable to compile class for JSP - Caused by: java.lang.NullPointerException at org.apache.jasper.compiler.Parser.parseBody(Parser.java:1857)
Can anyone see what im doing wrong?
Her is my tld-files relevant tag:
<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>
And here is my AuthorizationTag doStartTag-method:
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;
}
}
Try this: <bodycontent>jsp</bodycontent>
, also note the difference between <body-content>
and <bodycontent>
As I know there is a taglib uri="http://www.springframework.org/security/tags"
which does exactly you are trying to achieve - it has <authorize>
tag. If you don't want use it, you could investigate the source code, e.g. org.springframework.faces.security.FaceletsAuthorizeTagHandler.
精彩评论