Lucian Enache
Lucian Enache

Reputation: 2530

Struts 1.1 logic iterator over an ArrayList of objects

I've got an ArrayList of objects that I read from a txt file, RegistrationFormBean(); is the element type

    public List getuserList() throws IOException{

            InputStream input = new FileInputStream("log.txt");

                int i=0;

                String temp[]=new String[5];

                                          tmp= new RegistrationFormBean();
                BufferedReader in = new BufferedReader(new FileReader("log.txt"));

                while ((str = in.readLine()) != null) {

                              StringTokenizer st = new StringTokenizer(str,"\t\t");

                    while(st.hasMoreElements()){
                    temp[i]=st.nextElement().toString();
                                                }

                                                   tmp.setName(temp[0]);
                    tmp.setCognome(temp[1]);
                    tmp.setCitta(temp[4]);
                    tmp.setDdnascita(temp[2]);
                    tmp.setCodfisc(temp[3]);

                    userList.add(tmp);
                                        }
                in.close();

    return userList;
}

this is the jsp page that should iterate over the arraylist returned and print the attributes for each element of the arraylist

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld"   prefix="bean" %>
<jsp:useBean id="userList" scope="request" class="com.webagesolutions.struts.actions.query"/>



<html:html>
<HEAD>
<%@ page 
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet" type="text/css">
<TITLE></TITLE>
</HEAD>

<BODY>
    <table border=1>
        <logic:iterate name="userList" id="nextElement" property="userList">
    <tr>
    <td><bean:write name="nextelement" property="name"/></td>
    <td><bean:write name="nextElement" property="cognome"/></td>
    <td><bean:write name="nextElement" property="ddnascita"/></td>
    <td><bean:write name="nextElement" property="codfisc"/></td>
    <td><bean:write name="nextElement" property="citta"/></td>  
    </tr>
    </logic:iterate>        
    </table>
</BODY>
</html:html>

So I'm wondering how to access the elements of next element, suppose that the property are the element.name element.cognome element.ddnascita (attributes of element), should I define some getters in the class that reads the txt file ? should I reference the element type in the jsp page ?

note that with the code I've wrote I get only the first column printed and it contains the "citta" wich in my tmp is the last attribute of the element, also I've did a debug and the list gets loaded correctly so the problem I suppose is in the jsp page.

Upvotes: 2

Views: 9409

Answers (1)

tusar
tusar

Reputation: 3424

You can use <nested:nest> tag to access nested properties. To use it, you need to add the taglib in the JSP page:

<%@ taglib uri="/tags/struts-nested" prefix="nested"%>

For the above example, you have userList as list of the parent beans, which has some composite properties like cognome and plain String properties like name. Now, start iterating the userList like this:

<nested:nest property="userList">
    <!-- 'name' is just plain String, does not need a nested iteration --> 
    <nested:write property="name"/> 

    <!-- but 'cognome' has some inner properties, we are interested in exploring -->
    <nested:iterate property="cognome">
         <nested:write property="somePropertyOfCognome"/>
    </nested:iterate>

</nested:nest>

Upvotes: 3

Related Questions