我认为,如果您亲眼看到它实际上可以完全在没有脚本的情况下完成,它会更有帮助。
这是在其他JSTL(只需jstl-1.2.jar
放入/WEB-INF/lib
)核心和函数taglib 的帮助下进行的 1 对 1 重写:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html>
<head>
<title>My Events - <decorator:title /></title>
<link href="${pageContext.request.contextPath}/assets/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="tabs">
<a
${fn:contains(pageContext.request.requestURI, '/events/') ? 'class="selected"' : ''}
href="${pageContext.request.contextPath}/events/Listing.action">Events</a>
<a
${fn:contains(pageContext.request.requestURI, '/people/') ? 'class="selected"' : ''}
href="${pageContext.request.contextPath}/people/Listing.action">People</a>
</div>
<div class="submenu">
<c:if test="${fn:contains(pageContext.request.requestURI, '/events/')}">
<a href="Listing.action">List of Events</a>
|<a href="New.action">New Event</a>
</c:if>
<c:if test="${fn:contains(pageContext.request.requestURI, '/people/')}">
<a href="Listing.action">List of People</a>
|<a href="New.action">New Person</a>
</c:if>
</div>
这是一个更优化的重写,请注意,我曾经c:set
“缓存”表达式结果以供重用,并且我使用 HTML<base>
标记来避免将上下文路径放在每个链接中(只需使网页中的所有相对 URL 都相对于它 - 没有前导削减!):
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<c:set var="isEvents" value="${fn:contains(pageContext.request.requestURI, '/events/')}" />
<c:set var="isPeople" value="${fn:contains(pageContext.request.requestURI, '/people/')}" />
<html>
<head>
<title>My Events - <decorator:title /></title>
<base href="${pageContext.request.contextPath}">
<link href="assets/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="tabs">
<a ${isEvents ? 'class="selected"' : ''} href="events/Listing.action">Events</a>
<a ${isPeople ? 'class="selected"' : ''} href="people/Listing.action">People</a>
</div>
<div class="submenu">
<c:if test="${isEvents}">
<a href="Listing.action">List of Events</a>|<a href="New.action">New Event</a>
</c:if>
<c:if test="${isPeople}">
<a href="Listing.action">List of People</a>|<a href="New.action">New Person</a>
</c:if>
</div>
如果您在应用程序范围内收集所有这些“硬编码”值,例如和链接文本,并在每个 JSTL 下使用以显示选项卡,它实际上可以events
得到people
更多Map
优化<c:forEach>
。
至于您的实际问题,您可以通过在webapp的web.xml
. 它可能有助于发现受监督的小脚本。
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
要了解有关 EL 的更多信息,请查看Java EE 教程第二部分第 5 章。隐式 EL 对象,如此处${pageContext}
所述。要了解有关 JSTL 的更多信息,请查看Java EE 教程第二部分第 7 章。请注意,JSTL 和 EL 是两个不同的东西。JSTL 是一个标准的标记库,EL 只允许以编程方式访问后端数据。虽然它通常用于像 JSTL 这样的标记库中,但它也可以在模板文本中独立使用。