Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

Enabling JavaServerPages Standard Tag Library (JSTL) in 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}"/>

(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?

question from:https://stackoverflow.com/questions/65843532/jstl-cif-cif-condition-is-executed-for-both-true-and-false

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

It's actually pretty simple (assuming you're using Servlet 2.5 or newer):

  1. Download jstl-1.2.jar and put/copy it in webapp's runtime classpath by placing in /WEB-INF/lib folder. When you're using Maven, use the below coordinate to let Maven automatically do it during build:

     <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>jstl</artifactId>
         <version>1.2</version>
     </dependency>
    
  2. 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). Remaining steps are the same (just put in classpath and declare in top of JSP).

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 itself ships with only the <jsp:xxx> tags out of the box. These are not part of JSTL.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...