logic:iterate 里面的id和name寫什么,對應哪里
<table bgcolor="#ff80ff" border="1"><tr>
<th>工號</th>
<th>姓名</th>
<th>工作</th>
<th>年限</th>
<th>工資</th>
</tr>
<logic:iterate id="emp" name="list"> <td><bean:write name="emp" property="Empno"/></td>
<td><bean:write name="emp" property="Ename"/></td>
<td><bean:write name="emp" property="Job"/></td>
<td><bean:write name="emp" property="Hiredate"/></td>
<td><bean:write name="emp" property="Sal"/></td>
</logic:iterate>
</table>
</body>
--------------------------------------------------------------------------------------------------
public class QueryAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
EmpDao empDao = new EmpDao();
ArrayList list = empDao.empName();
//System.out.println(list);
request.setAttribute("list", list);
return new ActionForward("/display.jsp");
}
}
- <logic:iterate id="emp" name="list">
name屬性:是你放置Bean的集合,在你的這段代碼中:
- public class QueryAction extends Action {
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- EmpDao empDao = new EmpDao();
- ArrayList list = empDao.empName();
- //System.out.println(list);
- request.setAttribute("list", list);
- return new ActionForward("/display.jsp");
- }
- }
- request.setAttribute("list", list);
- request.setAttribute("myList", list);
logic:iterator中的id屬性,其實是你要從你的集合中取出的Bean的名字,這個名字是任意起的,主要是下面的<bean:write name="emp" property="Ename"/>標簽中的name屬性要跟logic:iterator中的id屬性對應上.其實這里的是這樣的,首先<logic:iterator>標簽會把name屬性值為list的集合里的bean逐一取出來,每取出來一個,就把他存到名為id屬性的值的pageContext范圍內,一看代碼你就能明白:
主要代碼:
- //首先取出List,getAttribute()方法中的值就是<logic:iterator>標簽的name
- //值
- List list=request.getAttribute("list");
- //然后把取出來的bean存入pageContext范圍內,對應的名字就是id的值
- pageContext.setAttribute("emp",bean);
- //<bean:write>標簽其實就是利用反射把Bean從相應的范圍內取出
- pageContext.getAttribute("emp");
posted on 2009-04-13 11:53 MichaelLee 閱讀(1788) 評論(0) 編輯 收藏 所屬分類: Struts