【问题标题】:request attribute and pagecontext attribute in JSPJSP中的request属性和pagecontext属性
【发布时间】:2015-11-04 11:52:21
【问题描述】:

我遇到了一个场景,我在 Action 类中设置了我的属性:

request.setAttribute("itemList", itemList); /* valid List item - confirmed */

在 JSP 上,我尝试在 forEach 标记中使用它

<c:forEach items="${itemList}" var="individualItem">

<!-- rest of the code -->

它被评估为空项目。但是我在这个标签之前添加了一个小脚本:

<% List<MyItem> itemList = (List<MyItem>)request.getAttribute("itemList"); // evaluates as my valid item list 
   List<MyItem> itemList = (List<MyItem>)pageContext.getSession().getAttribute("itemList"); // evaluates as NULL
%>

这是否意味着我最好在前端使用 pageContext 或 session 属性而不是 request 属性?还是有规律?

韩国,

【问题讨论】:

    标签: jsp attributes request struts pagecontext


    【解决方案1】:

    规则:

    这取决于您的要求,Session 在整个会话中(即直到用户关闭浏览器或会话超时)保留数据(即变量值),而 Request 在单个请求中保留数据,这包括情况当请求被重定向或分派时。

    在您的示例中, pageContext.getSession().getAttribute("itemList") 返回 null 因为您在请求中设置了属性而不是会话,但是为什么 EL 代码返回 null,我不知道为什么。 ....可能是你在你的jsp中定义了一个名为'itemList'的局部变量,它是null,所以它首先读取它,检查answer here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 2014-06-22
      • 1970-01-01
      • 2016-06-22
      • 2011-04-04
      • 2016-03-04
      • 1970-01-01
      相关资源
      最近更新 更多