简体   繁体   English

Java-使用JSTL的每个循环的基础

[英]Java - Basic for each loop with JSTL

I'm really new to JSTL and having trouble grasping exactly how for each loops work. 我真的是JSTL的新手,很难确切掌握每个循环的工作方式。 But say in my java bean I have a very simple while loop, that goes through and grabs properties of an object. 但是说在我的java bean中,我有一个非常简单的while循环,它可以遍历并获取对象的属性。 I get the expected output from the loop when I log it. 我在登录时从循环中获得了预期的输出。 Which is simply a string that looks something like headerTest, headerMetaTest . 这只是一个字符串,看起来像headerTest,headerMetaTest Here is the code from my java bean: 这是我的Java bean中的代码:

Iterator<Resource> serviceList = null;
serviceList = resource.getChild("header").listChildren();

while(serviceList.hasNext()){
Resource child = serviceList.next();
headerTitle = child.adaptTo(ValueMap.class).get("headerTitle", "");
headerMeta = child.adaptTo(ValueMap.class).get("headerMeta, "");
}

However when I try to access it in the JSTL I'm getting nothing: 但是,当我尝试在JSTL中访问它时,却什么也没得到:

<c:forEach var="child" items="${serviceList}">
    <p>${child.headerTitle}</p>
    <p>${child.headerMeta}</p>
</c:forEach>

The puzzling part is I get no errors, nothing simply returns. 令人困惑的是,我没有错误,没有任何回报。 Any ideas? 有任何想法吗? Really, really lost on this one and any help is greatly appreciated. 真的,真的在这一方面迷失了,任何帮助将不胜感激。 I'm a newb to this so code samples are a good way for me to learn and would be great if possible. 我对此很陌生,因此代码示例是我学习的好方法,并且如果可能的话会很棒。

There are four scopes to be aware of in JSP pages. JSP页面中需要注意四个范围。

page, request, session and application. 页面,请求,会话和应用程序。

JSTL tags will usually look for attributes in that order. JSTL标记通常会按该顺序查找属性。

page maps to attributes assigned during the processing of the page, these are usually quite rare. 页面映射到页面处理期间分配的属性,这些通常很少见。

request is for attributes assigned to the ServletRequest, they are the most common attributes to use as they last for the page request duration, and are then discarded. request是分配给ServletRequest的属性,它们是在页面请求期间持续使用的最常用属性,然后被丢弃。

eg 例如

public void processMyServlet(ServletRequest request, ServletResponse){
    ...
    request.setAttribute("myAttribute",attributeValue);
    ...
}

session is for attributes assigned to the HttpSession. session是分配给HttpSession的属性。 This is useful for user values that are used often during the user session. 这对于在用户会话期间经常使用的用户值很有用。

eg 例如

public void processMyServlet(HttpServletRequest request, HttpServletResponse){
    ...
    request.getSession().setAttribute("myAttribute",attributeValue);
    ...
}

application is for attributes assigned to the ServletContext, this is useful for values that are consistent across the application and do not change. application用于分配给ServletContext的属性,这对于在整个应用程序中保持一致且不变的值很有用。

eg 例如

public void processMyServlet(HttpServletRequest request, HttpServletResponse){
    ...
    request.getServletContext().setAttribute("myAttribute",attributeValue);
    ...
}

If you are calling a servlet that dispatches your jsp then at the very least you will need. 如果您正在调用一个分派jsp的servlet,那么至少您将需要。

request.setAttribute("serviceList",myResourceCollection); 

somewhere during the servlet processing. servlet处理期间的某个位置。

if you are doing everything in jsp then you will need something like 如果您正在用jsp做所有事情,那么您将需要

<% java code to create collection

   request.setAttribute("serviceList",myResourceCollection); 
%>

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

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