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

时间:2013-06-23 23:05:48

标签: java jstl

我是JSTL的新手,并且无法准确掌握每个循环的工作原理。但是在我的java bean中,我有一个非常简单的while循环,它通过并抓取对象的属性。当我记录它时,我得到了循环的预期输出。这只是一个看起来像 headerTest,headerMetaTest 的字符串。这是我的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, "");
}

然而,当我尝试在JSTL中访问它时,我什么都没得到:

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

令人费解的部分是我没有错误,没有任何简单的回报。有任何想法吗?真的,真的迷失了这一点,任何帮助都非常感谢。我是一个新手,所以代码示例是我学习的好方法,如果可能的话会很好。

1 个答案:

答案 0 :(得分:1)

JSP页面中有四个范围需要注意。

页面,请求,会话和应用程序。

JSTL标签通常会按顺序查找属性。

页面映射到页面处理过程中分配的属性,这些通常都是相当的 罕见的。

请求是分配给ServletRequest的属性,它们是最常见的 要在页面请求持续时间内使用的属性,然后将其丢弃。

例如

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

会话用于分配给HttpSession的属性。这很有用 用户会话期间经常使用的用户值。

例如

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

应用程序用于分配给ServletContext的属性,这对于有用 在整个应用程序中保持一致且不会更改的值。

例如

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

如果你正在调用一个调度你的jsp的servlet,那么你至少需要它。

request.setAttribute("serviceList",myResourceCollection); 

在servlet处理过程中的某个地方。

如果你在jsp中做所有事情,那么你需要像

这样的东西
<% java code to create collection

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