导入自定义tagLib而不使用像<%@这样的指令

时间:2012-12-08 18:45:11

标签: java jsp java-ee taglib scriptlet

我在web.xml中的所有JSP中都禁用了scriptlet:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <scripting-invalid>true</scripting-invalid>
    </jsp-property-group>
</jsp-config>

但是我需要导入一些正在使用的自定义tagLib:

<%@ taglib prefix="utils" uri="/tags-utils" %>

如何在不使用scriptlet的情况下导入它? 另外,如何避免使用以下内容?

<%@ page language="java" contentType="text/html; charset=UTF-8" %>

删除scriptlet的决定是避免多个开发人员编写的项目中的scriptlet混乱。

如果在不使用scriptlet的情况下无法更改导入,那么除了<%@ taglib<%@ page之外的其他用途我将如何禁用它?

试图改变

<%@ taglib prefix="s" uri="/struts-tags" %>

<jsp:directive.tagLib prefix="s" uri="struts-tags" />

但是Servlet给我一个错误:

  

[org.apache.catalina.core.ContainerBase。[jboss.web]。[default-host]。[/ portal]。[jsp]](http-localhost-127.0.0.1-80-3)Servlet。 servlet jsp的service()抛出异常:org.apache.jasper.JasperException:/index.jsp(2,18)&lt; jsp:directive.tag指令只能用于标记文件

我还有什么需要做的吗?

2 个答案:

答案 0 :(得分:2)

JSP 2.2规范

JSP 1.3.10.1 JSP语法的EBNF语法

ScriptlessBody ::= ( ( ‘<%--’ JSPCommentBody )
                   | ( ‘<%@’ DirectiveBody )
                   | ( ‘<jsp:directive.’ XMLDirectiveBody )
                   | ( ‘<%!’ <TRANSLATION_ERROR> )
                   | ( ‘<jsp:declaration’ <TRANSLATION_ERROR> )
                   | ( ‘<%=’ <TRANSLATION_ERROR> )
                   | ( ‘<jsp:expression’ <TRANSLATION_ERROR> )
                   | ( ‘<%’ <TRANSLATION_ERROR> )
                   | ( ‘<jsp:scriptlet’ <TRANSLATION_ERROR> )
                   | ( ‘${‘ ELExpressionBody )
                   | ( ‘#{‘ ELExpressionBody )
                   | ( ‘<jsp:text’ XMLTemplateText )
                   | ( ‘<jsp:’ StandardAction )
                   ( ( ‘</’ ExtraClosingTag )
                   | ( ‘<‘ CustomAction CustomActionBody )
                   | TemplateText
                   )*

所以,当scripting-invalid=true时:

非法

<%
<%!
<%=
<jsp:scriptlet
<jsp:declaration
<jsp:expression

<强>法律

<%@
<jsp:directive.
<jsp:

以下是合法的:

 <%@ taglib prefix="utils" uri="/tags-utils" %>

只要“app context uri”+“/ tags-utils”(taglib的上下文相对路径)映射到“taglib absolute uri”。

或者,您可以尝试:

 <%@ taglib prefix="utils" uri="http://www.mycorp/utiltags" %>  // use your absolute taglib URI

OR

 <%@ taglib prefix="utils" uri="uri_path_relative_to_jsp_uri" %>  // no leading "/"

OR

 <%@ taglib prefix="utils" tagdir="/WEB-INF/tags" %> // include subdir if approp

以下是发明的。没有定义jsp:directive.tagLib标记。不要使用。

 <jsp:directive.tagLib prefix="s" uri="struts-tags" />

代替:

 <%@ page language="java" contentType="text/html; charset=UTF-8" %>

<强>尝试:

 <%@ page contentType="text/html; charset=UTF-8" %>  // language is for scriptlets

答案 1 :(得分:-1)

<%@taglib<%@page是指令,而不是scriptlet。

Scriptlet会有<%

或者你可以像这样写

<jsp:directive.taglib uri="uri" prefix="prefixOfTag" />