基本STRUTS标签-学习笔记-Logic标签

BEAN标签(name 是从别处得来的;id是自己的,相当于变量;property相当于变量的值)

前提:

1 String str=request.getParameter("param"); 
2 out.println("str); 

相当于:

1 <bean:parameter id="str" name="param"/>
2 <bean:write name="str"/>

 写stu.stuId。

<bean:write name="stu" property="stuId"/> 

<logic:iterate>

id:遍历的过程中,将其集合内元素起名为id,注意,此时元素可以为JavaBean.

设置name和property:

①数组的情况。

1 String[] colors=new String[]{"red","yellow","green"};
2 request.setAttribute("colors", colors);
3  
4 <logic:iterate id="color" name="colors">
5    <bean:write name="color"/>
6 </logic:iterate>

 ②集合的情况。

1 //集合
 2 ArrayLis  books=new ArrayList();
 3 books.add("水浒传");
 4 books.add("西游记");
 5 request.setAttribute("books",books);
 6 
 7 <logic:iterate id="book" name="books">
 8     <bean:write name="book"/>
 9 </logic:iterate>
10 
11 Map map=new HashMap();
12 map.put("1","11");
13 map.put("2", "22");
14 request.setAttribute("map", map);
15 
16 <logic:iterate id="e"  name="map">
17     <bean:write name="e" property="key"/>
18     <bean:write name="e" property="value"/>
19 </logic:iterate>

③某个集合里面有JavaBean的情况。

 1 // include JavaBean:Student
 2 ArrayList stus=new ArrayList();
 3 Student stu1=new Student();
 4 stu1.setStuId("001");
 5 Student stu2=new Student();
 6 stu2.setStuId("002");
 7 Student stu3=new Student();
 8 stu3.setStuId("003");
 9 
10 stus.add(stu1);
11 stus.add(stu2);
12 stus.add(stu3);
13 
14 session.setAttribute("stus",stus);
15 
16 //another page
17 <logic:iterate id="stu" name="stus">
18   <logic:write name="stu" property="stuId"/>
19 </logic:iterate>

④Javabean里面有集合的情况。

 1 //include JavaBean:Student & jihe
 2 Student stu4=new Student();
 3 ArrayList phones=new ArrayList();
 4 phones.add("00011");
 5 phones.add("00022");
 6 stu4.setPhones(phones);
 7 session.setAttribute("stu4",stu4);
 8 
 9 //another page
10 
11 <logic:iterate id="phone" name="stu4" property="phones">
12     <logic:write name="phone"/>
13 </loigc:iterate>

如果你真心觉得文章写得不错,而且对你有所帮助,那就不妨小小打赏一下吧,如果囊中羞涩,不妨帮忙“推荐"一下,您的“推荐”和”打赏“将是我最大的写作动力!

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.
qq群 微信
原文地址:https://www.cnblogs.com/hoaprox/p/4526742.html