在JSP中启用JavaServerPages标准标记库(JSTL) [英] Enabling JavaServerPages Standard Tag Library (JSTL) in JSP
问题描述
我感觉好像丢失了一些东西-从表面上看,JSP开箱即用,支持标签,如
I feel like I am missing something - from what it seems, JSP comes out of the box with support for tags, as this question's answer shows (the guy was asking a pure-jsp question and got an answer involving tags). But if I try to run the given code
<c:out value="${myString}"/>
(当然,前面定义了myString
),jsp只是将上述行写入html.
(with myString
defined before, of course), the jsp just writes the above line into the html.
我需要做些额外的事情来启用它吗?
Do I have to do something extra to enable it?
推荐答案
JSTL支持取决于所使用的服务器/servlet容器.有些附带JSTL,有些则没有.这与JSP/Servlet版本无关.通常,普通的JEE服务器(例如WildFly/Payara/TomEE)已经随包装附带了JSTL,但准系统的servlet容器(例如Tomcat/Jetty/Undertow)却没有.对于他们,您需要自己安装JSTL.
JSTL support is dependent on the server/servletcontainer used. Some ships with JSTL, others don't. This is regardless of the JSP/Servlet version. Usually, normal JEE servers such as WildFly/Payara/TomEE already ship with JSTL out the box, but barebones servletcontainers such as Tomcat/Jetty/Undertow don't. For them you'll need to install JSTL yourself.
实际上非常简单(假设您使用的是Servlet 2.5或更高版本):
It's actually pretty simple (assuming you're using Servlet 2.5 or newer):
-
下载 jstl-1.2.jar 并将其复制/复制到webapp的
/WEB-INF/lib
文件夹(这是webapp的默认类路径的一部分)中.使用Maven时,请使用以下坐标:
Download jstl-1.2.jar and put/copy it in webapp's
/WEB-INF/lib
folder (which is part of webapp's default classpath). When you're using Maven, use the below coordinate:
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
Declare the tags in top of JSP as per this JSTL documentation (click any of the taglibs to see the declaration examples). For JSTL core it's the following:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
That's all. If you're (still) on Servlet 2.4, then you'll need to download jstl.jar
and standard.jar
instead (which are part of JSTL 1.1). Remnant of the steps are the same (just put in classpath and declare in top of JSP).
您可能会注意到,一些差劲的在线教程会建议提取JAR文件,并用TLD声明使Webapp的web.xml
混乱.您永远不要这样做,这是一个错误的建议,这是由JSTL 1.0-> JSTL 1.1步骤期间taglib URI的更改引起的.代替在JSP中更新taglib URI,他们决定在web.xml
中重新定义旧的taglib URI,这变成了一个神话.
You may notice that some poor online tutorials would suggest to extract the JAR file and clutter the webapp's web.xml
with the TLD declarations. You should never do that, this is a wrong suggestion which is caused by the change in taglib URI's during the JSTL 1.0 -> JSTL 1.1 step. Instead of updating the taglib URI's in JSP, ones decided to redefine the old taglib URI's in web.xml
and it became a myth.
JSP本身出厂时仅带有<jsp:xxx>
标记.这些不是JSTL的不部分.
JSP itself ships with only the <jsp:xxx>
tags out of the box. These are not part of JSTL.
- What exactly is Java EE?
- How to install JSTL? The absolute uri: http://java.sun.com/jstl/core cannot be resolved
这篇关于在JSP中启用JavaServerPages标准标记库(JSTL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!