在 JSP 中启用 JavaServerPages 标准标记库 (JSTL) [英] Enabling JavaServerPages Standard Tag Library (JSTL) in JSP

查看:24
本文介绍了在 JSP 中启用 JavaServerPages 标准标记库 (JSTL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我错过了一些东西 - 从表面上看,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 版本无关.通常,像 WildFly/Payara/TomEE 这样的普通 JEE 服务器已经提供了开箱即用的 JSTL,但是像 Tomcat/Jetty/Undertow 这样的准系统 servletcontainer 没有.对于它们,您需要自己安装 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):

  1. 按照此答案中的说明安装 JSTL:如何安装JSTL?绝对uri:http://java.sun.com/jstl/core 无法解析.

按照此 JSTL 文档(单击任何标签库以查看声明示例).对于 JSTL 核心,如下所示:

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" %>

仅此而已.如果您(仍然)使用 Servlet 2.4,那么您需要下载 jstl.jar standard.jar (它们是JSTL 1.1).其余步骤相同(只需放入classpath并在JSP顶部声明即可).

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). Remaining steps are the same (just put in classpath and declare in top of JSP).

您可能会注意到,一些糟糕的在线教程会建议提取 JAR 文件并将 Web 应用程序的 web.xml 与 TLD 声明混在一起.你永远不应该这样做,这是一个错误的建议,这是由 JSTL 1.0 期间 taglib URI 的变化引起的 ->JSTL 1.1 步骤.不是更新 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 本身只带有 标签.这些不是 JSTL 的一部分.

JSP itself ships with only the <jsp:xxx> tags out of the box. These are not part of JSTL.

这篇关于在 JSP 中启用 JavaServerPages 标准标记库 (JSTL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆