我有一個使用標籤模板的頁面。 我的web.xml非常基礎。JSP標籤+ scriptlet。如何啓用scriptlet?
我只是想在頁面中運行一些代碼。
不,我對標籤或其他選擇不感興趣。我想使用壞習慣scriptlet哈哈。
到目前爲止,我得到這個 「HTTP錯誤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>
,我怎麼用我的標籤中的純小腳本」 ed JSP?
編輯#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>
看到了嗎? ''標籤內的&之間的Scriptlets。這正是我想要實現的。
這是在本地版本?您要部署哪個servlet容器?這聽起來像是一個主web.xml不允許這樣做,但需要更多的信息來幫助! – Codemwnci
它在Google App Engine上(它們使用Jetty AFAIK)dev(本地)服務器。 – Poni