我有一个从 txt 文件中读取的对象的 ArrayList,RegistrationFormBean();是元素类型
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;
}
这是应该迭代返回的数组列表并打印数组列表每个元素的属性的 jsp 页面
<!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>
所以我想知道如何访问下一个元素的元素,假设属性是element.name element.cognome element.ddnascita(元素的属性),我应该在读取txt的类中定义一些getter吗文件 ? 我应该在jsp页面中引用元素类型吗?
请注意,使用我编写的代码,我只打印了第一列,它包含“citta”,它在我的 tmp 中是元素的最后一个属性, 我还进行了调试,并且列表已正确加载,因此我认为问题出在 jsp 页面中。
最佳答案
您可以使用<nested:nest>
标签来访问嵌套属性。
要使用它,您需要在JSP页面中添加taglib:
<%@ taglib uri="/tags/struts-nested" prefix="nested"%>
对于上面的示例,您有 userList
作为父 bean 的列表,它具有一些复合属性,例如 cognome
和纯字符串属性,如 name
。现在,开始迭代userList
像这样:
<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>
关于jakarta-ee - Struts 1.1 对象 ArrayList 上的逻辑迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9633058/