簡體   English   中英

如何在JSP中顯示動作數據?

[英]How do I display action data in a JSP?

我在JSP中使用<s:iterator>標記來顯示人員對象List

我嘗試在動作類ListOfPersonsList一起創建, ListOfPersons其與getter和setter一起創建。 我仍然無法顯示數據-如何處理?

<s:iterator value="ListOfpersons" status="stat">

當我嘗試打印列表的大小時,我得到的是零。

如果我理解正確,那么在JSP頁面上使用OGNL表示列表時會遇到問題。 您應該使用YourListName[index].property格式命名字段。 然后,OGNL將了解您有一個名為YourListName的列表,並且其index元素具有在其輸入中帶有值的property

請參見下面的示例:

<table>
<s:iterator value="ListOfpersons" status="status">
<tr>
   <td><s:textfield name="ListOfpersons[%{#status.index}].firstname"/></td>
   <td><s:textfield name="ListOfpersons[%{#status.index}].lastname"/></td>
   <td><s:textfield name="ListOfpersons[%{#status.index}].age"/></td>
   <td><s:textfield name="ListOfpersons[%{#status.index}].sex"/></td>
</tr>
</s:iterator>
</table>

簡短答案:將信息存儲在請求中,然后在jsp中訪問它。

更長的答案:

  1. 在Servlet中創建一些對象(在您的情況下為操作)。
  2. 將對象存儲在某個JSP范圍( HttpServletRequest.setAttribute() )中。
  3. 將請求轉發(分發)到JSP頁面(這只是struts配置,您已經在執行此操作)。
  4. 在JSP頁面中,引用變量(可能使用ac:out標記,或者僅在JSP頁面文本中使用EL表達式)。

一些代碼(struts 1.x):

class Blah extends Action
{
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
  {
    ... do stuff
    request.setAttribute("Blammy", "Blammy Value");
    ... return some ActionForward.
  }
}

在JSP中:

<span>The value of the Blammy variable is this here thing: ${Blammy}</span>

要么

<span>The value of the Blammy variable is this here thing: <c:out value="${Blammy}"/></span>

一旦掌握了基本概念,就可以為有問題的List設置一個request屬性,並使用JSP中的iterator標記對其進行訪問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM