Passing an iterable object into a nested JSP

611 views Asked by At

This is what I have in my action class code:

private List<PersonDO> listPerson;

public void setListPerson(List<PersonDO> listPerson) {
    this.listPerson = listPerson;
}

public List<PersonDO> getListPerson() {
    return listPerson;
}

public String Details() {
   PersonDO personDO = new PersonDO()
   personDO.setName("Bob");
   personDO.setAge("20");
   
   this.listPerson = new ArrayList<PersonDO >();
   this.listPerson.add(personDO)

   return "success";
}

I know the object is passed into the valuestack when the "Details" action is call as I can iterate "listPerson" in my JSP as such

<s:iterator value="listPerson" status="rowSts" var="mapItem">
   <td style="vertical-align:middle;"><s:property value="#mapItem.name" /></td>
   <td style="vertical-align:middle;"><s:property value="#mapItem.age" /></td>
</s:iterator>

But my problem is I want to pass it into a common nested jsp without directly calling it with the name "listPerson" as this jsp be used through out my application

I have tried the following

main.jsp

<s:include value="sub.jsp">
    <s:param name="listResult" value="listPerson">
</s:include>

sub.jsp

<%pageContext.setAttribute("listResult" , request.getParameter("listResult")); %>
<s:set var="listResult" value="%{#attr.listResult}"/>


 <s:iterator value="listPerson" status="rowSts" var="mapItem">
      <td style="vertical-align:middle;"><s:property value="#mapItem.name" /></td>
      <td style="vertical-align:middle;"><s:property value="#mapItem.age" /></td>
 </s:iterator>

It is not working, I am trying to set a variable "listResult" to reference "listPerson" and iterate it in the sub.jsp.

UPDATE:

To clarify why I am doing this is because I want to reuse sub.jsp twice within main.jsp to avoid duplicate coding with only trivial differences such as a different variable name of a list.

I was able to solve my problem as such

<c:set var="listResult"  value="${listPerson}"  scope="request"/>
<s:include value="sub.jsp">
    <s:param name="shortlist">true</s:param>
</s:include>

<c:set var="listResult"  value="${listCompany}"  scope="request"/>
<s:include value="sub.jsp">
    <s:param name="shortlist">true</s:param>
</s:include>

I think it works because the jsp is generated top down and variable "listResult" is set another value before sub.jsp is rendered again. Correct me if I am wrong and please let me know if there are any issues with this method or if you propose a better solution.

1

There are 1 answers

3
Roman C On

You are trying to create a request scope variable and access it from included page. It might be an alternative to access request scope params.

Note, that parameters are accessed only as request params:

Parameters are passed as request parameters, so use the ${param.ParamName} notation to access them. Do not use the s:property tag to access parameters in included files.

Why not to use value stack and push the parameter that you want to access from iterator within included page?

<s:push value="listPerson">
  <s:include value="sub.jsp">
    <s:param name="shortlist">true</s:param>
  </s:include>
</s:push>

<s:push value="listCompany">
  <s:include value="sub.jsp">
    <s:param name="shortlist">true</s:param>
  </s:include>
</s:push>

If you need both lists as parameters, you can push both (one after another). The value stack is not limited with its size.