简体   繁体   中英

How do I display action data in a JSP?

I am using an <s:iterator> tag in my JSP to display a List of people objects.

I tried creating ListOfPersons as a List in the action class, along with a getter and setter. I am still unable to display the data--how can I do it?

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

When I tried printing the size of list I am getting zero.

If I understood you right, you are having problem with expressing list using OGNL on JSP pages. You should name fields with YourListName[index].property format. Then OGNL will understand that you have a list called YourListName and its element at index have property with value which is on its input.

Please see example below:

<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>

Short Answer: store the information in the request and access it in the jsp.

Longer Answer:

  1. Create some objects in the servlet (in your case, the action).
  2. Store the objects in some JSP scope ( HttpServletRequest.setAttribute() ).
  3. Forward (dispatch) the request to the JSP page (this is just struts config, you are already doing this).
  4. In the JSP page, reference the variables (perhaps using ac:out tag or just use an EL expression in the JSP page text).

Some code (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.
  }
}

In a JSP:

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

or

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

Once you have the basic concepts down, just set a request attribute with the List in question and access it using the iterator tag in your JSP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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