1.
logic:empty标签判断脚本变量是否为null,是否是一个空的字符串(长度为0),是否是一个空的collection或map(调用isEmpty()方法来判断)。logic:notEmpty判断标签不为空时处理标签。
<logic:empty name="myBean">是空的</logic:empty><logic:notEmpty name="myBean">不为空</logic:notEmpty>
上段代码表示当一个名为myBean的bean在所有的scope中都不存在时,输出The bean is missing;存在的话输出The bean is not missing。
此标签有三个属性:name、property和scope。
[color=red]logic:present 和 logic:empty他们的用法大致相同,唯一的不同点是:两者在对空字符串的处理上存在着不同。[/color]下面为index.jsp中的代码:1 <logic:notPresent name="users">2 notpresent3 </logic:notPresent>4 <logic:notEmpty name="users">5 notempty6 </logic:notEmpty>7 <logic:empty name="users">8 empty9 </logic:empty>10 <logic:present name="users">11 present12 </logic:present>当第一次访问该JSP的时候,由于users没有定义,并且也不在page,request,session,application任何一个作用域中,因此输出的结果为notpresent,empty。下面我们增加一个action,让他在index.jsp之前执行,然后再跳转到index.jsp中,同时在该action的execute方法中增加如下代码:1String userName = "";2request.setAttribute("users", userName);3return new ActionForward("/index.jsp");4这里将userName保存在request中,key为users,再将请求转发至index.jsp中,但是userName的值为一个空字符串,转发过后,输出的值为:empty,present这里我们再做一次改动,将action的execute方法中的代码改为:1String userName = null;2request.setAttribute("users", userName);3return new ActionForward("/hello.jsp");4不同的是userName 不再为空字符串了,而是null值,当转发至index.jsp后,输出的值为:notpresent,empty 。对比这几次改动,我们可以得出结论:对于没有在page,request,session,application中定义或者是没有分配内存空间(null值)的变量,这两个标记处理的方法是一致的,都会认为此变量不存在(notpresent)或者为空(empty)。而对于空字符串""值,他们的处理就不一样了,[color=red]logic:present 标记认为空字符串仍然是存在的,也就是说,只要是引用了一块内存空间的变量,logic:present 就会返回present ;而logic:empty则认为空字符串仍然为空,由此得出,在logic:empty看来,变量不仅仅要引用一块内存空间,而且该地址空间的值不能为空字符串,否则都认为该变量为空,都会返回empty [/color]
————————————————
版权声明:本文为CSDN博主「jmyyangzhan」的原创文章
原文链接:/jmyyangzhan/article/details/83725631
2.
Iterate主要用来处理在页面上输出集合类,集合一般来说是下列之一:
1、 java对象的数组
2、 ArrayList、Vector、HashMap等
该标记的功能强大,在Struts应用的页面中经常使用到。
iterate标记 :
id 脚本变量的名称,它保存着集合中当前元素的句柄。
name 代表了你需要叠代的集合,来自session或者request的属性。
type 是其中的集合类元素的类型
bean的write标记是用来将属性输出的,name用来匹配iterate的id,property用来匹配相应类的属性
1、对数组进行循环遍历
<table width="100%"><logic:iterate type=" example.User "><tr><td width="50%">name: <bean:write property="name"/><td/><td width="50%">password: <bean:write property="password"/></td></tr></logic:iterate></table>
另外,还可以通过length属性来指定输出元素的个数,offset属性指定了从第几个元素开始输出。
<%String[] testArray={"str1","str2","str3"};pageContext.setAttribute("test",testArray);%<logic:iterate id="show" name="test"><bean:write name="show"/></logic:iterate><br><logic:iterate id="show" name="test" length="2" offset="1"><bean:write name="show"/></logic:iterate>
结果:
str1
str2
str3
str2
str3
另外,该标记还有一个indexId属性,它指定一个变量存放当前集合中正被访问的元素的序号
<logic:iterate length="2" offset="1" indexId="number"><bean:write name="number"/>:<bean:write name="show"/></logic:iterate>
其显示结果为:
1:str2
2:str3
2 对HashMap进行循环遍历
程序代码
<%HashMap countries=new HashMap();countries.put("country1","中国");countries.put("country2","美国");countries.put("country3","英国");countries.put("country4","法国");countries.put("country5","德国");pageContext.setAttribute("countries",countries);%><logic:iterate id="country" name="countries"><bean:write name="country" property="key"/>:<bean:write name="country" property="value"/></logic:iterate>
在bean:write中通过property的key和value分别获得HaspMap对象的键和值。其显示结果为:
country5:德国
country3:英国
country2:美国
country4:法国
country1:中国
由结果可看出,它并未按添加的顺序将其显示出来。这是因为HaspMap是无序存放的。
3、嵌套遍历
程序代码:
<%String[] colors={"red","green","blue"};String[] countries1={"中国","美国","法国"};String[] persons={"乔丹","布什","克林顿"};ArrayList list2=new ArrayList();list2.add(colors);list2.add(countries1);list2.add(persons);pageContext.setAttribute("list2",list2);%><logic:iterate id="first" name="list2" indexId="numberfirst"><bean:write name="numberfirst"/><logic:iterate id="second" name="first"><bean:write name="second"/></logic:iterate><br></logic:iterate>
版权声明:本文为博客园博主夏冬青的原创文章
原文链接:/xiadongqing/p/5241125.html