[转]struts2 标签的使用之一 s:if

    技术2025-09-23  39

    struts2 的web 项目中为了方便的编写jsp,标签是最好的选择

    1:struts2 标签库的定义在**-core-版本号.jar META-INF 路径下找到struts-tags.tld文件;使用该标签需要在web 项目里面导入标签库:    A:在web.xml文件  (默认 可以省略)

    1.<taglib>2.    <taglib-uri>/struts-tags</taglib-uri>3.    <taglib-location>/WEB-INF/lib/*.jar</taglib-location>4.<taglib>    B:在jsp 导入标签的dingyi

    1.<%@ taglib prefix="s" uri="/struts-tags"%>注意uri要一直,上面定义的是默认写法 2:OGNL struts2 利用了内建的ognl表达式,它基于XWork,增加了对ValueStack的支持,在jsp里面通过ognl访问属性,struts2会自动搜寻栈内的所有实体。直到找到位置。如:#person.address.ip 等于 person.getAddress().getIp();翻译结果为条用get方法或是jstl的${person.address.ip}

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

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

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

    C:直接读取对象

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

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

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

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

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

    1.private boolean choosed = true;2.public boolean isChoosed(){3.    return choosed;

    **出处:http://blog.csdn.net/chinajust/archive/2009/02/22/3922718.aspx

    最新回复(0)