Friends iam using Struts1.2 and iam new to this framework. Iam stuck at one place. I have fetched the records from oracle database and set it in the arraylist of type(DTO object) which is defined in bean class. Now iam facing problem in iterating through the arraylist in the jsp page. It seems my jsp page cannot read the bean property set in the logic:iterate, and the logic iterate is getting skipped completely while execution.

Here is the code:

My DAO class for retrieving data and setting it in the DTO type arraylist

public List<CIFCustomerDTO> getCIFData(String custName, int minRow, int maxRow){
        ResultSet rs=null;
        Statement stmt=null;
        Connection con=null;
        String qry="";
        **List<CIFCustomerDTO> cifCustDTOList = new ArrayList<CIFCustomerDTO>();**
        try{

            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@172.16.100.85:1521:xxx","xyz","xyz");
            stmt = con.createStatement();
            qry = "SELECT * FROM (SELECT ROWNUM R, " +
                    "ANCILLARY_FLAG"
                    "ADMN_ADDRESS_1, " +
                    "......"
                    "FROM CIF_CUSTOMER WHERE CUSTOMER_NAME LIKE " + "'%" + custName + "%') WHERE R > "+minRow+" AND R < " + maxRow + "";
            rs = stmt.executeQuery(qry);
            CIFCustomerDTO cifCustDTO=new CIFCustomerDTO();


            while(rs.next()){

           //Setting only a few attributes instead of all

                cifCustDTO.setAcillaryFlag(rs.getString("ANCILLARY_FLAG"));
                cifCustDTO.setAdmnAddress1(rs.getString("ADMN_ADDRESS_1"));
                //Setting some more collumns in the DTO object.

                **cifCustDTOList.add(cifCustDTO);**
            }catch(Exception e){e.print stack trace}
           }

My Bean class where the getter setter of the Arraylist of DTO type is created.

public class InwardBean{
    List<CIFCustomerDTO> resultList = new ArrayList<CIFCustomerDTO>();

    public List<CIFCustomerDTO> getResultList() {
            return resultList;
        }

    public void setResultList(List<CIFCustomerDTO> resultList) {
            this.resultList = resultList;
        }
}

My Action class where iam calling dao function and setting the values in the arraylist object of DTO present in the bean.

public class PagingCIFAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

        CIFCustomerDAO cifCustDAO = new CIFCustomerDAO(); 
        List<CIFCustomerDTO> cifCustDTOList = null;
        InwardBean bean = new InwardBean();
        cifCustDTOList=cifCustDAO.getCIFData("JAMMU",0 ,6);
        bean.setResultList(cifCustDTOList);

        return mapping.findForward("success");      
    }
}

My Jsp Page where iam displaying the value of arraylist by iterating using logic:iterate

<table border="1" align="center" class="tbl-vista" bordercolor="#b49984" style="border-collapse:collapse">
        <tr>
        **<logic:iterate id="resultList" name="inwardBean" property="resultList">

                <td>
                    <bean:write name="resultList" property="cifId"/>
                </td>
                <td>
                    <bean:write name="resultList" property="customerName"/>
                </td>

        </logic:iterate>**
        </tr>
    </table>

The logic iterate part is getting skipped completely.

Dani AI

Generated

The reason the loop is being bypassed is almost always scope or an empty/undefined collection. The logic:iterate tag looks up the bean named by name in the JSP scopes (page, request, session, application). If inwardBean is never placed into one of those scopes before the forward, name="inwardBean" property="resultList" cannot be resolved and the tag has nothing to iterate. is spot on about exposing the bean to the page. Also confirm the property really returns a non-null List at render time. The Struts 1 tag docs call out that name/property must reference a collection visible to the page, and you can optionally control the lookup with the scope attribute. (exclamationlabs.github.io)

Two quick sanity checks that often fix silent skips or empty rows:

  • Instantiate a new DTO inside the JDBC loop (do not reuse the same instance across rows) and ensure you actually return the populated List.
  • Verify getters match your JSP properties exactly (e.g., getCifId(), getCustomerName()), and use a distinct id (e.g., cust) to avoid confusion with the collection name.

If you prefer JSTL (as @LastMitch hinted) you can render the rows with EL. Example:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<table border="1">
  <c:forEach items="${inwardBean.resultList}" var="cust">
    <tr>
      <td>${cust.cifId}</td>
      <td>${cust.customerName}</td>
    </tr>
  </c:forEach>
</table>

This works as long as inwardBean is in scope and resultList is a collection. See the official JSTL forEach reference for details. (docs.oracle.com)

Recommended Answers

All 2 Replies

Member Avatar for Member #949455

The logic iterate part is getting skipped completely.

There's nothing wrong here:

<logic:iterate property="resultList" name="inwardBean" id="resultList" >  
<tr>  
<td><bean:write name="resultList" property="cifId" /></td>  
<td><bean:write name="resultList" property="customerName" /></td>   
</tr>  
</logic:iterate>  

Now iam facing problem in iterating through the arraylist in the jsp page. It seems my jsp page cannot read the bean property set in the logic:iterate, and the logic iterate is getting skipped completely while execution.

You need this:

http://javarevisited.blogspot.com/2012/10/jstl-foreach-tag-example-in-jsp-looping.html

hai subratbehera,

i think you need to store inwardBean in any of the scope(like request or session) in PagingCIFAction
before forwarding the request to display the values of it.

so that the values will be available for the page where you want to display

try to place this line at line 10 in PagingCIFAction

request.setAttribute("inwardBean",bean); // like this

check it once and let me know

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.