struts2 标签的使用之一 s:if

1、要使用struts2的标签,先要导入标签库:<%@ taglib prefix="s" uri="/struts-tags"%>

2:OGNL struts2 利用了内建的ognl表达式,它基于XWork,增加了对ValueStack的支持
,在jsp里面通过ognl访问属性,struts2会自动搜寻栈内的所有实体。直到找到位置。
如:#person.address.ip 等于 person.getAddress().getIp();翻译结果为条用get方法
或是jstl的${person.address.ip}

<s:if>用法
A:直接写表达式

<s:set name='china' value='china'>
<s:if test="${china=='china'}">show</s:if>
result:  show
<s:set name="count" value="99">
<s:if test="${count>0}">bigger than 0</s:if>
<s:else>not</s:else>
result:  bigger than 0
B:在遍历里面使用判断:

<s:iterator id="id" value="label">
    <s:if test="%{#id.attrValueId!=0}">
             <s:property value="#id.attrValue" /> 
             <s:property value="#id.countAll" /> 

              <s:property value="#id.countRequest" /> 
    </s:if>
    <s:else>
        <s:property value="#id.attrValue" />
    </s:else>
</s:iterator>
label是一个List<Attribu>  Attribu 包含属性attrValueId和countAll
在s:iterator域内这是id的值是"id",使用ognl读取遍历对象的方法是 #id
test="%{#id.attrValueId!=0}" 看子对象的属性attrValueId是否为0
<s:property value="#id.attrValue" /> 打印子对象的attrValue属性

C:直接读取对象

<s:if test="request.price==null||request.price<=0">
</s:if>
读取对象request,判断price是否小于0;
request 可以是如何的javaBean,也可以是基本属性

D:直接读取对象的另一种写法

 <s:if test="%{aTransactionSummaryBean!=null}">
E:多个条件的判断

<s:if test='%{isShowAll=="Y"||isShowAll==null||isShowAll==""}'>
    <li class="selected">
</s:if>
<s:else>
    <li>else
</s:else>
isShowAll 为Action 里面的字符串属性

F:直接拿Action里面的boolean 貌似不xing
Action里面

private boolean choosed = true;
public boolean isChoosed(){
    return choosed;
}
<s:if test="choosed"></s:if>
发现这个判断无法正确运行,也许是ognl 是通过get方法来获取对象的,如果在action 里面有下面的方法;

public String getChoosed(){
    return "true";
}
上面那个s:if可以正确执行

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值