韩国人的真实生活图片:在struts中使用logic标签详细说明(2)
来源:百度文库 编辑:九乡新闻网 时间:2024/07/14 11:56:25
通过上述步骤就可以在jsp页面中显示出多条记录了..
1. 遍历集合
<%
Vector animals = new Vector();
animals.addElement("Dog");
animals.addElement("Cat");
animals.addElement("Bird");
animals.addElement("Chick");
request.setAttribute("Animals", animals);
%>
以上代码先定义了一个Vector 类型的集合变量 Animals, 它存放在request 范围内. 接下来
在
Dog
Cat
Bird
Chick
length 属性指定需要遍历的元素的数目, 如果没有设置length 属性, 就遍历集合中的所有元素.
offset 属性指定开始遍历的起始位置, 默认值为 "0" , 表示从集合的第一个元素开始遍历.
scope:可以指定指定作用范围如:scope=”request”或session.
indexId 属性定义一个代表当前遍历元素序号的变量, 这个变量被存放在 page 范围内, 可以被标签主体的
indexId="index" // 遍历元素序号的变量, 这个变量放在page 范围内
name="Animals" // request 中的集合名, 从中取循环取出元素
offset="1" // 从集合的第 2 条记录开始取数
length="2"> // 取出 2 个元素
// 输出每个元素的内容, 与id 的属性一致
2. 遍历Map
<%
HashMap months = new HashMap();
months.put("Jan","January");
months.put("Feb","February");
months.put("Mar","March");
request.setAttribute("month", months);
%>
以上代码先定义一个名为"months" 的HashMap, 存放在request 范围内. 接下来在
0.Mar: March
1.Feb: February
2.Jan: January
如果HashMap 中的每个元素的 value 是集合对象, 则可以采用嵌套的
<%
HashMap h = new HashMap();
String vegetables[] = {"pepper","cucumber"};
String fruits[] = {"apple","orange","banana","cherry","watermelon"};
String flowers[] = {"chrysanthemum","rose"};
String trees[] = {"willow"};
h.put("Vegetables", vegetables);
h.put("Fruits",fruits);
h.put("Flowers",flowers);
h.put("Trees",trees);
request.setAttribute("catalog",h);
%>
indexId="ind" // 与
name="catelog"> // 指定输出元素的名称
property="key"/> // 集合中的键名
name="element" // 指定输出元素的名称
property="value" // 集合中的键值
length="3" // 取3 个元素
offset="1"> // 从第 2 个位置取
-------
以上代码先定义一个名为"catelog" 的HashMap , 存放在request 范围内, 它的每个元素的value 为字符串数组.
接下来外层的
3. 设置被遍历的变量
可以通过以下方式来设置需要遍历的变量
(1) 设置name 属性, name 属性指定需要遍历的集合或Map, 例如:
(2) 设置name 属性和property 属性, name 属性指定一个JavaBean, property 属性指定JavaBean 的一个属性, 这个属性为需要遍历的集合或Map, 例如:
--------
(3) 设置collection 属性, collection 属性指定一个运行时表达式, 表达式的运算结果为需要遍历的集合或Map, 例如:
4. 读取JavaBean 中的数据
(1) 在Jsp 页面中加入JavaBean 如:
上面这个JavaBean 要求必须存在一个集合数组对象,如Vector,Collection,ArrayList 等;在这个JavaBean 的构造函数中,取得数据
库中的数据,并将其存入数组对象中。
(2) 使用
name="articleClasses" // name : JavaBean 在页面中所设置的引用ID.
property="coll"> // coll : JavaBean 中的集合数组属性名称.
paramName="aClasses"
paramProperty="classId">
property="className" /> // 取出JavaBean中, 存放在集合对象中的,对象的className 属性值
(3) 在JavaBean 中的集合对象中存放实体对象的语句如下:
......
public class GetArticleClasses
{
// 数据集合
private Collection coll;
// 返回数据集合
public Collection getColl()
{
return coll;
}
// 构造函数, 取出数据,存入集合中
public GetArticleClasses()
{
coll = new ArrayList();
try{
// 数据库连接
Connection connection = DBConnection.getConnection();
if(connection != null)
{
Statement statement = connection.createStatement();
ResultSet resultset;
ArticleClass articleclass;
resultset = statement.executeQuery("SELECT * FROM table ORDER BY id");
while( resultset.next())
{
articleclass = new ArticleClass();
articleclass.setId(resultset.getInt("id"));
articleclass.setClassId(resultset.getString("class"));
articleclass.setClassName(resultset.getString("name"));
coll.add(articleclass))
}
resultset.close();
connection.close();
} else {
coll = null;
}
} catch(Exception exception) {
coll = null;
}
}
}