【发布时间】:2011-09-29 16:11:57
【问题描述】:
我创建了一个新的 JSP 标记(在 Struts 1.2.9/Java 5/Tomcat 5.5 Web 应用程序中),当登录用户具有给定角色之一时,它会在标记正文中呈现内容。
<?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>
使用自定义标签的文件示例:
<?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}"/>
</tiles:insert>
</initech-user:userHasRole>
</jsp:root>
标签部分工作,因为标签内的所有“正常”内容都被渲染(html标签,文本)。在上面的示例中,文本“abcde”在 JSP 页面上可见,但使用嵌套的 tile 标签插入的内容不可见。为了澄清,以下部分未正确呈现:
<tiles:insert name="tiles.components.deletebutton">
<tiles:put name="deleteClass" value="build"/>
<tiles:put name="deleteId" value="${sessionScope.buildForm.id}"/>
</tiles:insert>
当我查看 HTML 源代码时,我看到内容“按原样”直接呈现到 JSP 页面(即像普通 HTML 内容一样写入页面),但我当然想要瓷砖标签进行评估并将标签的输出写入我自己的标签中。这显然不仅适用于磁贴标签,还适用于其他动态内容。
是否可以实现自定义标签,以便也呈现由 tile 标签库插入的内容?
【问题讨论】: