java c:forEach 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12300979/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:22:14  来源:igfitidea点击:

c:forEach not working

javajspweb

提问by gausss

I have a problem with a taglib method c:forEach. I want to get a List of languages from a servlet class and show it on a jsp page with c:forEach. But it is just showing nothing^^ an empty select tag.

我的 taglib 方法 c:forEach 有问题。我想从 servlet 类中获取语言列表,并使用 c:forEach 在 jsp 页面上显示它。但它只是什么都不显示^^ 一个空的选择标签。

The for each loop in the jsp file (i have taglib import and already tried without c:out):

jsp 文件中的 for each 循环(我有 taglib 导入并且已经在没有 c:out 的情况下尝试过):

...
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
...
<c:forEach var="lang" items="${registrationServlet.inputLangs}">
  <option><c:out value="${lang}"></c:out></option>
</c:forEach>

My Servlet Class (it is a servlet because I have to do some form submitting stuff with it too):

我的 Servlet 类(它是一个 servlet,因为我也必须用它来做一些表单提交):

...
// List of languages to choose from
List<String> inputLangs;
...
// Query the languages from the database
public List<String> getInputLangs() {
    try {
        String query = "SELECT DISTINCT Lang FROM country";
        ResultSet result = DbConnection.read(query);
        while (result.next()) {
            inputLangs.add(result.getString("lang"));
        }
    } catch (SQLException e) {
        System.err.println("Couldn't get languages from DB.");
    }
    return inputLangs;
}

What am I doing wrong?^^

我做错了什么?^^

BTW. it works with pure java:

顺便提一句。它适用于纯 Java:

<%
RegistrationServlet reg = new RegistrationServlet();
for (String lang : reg.getInputLangs()) {
%>
  <option><%=lang%></option>
<%
}
%>

But as far as I know that's a no go in jsp files ;)

但据我所知,这在 jsp 文件中是行不通的;)

回答by JB Nizet

${registrationServlet.inputLangs}means:

${registrationServlet.inputLangs}方法:

  • find an attribute named "registrationServlet" in the page scope
  • if not found, find an attribute named "registrationServlet" in the request scope
  • if not found, find an attribute named "registrationServlet" in the session scope
  • if not found, find an attribute named "registrationServlet" in the application scope
  • if found, call getInputLangs()on the found object
  • 在页面范围内找到一个名为“registrationServlet”的属性
  • 如果未找到,则在请求范围内查找名为“registrationServlet”的属性
  • 如果没有找到,在会话作用域中找到一个名为“registrationServlet”的属性
  • 如果没有找到,则在应用范围内找到一个名为“registrationServlet”的属性
  • 如果找到,调用getInputLangs()找到的对象

So, if you haven't stored any instance of RegistrationServletin any scope, this expression will always evaluate to null. If you keep this design, the doGet()(or doPost()) method of your servlet should have the following line:

因此,如果您没有RegistrationServlet在任何范围内存储任何实例,则此表达式将始终计算为null。如果您保持这种设计,您的 servlet的doGet()(or doPost()) 方法应该具有以下行:

request.setAttribute("registrationServlet", this);

But it would be much cleaner to have

但它会更干净

request.setAttribute("inputLangs", getInputLangs());

and, in the JSP:

并且,在 JSP 中:

<c:forEach var="lang" items="${inputLangs}">

回答by Prabash B

Have you got correct JSTL jar files on your class path? In your case JSTL 1.1 JAR file in the /WEB-INF/lib

你的类路径上有正确的 JSTL jar 文件吗?在您的情况下,/WEB-INF/lib 中的 JSTL 1.1 JAR 文件

https://stackoverflow.com/tags/jstl/info

https://stackoverflow.com/tags/jstl/info