jstl代码问题

发布于 2024-10-09 02:20:11 字数 3728 浏览 1 评论 0原文

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type="text/css">
.even {
 background-color: silver;
}
</style>
<title>Registration Page</title>
</head>
<body>

<form:form action="add.htm" commandName="user">
 <table>
  <tr>
   <td>User Name :</td>
   <td><form:input path="name" /></td>
  </tr>
  <tr>
   <td>Password :</td>
   <td><form:password path="password" /></td>
  </tr>
  <tr>
   <td>Gender :</td>
   <td><form:radiobutton path="gender" value="M" label="M" /> <form:radiobutton
    path="gender" value="F" label="F" /></td>
  </tr>
  <tr>
   <td>Country :</td>
   <td><form:select path="country">
    <form:option value="0" label="Select" />
    <form:option value="India" label="India" />
    <form:option value="USA" label="USA" />
    <form:option value="UK" label="UK" />
   </form:select></td>
  </tr>
  <tr>
   <td>About you :</td>
   <td><form:textarea path="aboutYou" /></td>
  </tr>
  <tr>
   <td>Community :</td>
   <td><form:checkbox path="community" value="Spring"
    label="Spring" /> <form:checkbox path="community" value="Hibernate"
    label="Hibernate" /> <form:checkbox path="community" value="Struts"
    label="Struts" /></td>
  </tr>
  <tr>
   <td></td>
   <td><form:checkbox path="mailingList"
    label="Would you like to join our mailinglist?" /></td>
  </tr>
  <tr>
   <td colspan="2"><input type="submit" value="Register"></td>
  </tr>
 </table>
</form:form>
<c:if test="${fn:length(userList) > 0}">
 <table cellpadding="5">
  <tr class="even">
   <th>Name</th>
   <th>Gender</th>
   <th>Country</th>
   <th>About You</th>
  </tr>
  <c:forEach items="${userList}" var="user" varStatus="status">
   <tr class="<c:if test="${status.count % 2 == 0}">even</c:if>">
    <td>${user.name}</td>
    <td>${user.gender}</td>
    <td>${user.country}</td>
    <td>${user.aboutYou}</td>
   </tr>
  </c:forEach>
 </table>
</c:if>
</body>
</html>

当我执行我的jsp页面时,这段代码根本不显示。完整的源代码如下。

<c:if test="${fn:length(userList) > 0}">
    <table cellpadding="5">
        <tr class="even">
            <th>Name</th>
            <th>Gender</th>
            <th>Country</th>
            <th>About You</th>
        </tr>
        <c:forEach items="${userList}" var="user" varStatus="status">
            <tr class="<c:if test="${status.count % 2 == 0}">even</c:if>">
                <td>${user.name}</td>
                <td>${user.gender}</td>
                <td>${user.country}</td>
                <td>${user.aboutYou}</td>
            </tr>
        </c:forEach>
    </table>
</c:if>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type="text/css">
.even {
 background-color: silver;
}
</style>
<title>Registration Page</title>
</head>
<body>

<form:form action="add.htm" commandName="user">
 <table>
  <tr>
   <td>User Name :</td>
   <td><form:input path="name" /></td>
  </tr>
  <tr>
   <td>Password :</td>
   <td><form:password path="password" /></td>
  </tr>
  <tr>
   <td>Gender :</td>
   <td><form:radiobutton path="gender" value="M" label="M" /> <form:radiobutton
    path="gender" value="F" label="F" /></td>
  </tr>
  <tr>
   <td>Country :</td>
   <td><form:select path="country">
    <form:option value="0" label="Select" />
    <form:option value="India" label="India" />
    <form:option value="USA" label="USA" />
    <form:option value="UK" label="UK" />
   </form:select></td>
  </tr>
  <tr>
   <td>About you :</td>
   <td><form:textarea path="aboutYou" /></td>
  </tr>
  <tr>
   <td>Community :</td>
   <td><form:checkbox path="community" value="Spring"
    label="Spring" /> <form:checkbox path="community" value="Hibernate"
    label="Hibernate" /> <form:checkbox path="community" value="Struts"
    label="Struts" /></td>
  </tr>
  <tr>
   <td></td>
   <td><form:checkbox path="mailingList"
    label="Would you like to join our mailinglist?" /></td>
  </tr>
  <tr>
   <td colspan="2"><input type="submit" value="Register"></td>
  </tr>
 </table>
</form:form>
<c:if test="${fn:length(userList) > 0}">
 <table cellpadding="5">
  <tr class="even">
   <th>Name</th>
   <th>Gender</th>
   <th>Country</th>
   <th>About You</th>
  </tr>
  <c:forEach items="${userList}" var="user" varStatus="status">
   <tr class="<c:if test="${status.count % 2 == 0}">even</c:if>">
    <td>${user.name}</td>
    <td>${user.gender}</td>
    <td>${user.country}</td>
    <td>${user.aboutYou}</td>
   </tr>
  </c:forEach>
 </table>
</c:if>
</body>
</html>

When i execute my jsp page, this piece of code does not show up at all. The full source code is below.

<c:if test="${fn:length(userList) > 0}">
    <table cellpadding="5">
        <tr class="even">
            <th>Name</th>
            <th>Gender</th>
            <th>Country</th>
            <th>About You</th>
        </tr>
        <c:forEach items="${userList}" var="user" varStatus="status">
            <tr class="<c:if test="${status.count % 2 == 0}">even</c:if>">
                <td>${user.name}</td>
                <td>${user.gender}</td>
                <td>${user.country}</td>
                <td>${user.aboutYou}</td>
            </tr>
        </c:forEach>
    </table>
</c:if>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花随流水 2024-10-16 02:20:11

当根本没有安装 JSTL 时,可能会发生这种情况。要检查这一点,请右键单击网络浏览器中的页面并选择查看源代码。如果您看到所有 HTML 源代码中的 JSTL 标记均未解析,则意味着它确实未安装。然后,您需要将 JSTL JAR 放入 /WEB-INF/lib 文件夹中。对于 Tomcat 6.x 或更高版本,只需删除 jstl 即可-1.2.jar 文件(并确保您的 web.xml 按照 Servlet 2.5 规范声明)。

但是,如果您在 HTML 源代码中没有看到 JSTL 标记,则意味着条件 ${fn:length(userList) > 0} 始终为 false。您需要通过将非空 userList 作为请求属性来确保情况并非如此。

顺便说一句, ${fn:length(userList) > 0} 也可以简化为 ${not empty userList}

This can happen when JSTL isn't installed at all. To check this, rightclick page in webbrowser and choose View Source. If you see JSTL tags unparsed among all that HTML source, then it means that it's indeed not installed. You need to drop the JSTL JAR(s) in /WEB-INF/lib folder then. For Tomcat 6.x or newer, it suffices to drop just the jstl-1.2.jar file therein (and ensure that your web.xml is declared as per Servlet 2.5 spec).

But if you don't see JSTL tags in the HTML source then it means that the condition ${fn:length(userList) > 0} is always false. You need to ensure that this is not the case by putting a non-empty userList as a request attribute.

By the way, the ${fn:length(userList) > 0} can also be simplified to ${not empty userList}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文