【发布时间】:2011-11-02 04:52:34
【问题描述】:
我有一个使用标签模板的页面。 我的 web.xml 非常基础。
我只是想在页面中运行一些代码。
不,我对标签或其他替代方案不感兴趣。我想用坏习惯的scriptlet哈哈。
到目前为止,我收到了这个“HTTP ERROR 500”错误:
Scripting elements ( %!, jsp:declaration, %=, jsp:expression, %, jsp:scriptlet ) are disallowed here.
虽然我的文件看起来像:
/WEB-INF/web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
/WEB-INF/tags/wrapper.tag
<%@tag description="Simple Wrapper Tag" pageEncoding="UTF-8"%>
<%@ attribute name="title" required="true" type="java.lang.String"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>${title}</title>
</head>
<body>
<jsp:doBody />
</body>
</html>
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags"%>
<t:wrapper>
<jsp:attribute name="title">My nice title</jsp:attribute>
<jsp:body>
<h1><%="some code generated text"%></h1>
</jsp:body>
</t:wrapper>
我已尝试修改 web.xml 以显式启用它,如下所示(不起作用):
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>false</scripting-invalid>
</jsp-property-group>
<jsp-property-group>
<url-pattern>*.tag</url-pattern>
<scripting-invalid>false</scripting-invalid>
</jsp-property-group>
</jsp-config>
那么,如何在我的标记 JSP 中使用纯 scriptlet?
编辑#1:
理想的代码应如下所示,在使用我的模板(如上的“包装器”)的页面内:
<%@page import="java.util.Calendar"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags"%>
<t:wrapper>
<jsp:attribute name="title">My nice title</jsp:attribute>
<%
final int day_of_week = Calendar.getInstance().get(
Calendar.DAY_OF_WEEK);
if (day_of_week == Calendar.SATURDAY)
{
%>
<jsp:body>
<h1>Have a nice Saturday (<%=Integer.toString(day_of_week)%>)!</h1>
</jsp:body>
<%
}
else
{
%>
<jsp:body>
<h1>Have a nice rest-of-the-week (<%=Integer.toString(day_of_week)%>)!</h1>
</jsp:body>
<%
}
%>
</t:wrapper>
看到了吗? '' 标签内 & 之间的 Scriptlet。这正是我想要实现的目标。
【问题讨论】:
-
这是本地版本吗?您要部署到哪个 servlet 容器?听起来这是一个主 web.xml 不允许这样做,但需要更多信息来帮助!
-
它在 Google App Engine(他们使用 Jetty AFAIK)开发(本地)服务器上。
标签: java jsp jsp-tags scriptlet