简体   繁体   English

jstl<c:if> 标签在 jsp 文件中不起作用,在 tomcat 7 中出错

[英]jstl <c:if> tags not working in jsp file, getting error in tomcat 7

I have problem using jstl tag <c:if> in jsp file.我在 jsp 文件中使用 jstl 标记<c:if>时遇到问题。 Basically I should make this as 2 questions although they are related.基本上我应该把它作为 2 个问题,尽管它们是相关的。

The first question:第一个问题:

In my WEB-INF/lib, I put a jstl 1.2.jar在我的 WEB-INF/lib 中,我放了一个 jstl 1.2.jar

In my jsp file, I put this <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> .在我的 jsp 文件中,我把这个<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

In my jsp file, I use the <c:if> to do something.在我的 jsp 文件中,我使用<c:if>来做一些事情。 If the condition is true, it will shows some special message如果条件为真,它会显示一些特殊信息

Basically the contents inside <c:if> is not working, because the message is not shown even the condition is true.基本上<c:if>的内容不起作用,因为即使条件为真,消息也不会显示。

But if I changed to use the older namespace, <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> .但是如果我改为使用旧的命名空间, <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> Others are not changed, then the if tag is working, because the message inside the if tag is shown.其他的没有改变,那么 if 标签就起作用了,因为 if 标签内的消息被显示了。

My first question is why I have to use /jstl instead of /jsp/jstl in namespace.我的第一个问题是为什么我必须在命名空间中使用 /jstl 而不是 /jsp/jstl 。 I am using jstl1.2.jar.我正在使用 jstl1.2.jar。 so I am supposed to use the newer uri for 1.2.所以我应该为 1.2 使用较新的 uri。 however, older uri works but not newer uir.但是,较旧的 uri 有效,但较新的 uri 无效。

The second question:第二个问题:

I ignore the first question I have, and just use /jstl as my namespace just because it makes my web app work the way I want.我忽略了我的第一个问题,只是使用 /jstl 作为我的命名空间,因为它使我的 Web 应用程序以我想要的方式工作。 However, when I deploy my web app into tomcat 7.X, it shows exceptions as the following:但是,当我将 Web 应用程序部署到 tomcat 7.X 时,它显示如下异常:

org.apache.jasper.JasperException: /mywebapp.jsp (line: 35, column: 10) According to TLD or attribute directive in tag file, attribute test does not accept any expressions org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42) org.apache.jasper.JasperException: /mywebapp.jsp (line: 35, column: 10) 根据 TLD 或标签文件中的属性指令,属性测试不接受任何表达式 org.apache.jasper.compiler.DefaultErrorHandler.jspError( DefaultErrorHandler.java:42)

in /mywebapp.jsp line:35, column:10, that is < c:if> tag.在/mywebapp.jsp line:35, column:10,即<c:if>标签。

If I change the namespace from <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> to <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>, it can deploy in tomcat successfully but the functionality inside of < c:if> tag is not useful.如果我将名称空间从 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> 更改为 <%@ taglib prefix="c" uri="http:/ /java.sun.com/jsp/jstl/core"%>,可以在tomcat中成功部署,但是<c:if>标签里面的功能没有用。 other codes outside of <c:if> tag work though. <c:if>标签之外的其他代码虽然有效。

So I am very confused and really want to know how to fix this.所以我很困惑,真的很想知道如何解决这个问题。

By the way, I am using servlet 2.5, jsp 2.0, jstl 1.2.顺便说一下,我使用的是 servlet 2.5、jsp 2.0、jstl 1.2。 I did try to upgrade the jsp2.0 to jsp2.1 in order to see if I can fix the first problem, but I have no idea how to upgrade jsp version.我确实尝试将jsp2.0升级到jsp2.1以查看是否可以解决第一个问题,但我不知道如何升级jsp版本。

But if I changed to use the older namespace, <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> .但是如果我改为使用旧的命名空间, <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> others are not changed, then the if tag is working, because the message inside the if tag is shown.其他人未更改,则 if 标记正在工作,因为显示了 if 标记内的消息。

That can happen if you have a standard.jar of JSTL 1.0 in the /WEB-INF/lib .如果您在/WEB-INF/lib有一个 JSTL 1.0 的standard.jar .jar,就会发生这种情况。 Get rid of it.摆脱它。 See also our JSTL wiki page .另请参阅我们的 JSTL 维基页面 I by the way assume that you've untouched Tomcat's and JRE's own /lib folders and have not dropped any JSTL-related JARs in there, or have extracted the JSTL JAR's contents in a careless attempt to solve the problem.顺便说一下,我假设您没有触及 Tomcat 和 JRE 自己的/lib文件夹,并且没有将任何与 JSTL 相关的 JAR 放入其中,或者在粗心大意的尝试中提取了 JSTL JAR 的内容来解决问题。


By the way, I am using servlet 2.5, jsp 2.0, jstl 1.2.顺便说一下,我使用的是 servlet 2.5、jsp 2.0、jstl 1.2。 I did try to upgrade the jsp2.0 to jsp2.1 in order to see if I can fix the first problem, but I have no idea how to upgrade jsp version.我确实尝试将jsp2.0升级到jsp2.1以查看是否可以解决第一个问题,但我不知道如何升级jsp版本。 I am very new to programming.我对编程很陌生。

You should absolutely not provide any Servlet or JSP libraries in /WEB-INF/lib yourself.您绝对不应该自己在/WEB-INF/lib提供任何 Servlet 或 JSP 库。 The servlet container (in your case, Tomcat) already ships with it. servlet 容器(在您的情况下是 Tomcat)已经附带了它。 See also How do I import the javax.servlet API in my Eclipse project?另请参阅如何在我的 Eclipse 项目中导入 javax.servlet API?

You only need to make sure that your web.xml root declaration complies whatever your servlet container supports.您只需要确保您的web.xml根声明符合您的 servlet 容器支持的任何内容。 Tomcat 7 is a Servlet 3.0 compatible container , so your web.xml root declaration should look like this: Tomcat 7 是一个 Servlet 3.0 兼容容器,因此您的web.xml根声明应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <!-- Config here. -->

</web-app>

I ran into this problem today and I just got it to work as described below.我今天遇到了这个问题,我刚刚让它按照下面的描述工作。 I do not know if this solution is fitted for everyone, but it did work for me.我不知道这个解决方案是否适合每个人,但它确实对我有用。 I am a novice in java, so it might not be the best solution.我是java新手,所以它可能不是最好的解决方案。

STL JSP Error Fix - Jar File location for JSTL STL JSP 错误修复 - JSTL 的 Jar 文件位置

1. Change <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> TO: <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 1.将<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>改为:<%@ taglib uri="http://java.sun.com/ jsp/jstl/core" 前缀="c" %>

2. Download below jar files and COPY them to …/WEB-INF/lib folder. 2. 下载以下 jar 文件并将它们复制到.../WEB-INF/lib 文件夹。

https://repo.maven.apache.org/maven2/jakarta/servlet/jsp/jstl/jakarta.servlet.jsp.jstl-api/1.2.7/jakarta.servlet.jsp.jstl-api-1.2.7.jar https://repo.maven.apache.org/maven2/jakarta/servlet/jsp/jstl/jakarta.servlet.jsp.jstl-api/1.2.7/jakarta.servlet.jsp.jstl-api-1.2.7。罐

https://repo.maven.apache.org/maven2/org/glassfish/web/jakarta.servlet.jsp.jstl/1.2.6/jakarta.servlet.jsp.jstl-1.2.6.jar https://repo.maven.apache.org/maven2/org/glassfish/web/jakarta.servlet.jsp.jstl/1.2.6/jakarta.servlet.jsp.jstl-1.2.6.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM