开发者

Custom JSP tag does not render dynamic body content

I have created a new JSP tag (in a Struts 1.2.9/Java 5/Tomcat 5.5 web application), which renders content within the tag body, when the logged-in user has one of the given roles.


<?xml version="1.0" encoding="UTF-8"?>
<%@ attribute name="userRoles" rtexprvalue="false" required="true" description="Comma-separated list of user role names, against which the logged-in user's roles are tested." %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<jsp:directive.tag description="Evaluates the nested body content if the logged-in user has one of the roles given in the userRoles attribute." />
<jsp:directive.tag body-content="tagdependent" />

<c:if test="${sessionScope.userData ne null}">
    <jsp:doBody var="bodyContent" scope="page"/>
    <jsp:scriptlet>
        String userRoles = (String) jspContext.getAttribute("userRoles");
        com.initech.core.db.model.UserData userData = (com.initech.core.db.model.UserData) session.getAttribute("userData");
        if(com.initech.web.struts.action.UserUtils.hasOneOfRolesInCommaSeparatedList(userData, userRoles)){
            String bodyContent = (String) jspContext.getAttribute("bodyContent");
            out.write(bodyContent);
        }
    </jsp:scriptlet>
</c:if>

Example of a file, where the custom tag is used:


<?xml version="1.0" encoding="UTF-8"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
    xmlns:tiles="http://jakarta.apache.org/struts/tags-tiles"
    xmlns:initech-user="urn:jsptagdir:/WEB-INF/tags/initech-user/">
    <html:xhtml />

    <initech-user:userHasRole userRoles="Admin,TPS Manager,">
        abcde
        <tiles:insert name="tiles.components.deletebutton">
            <tiles:put name="deleteClass" value="build"/>
            <tiles:put name="deleteId" value="${sessionScope.buildForm.id}"/>
        开发者_StackOverflow社区</tiles:insert>
    </initech-user:userHasRole>

</jsp:root>

The tag works partially, in the sense that all "normal" content within the tag is rendered (html tags, text). In the example above the text "abcde" is visible on the JSP page, but the content inserted with the nested tiles tags is not visible. To clarify, the following part is not rendered correctly:


<tiles:insert name="tiles.components.deletebutton">
            <tiles:put name="deleteClass" value="build"/>
            <tiles:put name="deleteId" value="${sessionScope.buildForm.id}"/>
        </tiles:insert>

When I look at the HTML source, I see that the content is rendered directly to the JSP page "as is" (i.e. written to the page as if it were normal HTML content), but of course I want the tiles tags to be evaluated and the output of the tags to be written within my own tag. This does apparently not apply only to tiles tags, but also to other dynamic content.

Is it possible to implement a custom tag so that also the content inserted by the tiles tag-library is rendered?


Problem solved by changing the tag directive "body-content" followingly:

<jsp:directive.tag body-content="scriptless" />


In tomcat 6.x jsp 2.1 you must use:

<%@tag body-content="scriptless"  %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜