补充一下,针对下面的jsp程序,能否解释一下iterate的应用:
<!-- \u8D2D\u4E66\u7F51\u7AD9\u7684\u521D\u59CB\u754C\u9762 -->
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<head>
<title>
<bean:message key="initial.title"/>
</title>
</head>
<body>
<!--input the book name that looked up by logon user -->
<html:form action="/shopping/initialBookAction" focus="form1">
<table>
<tr>
<th>
<bean:message key="initial.lookup" />
</th>
<td>
<html:text property="bookname" size="16" maxlength="16"/>
</td>
<td>
<html:submit property="submit" value="Submit"/>
<html:submit property="submittotallist" value="book list"/>
</td>
</tr>
</table>
<!--display all the book that display
it includes two situations:1,all books
2,the books that looked up
-->
<%
String button1 = (String)request.getParameter("submit");
String button2 = (String)request.getParameter("submittotallist");
if(button1!=null||button2!=null){
%>
<table border="1">
<tr>
<td><bean:message key="initial.bookname"/></td>
<td><bean:message key="initial.author"/></td>
</tr>
<%
Iterator booklist =(Iterator)session.getAttribute("bookList");
if(booklist==null)
System.out.println("booklist is null");
else
System.out.println("booklist is NOT null");
%>
<% for(;booklist.hasNext();){
shopping.util.Book bookTemp = (shopping.util.Book)booklist.next();
%>
<tr>
<td>
<A href="/shopping/shopping/lookupBookAction.do?lookupbookname=<%=bookTemp.getBookName()%>"><%=bookTemp.getBookName()%></A>
</td>
<td><%=bookTemp.getAuthor()%></td>
</tr>
<%}%>
<logic:iterate id="book" collection="<%=booklist%>" type="shopping.util.Book" offset="0" length="2">
<tr>
<td><%=book.getBookName()%></td>
<td><%=book.getAuthor()%></td>
<td>waiting...</td>
</tr>
</logic:iterate>
</table>
<%}%>
</html:form>
</body></html:html>
<!-- \u8D2D\u4E66\u7F51\u7AD9\u7684\u521D\u59CB\u754C\u9762 -->
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<head>
<title>
<bean:message key="initial.title"/>
</title>
</head>
<body>
<!--input the book name that looked up by logon user -->
<html:form action="/shopping/initialBookAction" focus="form1">
<table>
<tr>
<th>
<bean:message key="initial.lookup" />
</th>
<td>
<html:text property="bookname" size="16" maxlength="16"/>
</td>
<td>
<html:submit property="submit" value="Submit"/>
<html:submit property="submittotallist" value="book list"/>
</td>
</tr>
</table>
<!--display all the book that display
it includes two situations:1,all books
2,the books that looked up
-->
<%
String button1 = (String)request.getParameter("submit");
String button2 = (String)request.getParameter("submittotallist");
if(button1!=null||button2!=null){
%>
<table border="1">
<tr>
<td><bean:message key="initial.bookname"/></td>
<td><bean:message key="initial.author"/></td>
</tr>
<%
Iterator booklist =(Iterator)session.getAttribute("bookList");
if(booklist==null)
System.out.println("booklist is null");
else
System.out.println("booklist is NOT null");
%>
<% for(;booklist.hasNext();){
shopping.util.Book bookTemp = (shopping.util.Book)booklist.next();
%>
<tr>
<td>
<A href="/shopping/shopping/lookupBookAction.do?lookupbookname=<%=bookTemp.getBookName()%>"><%=bookTemp.getBookName()%></A>
</td>
<td><%=bookTemp.getAuthor()%></td>
</tr>
<%}%>
<logic:iterate id="book" collection="<%=booklist%>" type="shopping.util.Book" offset="0" length="2">
<tr>
<td><%=book.getBookName()%></td>
<td><%=book.getAuthor()%></td>
<td>waiting...</td>
</tr>
</logic:iterate>
</table>
<%}%>
</html:form>
</body></html:html>
解决方案 »
- tomcat5.0.28 严重: BAD packet signature 18245
- 求救:千万级sql优化
- 难道没有人开发过SOAP/JMS的webservice??
- oracle启动
- 请教tomcat为什么总是抛出此异常
- 怎么样删除一个文件!????????
- J2EE安装时出现问题!求救。
- colspan问题 ,简单得很!
- 为什么IE6不能正确的显示我的XML文件?
- IDEA无法扫描到JDK9下的某些包?而JDK7可以扫描到?
- 急!急!急!请教各位struts1.1中的validation.xml和validator-rules.xml问题!!!
- struts中读取select option的错误问题
先定义一个id,然后定义你要传递的object,以及这个object的type。之后通过tag来写入每个属性。注意tag的配对。
<logic:iterate name="vesselInList" id="vesselIn" scope="request" type="longkou.entity.VesselIn">
<tr bgcolor=#ffffff align="center">
<td height=25 width="60">
<bean:write name="vesselIn" property="vessel_no"/>
</td>
<td height=25 width="60">
<bean:write name="vesselIn" property="voyage_no"/>
</td>
<td height=25 width="55">
<bean:write name="vesselIn" property="from_port_no"/>
</td>
<td height=25 width="60">
<bean:write name="vesselIn" property="end_port_no"/>
</td>
<td height=25 width="58">
<bean:write name="vesselIn" property="man_approve"/>
</td>
<td height=25 width="101">
<bean:write name="vesselIn" property="dept_approve"/>
</td>
<td height=25 width="162">
<bean:write name="vesselIn" property="date_approve"/>
</td>
</tr>
</logic:iterate>
通常与<bean:write name="" property="" filter=""/>配合使用,name值要与前面的id值相等。
<tr>
<td><%=book.getBookName()%></td>
<td><%=book.getAuthor()%></td>
<td>waiting...</td>
</tr>
</logic:iterate>
中“collection”是什么意思?“offset”、“length”各是什么意思??
http://jakarta.apache.org/struts/userGuide/struts-logic.html
目录嵌套为:
jakarta-struts-1.1-src\jakarta-struts-1.1-src\src\share\org\apache\struts\taglib\logic
描述
collection
如果没有设置name属性,它就指定了要进行重复的集合
Id
页作用域bean和脚本变量的名称,它保存着集合中当前元素的句柄
indexed
页作用域JSP bean的名称,它包含着每次重复完成后集合的当前索引
Length
重复的最大次数
Name
作为集合的bean的名称,或是一个bean名称,它由property属性定义的属性,是个集合
Offset
重复开始位置的索引
property
作为集合的Bean属性的名称
Scope
如果指定了bean名称,这个属性设置bean的作用域。若没有设置,搜索范围从页到应用程序作用域
Type
为当前定义的页作用域bean的类型
例如:<logic:iterate id=”currentInt”collection=”<% =myList %>”type=”java.lang.Integer”offset=”1”length=”2”> <% =currentint %> </logic:iterate>代码将从列表中的第一个元素开始重复两个元素并且能够让当前元素作为页作用域和java.lang.Integer类型的脚本变量来使用。也就是说,如果myList包含元素1,2,3,4等,代码将会打印1和2。
<tr>
<td><%=book.getBookName()%></td>
<td><%=book.getAuthor()%></td>
<td>waiting...</td>
</tr>
</logic:iterate>id-> </logic:iterate>标签内的bean标识名称。
collection-> 一个数据集合。(你的程序中必有booklist数据结构的相关定义<%……%>)
type-> 用于取collection中每个数据时做相应的类型转换。也就是collection中每个元素的类型。
offset-> 偏移量,从什么开始取数据。
length-> 取多长数据。
<%=book.getBookName()%> 这个就完全是取决于每个程序的不同了。写这个程序的老兄好像不太像让你搞明白。其实这样写你会容易明白:<bean:write name="book" property="bookName"/>其作用是完全一样的。
<%
Iterator booklist =(Iterator)session.getAttribute("bookList");
if(booklist==null)
System.out.println("booklist is null");
else
System.out.println("booklist is NOT null");
%>
这个就是我说的你的booklist 的数据来源—————从session中取得。
[建议]:
你要是自己做项目,就最好不要从session中做数据。完全可以定义一个ActionForm来传递数据。
于是:
<logic:iterate id="book" collection="<%=booklist%>" type="shopping.util.Book" offset="0" length="2">
将会变成:
<logic:iterate id="book" name="YourFormBeanName" property="bookList" type="shopping.util.Book" offset="0" length="2">
<tr>
<td><c:out value="${book.bookName}" /></td>
<td><c:out value="${book.author}" /></td>
<td>waiting...</td>
</tr>
</c:forEach>
多简单。
具体请参考sun的java stander tag library标准。
lyk.java/lykDAO.java/lykForm.java/lykAction四个文件及20分见:
http://expert.csdn.net/Expert/topic/2400/2400412.xml?temp=.2245447另外包含分页、连接、删除、更新功能的软件表格文件lykOUT.jsp内容如下:
--------------
<%@ page contentType="text/html;charset=GB2312" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %><html:html>
<head>
<title></title>
<html:base/>
</head>
<body bgcolor="white">
<table cellPadding=2 width="80%" border=2>
<tr>
<td><span CONTENTEDITABLE=true>user_id</span></td>
<td><span CONTENTEDITABLE=true>password</span></td>
<td><span CONTENTEDITABLE=true>name</span></td>
<td><span CONTENTEDITABLE=true>description</span></td>
<td><span CONTENTEDITABLE=true>web_site</span></td>
<td><span CONTENTEDITABLE=true>address</span></td>
<td><span CONTENTEDITABLE=true>删除</span></td>
<td><span CONTENTEDITABLE=true>编辑</span></td>
<td><span CONTENTEDITABLE=true>链接</span></td></tr>
<bean:define property='offset' name='lykForm' id='offset' />
<bean:define property='length' name='lykForm' id='length' />
<logic:iterate name='lyk' id='lyk' type='OST.lyk' scope='request' offset='<%= String.valueOf(offset) %>' length='<%= String.valueOf(length) %>'>
<logic:notEqual name='lykForm' property='action' value='edit'>
<tr>
<td><bean:write name='lyk' property='user_id' /></td>
<td><bean:write name='lyk' property='password' /></td>
<td><bean:write name='lyk' property='name' /></td>
<td><bean:write name='lyk' property='description' /></td>
<td><bean:write name='lyk' property='web_site' /></td>
<td><bean:write name='lyk' property='address' /></td>
<td><a href="lykAction.do?action=delete&expression=<bean:write name='lyk' property='user_id' />">删除</a></td>
<td><a href="lykAction.do?action=edit&expression=<bean:write name='lyk' property='user_id' />">编辑</a></td>
<td><a href="lykAction.do?action=link&expression=<bean:write name='lyk' property='user_id' />">链接</a></td></tr>
</logic:notEqual>
<logic:equal name='lykForm' property='action' value='edit'>
<tr>
<form method='post' name='lykForm' action="/OST/lykAction.do?action=update&expression=<bean:write name='lyk' property='user_id' />">
<td><html:text name='lyk' property='user_id' /></td>
<td><html:text name='lyk' property='password' /></td>
<td><html:text name='lyk' property='name' /></td>
<td><html:text name='lyk' property='description' /></td>
<td><html:text name='lyk' property='web_site' /></td>
<td><html:text name='lyk' property='address' /></td>
<td><a href="lykAction.do?action=delete&expression=<bean:write name='lyk' property='user_id' />">删除</a></td>
<td><html:submit value='更新'></html:submit></form ><html:form method='post' action='/lykAction.do?action=reset'><html:submit value='取消'></html:submit>
</html:form ></td>
<td><a href="lykAction.do?action=link&expression=<bean:write name='lyk' property='user_id' />">链接</a></td></tr>
</logic:equal>
</logic:iterate><tr><td align=middle colSpan=10 ><bean:write property='mybeanvariable1' name='lykForm' filter='false' /></td></tr></tr>
</table>
</body>
</html:html>
--------------------------------------------------------------------------
其中在“<logic:iterate name='lyk' id='lyk' type='OST.lyk' scope='request' offset='<%= String.valueOf(offset) %>' length='<%= String.valueOf(length) %>'>”中,offset代表本页开始显示的记录,length代码本页显示的记录条数。
<logic:notEqual name='lykForm' property='action' value='edit'> 表示非常编辑时执行下面的代码。
<logic:equal name='lykForm' property='action' value='edit'> 表示编辑时执行下面的代码。
<a href="lykAction.do?action=link&expression=<bean:write name='lyk' property='user_id' />">是显示分页及相应连接的内容。