java jstl <c:if> 标签在 jsp 文件中不起作用,在 tomcat 7 中出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11573725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jstl <c:if> tags not working in jsp file, getting error in tomcat 7
提问by Joey
I have problem using jstl tag <c:if>
in jsp file. Basically I should make this as 2 questions although they are related.
我<c:if>
在 jsp 文件中使用 jstl 标签时遇到问题。基本上我应该把它作为 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. If the condition is true, it will shows some special message
在我的 jsp 文件中,我使用<c:if>
来做一些事情。如果条件为真,它会显示一些特殊的信息
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"%>
. Others are not changed, then the if tag is working, because the message inside the if tag is shown.
但是如果我改为使用旧的命名空间,<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
. 其他的没有改变,那么 if 标签就起作用了,因为 if 标签内的消息被显示了。
My first question is why I have to use /jstl instead of /jsp/jstl in namespace. I am using jstl1.2.jar. so I am supposed to use the newer uri for 1.2. however, older uri works but not newer uir.
我的第一个问题是为什么我必须在命名空间中使用 /jstl 而不是 /jsp/jstl 。我正在使用 jstl1.2.jar。所以我应该为 1.2 使用较新的 uri。但是,较旧的 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. However, when I deploy my web app into tomcat 7.X, it shows exceptions as the following:
我忽略了我的第一个问题,只是使用 /jstl 作为我的命名空间,因为它使我的 Web 应用程序以我想要的方式工作。但是,当我将 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. other codes outside of <c:if>
tag work though.
如果我将名称空间从 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> 更改为 <%@ taglib prefix="c" uri="http:/ /java.sun.com/jsp/jstl/core"%>,可以在tomcat中成功部署,但是<c:if>标签里面的功能没有用。<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. 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.
顺便说一下,我使用的是 servlet 2.5、jsp 2.0、jstl 1.2。我确实尝试将jsp2.0升级到jsp2.1以查看是否可以解决第一个问题,但我不知道如何升级jsp版本。
回答by BalusC
But if I changed to use the older namespace,
<%@ 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.
但是如果我改为使用旧的命名空间,
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
. 其他人未更改,则 if 标记正在工作,因为显示了 if 标记内的消息。
That can happen if you have a standard.jar
of JSTL 1.0 in the /WEB-INF/lib
. Get rid of it. See also our JSTL wiki page. 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.
如果您standard.jar
在/WEB-INF/lib
. 摆脱它。另请参阅我们的 JSTL 维基页面。顺便说一下,我假设您没有触及 Tomcat 和 JRE 自己的/lib
文件夹,并且没有将任何与 JSTL 相关的 JAR 放入其中,或者为了解决问题而提取了 JSTL JAR 的内容。
By the way, I am using 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. I am very new to programming.
顺便说一下,我使用的是 servlet 2.5、jsp 2.0、jstl 1.2。我确实尝试将jsp2.0升级到jsp2.1以查看是否可以解决第一个问题,但我不知道如何升级jsp版本。我对编程很陌生。
You should absolutely not provide any Servlet or JSP libraries in /WEB-INF/lib
yourself. The servlet container (in your case, Tomcat) already ships with it. See also How do I import the javax.servlet API in my Eclipse project?
您绝对不应该/WEB-INF/lib
自己提供任何 Servlet 或 JSP 库。servlet 容器(在您的情况下是 Tomcat)已经附带了它。另请参阅如何在我的 Eclipse 项目中导入 javax.servlet API?
You only need to make sure that your web.xml
root declaration complies whatever your servlet container supports. Tomcat 7 is a Servlet 3.0 compatible container, so your web.xml
root declaration should look like this:
您只需要确保您的web.xml
根声明符合您的 servlet 容器支持的任何内容。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>