struts2学习笔记 -- day06 OGNL表达式用法

来源:互联网 发布:毛笔书法软件下载 编辑:程序博客网 时间:2024/05/18 14:44

一、OGNL表达式

1、OGNL表达式概述

它是struts2的默认的表达式语言。要想使用它必须依赖于struts2的标签库

<%@ taglib uri="/struts-tags" prefix="s" %>

它其实就是让我们在页面中,使用对象来访问它的方法(带有返回值的方法)

2、明确

OGNL表达式,既可以用于取值展示,也可以用于赋值封装。

取值展示是程序员做的。

赋值封装是struts2框架做的。(它在用的时候,不论是不是用了Struts2标签库)

3、OGNL表达式的基本用法

<%--1、在浏览器上展示OGNL表达式的内容使用的标签:<s:property value=""/>作用:把value属性取值所对应的内容输出到浏览器上。(去OGNL上下文中找数据去了)value属性:它的取值不是一个普通的字符串,而是OGNL表达式。--%>OGNL标准写法:<s:property value="%{OGNLExpression}"/><br/>OGNL简写:<s:property value="OGNLExpression"/>

4、OGNL表达式的特殊用法

(1)、把普通字符串转换成OGNL表达式

<%--2、把一个普通的字符强制看成是OGNL表达式:写法就是:%{字符串}===>OGNL表达式 同时:  我们也可以把一个OGNL表达式强制看成是字符串:  写法:  %{OGNLExpression}===>%{'OGNLExpression'}   ===>%{"OGNLExpression"}  还可以简写为:  'OGNLExpression'  |  "OGNLExpression" --%>OGNL转字符串标准写法:<s:property value="%{'OGNLExpression'}"/><br/>OGNL转字符串简写:<s:property value='"OGNLExpression"'/>

(2)、访问对象的普通方法

<%--3、它可以访问对象的普通方法 --%><s:property value='"OGNLExpression".length()'/><br/><s:property value='"OGNLExpression".toUpperCase()'/><br/><s:property value='"OGNLExpression".split("E")'/><br/>

(3)、访问静态属性

<%--4、它还可以访问静态属性 访问静态属性:要求必须按照格式书写。书写格式:@包名.包名...类名@静态属性名称--%><s:property value="@java.lang.Integer@MAX_VALUE"/>

(4)、访问静态方法

<%--5、它还可以访问静态方法 访问静态方法:要求必须按照格式书写。书写格式:@包名.包名...类名@静态方法名称OGNL表达式默认是不值静态方法调用。要想让其支持,需要在struts.xml配置文件中开启<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>--%><s:property value="@java.lang.Math@random()"/>

(5)、创建集合对象,Map、List

<%--6、它可以创建集合对象,创建Map,创建List --%>HTML标签编写一个性别单选按钮:<input type="radio" name="gender" value="男">男<input type="radio" name="gender" value="女">女<br/>Struts2标签编写一个性别单选按钮:<%--list的取值是一个OGNL表达式。 当写了{}之后就表示创建了一个集合对象:List list = new ArrayList();当变成{'男','女'}的时候,就相当于:list.add('男');list.add('女');--%><s:radio list="{'男','女'}" name="gender"/><hr/>HTML标签编写一个性别单选按钮:<input type="radio" name="gender" value="male">男<input type="radio" name="gender" value="female">女<br/>Struts2标签编写一个性别单选按钮:<%--list的取值是一个OGNL表达式。 当写了#{}之后就表示创建了一个集合对象:Map map = new HashMap();#{}中写的是类似于json格式的数据:#{key:value,key:value}当变成#{'male':'男','female':'女'}的时候,就相当于:map.put("male","男");map.put("female","女");--%><s:radio list="#{'male':'男','female':'女'}" name="gender"/>

二、struts中的迭代标签 -- <s:iterator>

1、标签属性的含义

value:取值是一个OGNL表达式。

var:写了该属性:var的取值作为key,把当前遍历的对象作为value。存入contextMap中。

        没写该属性:把当前遍历的对象压入栈顶。每次遍历结束,都会弹出(map中,从Map移出。值栈中,就会弹栈)

begin:遍历的开始索引

end:遍历的结束索引

step:遍历的步长

status:遍历的计数器对象。status中的方法如下

        getCount():获取当前遍历的是第几个。从1开始

        getIndex():获取当前遍历的索引。从0开始

        isOdd:是否是奇数行

        isEven:是否是偶数行

        isFirst:是否第一行

        isLast:是否最后一行

2、var属性没有写的情况下遍历代码

<s:iterator value="customers"><TR style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none"><TD>${custName }</TD><TD>${custLevel }</TD><TD>${custSource }</TD><TD>${custIndustry }</TD><TD>${custAddress }</TD><TD>${custPhone }</TD><TD><a href="${pageContext.request.contextPath }/customer/CustomerServlet?method=editCustomerUI&custId=${custId}">修改</a>  <a href="${pageContext.request.contextPath }/customer/CustomerServlet?method=removeCustomer&custId=${custId}">删除</a></TD></TR></s:iterator>

3、var属性写了的情况下遍历代码

<s:iterator value="customers" var="cust"><TR style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none"><TD><s:property value="#cust.custName"/></TD><TD><s:property value="#cust.custLevel"/></TD><TD><s:property value="#cust.custSource"/></TD><TD><s:property value="#cust.custIndustry"/></TD><TD><s:property value="#cust.custAddress"/></TD><TD><s:property value="#cust.custPhone"/></TD><TD><a href="${pageContext.request.contextPath }/customer/CustomerServlet?method=editCustomerUI&custId=<s:property value='#cust.custId'/>">修改</a>  <a href="${pageContext.request.contextPath }/customer/CustomerServlet?method=removeCustomer&custId=<s:property value='#cust.custId'/>">删除</a></TD></TR></s:iterator>