Struts之四 ValueStack及OGNL

5 值栈 ValueStack: 


5.1 可以从 ActionContext 中获取值栈对象

ValueStack valueStack = ActionContext.getContext().getValueStack();

5.2 值栈分为两个逻辑部分

> Map 栈(ContextMap): 实际上是 OgnlContext 类型, 是个 Map, 也是对 ActionContext 的一个引用. 里边保存着各种 Map : requestMap, sessionMap, applicationMap, parametersMap, attr

> 对象栈(ObjectStack): 实际上是 CompoundRoot 类型, 是一个使用 ArrayList 定义的栈. 里边保存各种和当前 Action 实例相关的对象.是一个数据结构意义的栈.

5.3 从值栈中取值 


(1)读取 ObjectStack 里的对象的属性

>若想访问 ObjectStack 里的某个对象的属性. 可以使用以下几种形式之一: 

object.propertyName

object["propertyName"]

object["propertyName"]


>ObjectStack 里的对象可以通过一个从零开始的下标来引用. ObjectStack 里的栈顶对象可以用 [0] 来引用, 它下面的那个对象可以用 [1] 引用. 若希望返回栈顶对象的 message 属性值:  [0].message 或 [0][“message”] 或 [0][‘message’]

若在指定的对象里没有找到指定的属性, 则到指定对象的下一个对象里继续搜索. 即 [n] 的含义是从第 n 个开始搜索, 而不是只搜索第 n 个对象。若从栈顶对象开始搜索, 则可以省略下标部分


(2)读取 Context Map 里的对象的属性(需要给 OGNL 表达式加上一个前缀字符 #. 如果没有前缀字符 #, 搜索将在 ObjectStack 里进行)

#object.propertyName

#object["propertyName"]

#object["propertyName"]

示例:

返回 session 中的 code 属性: #session.code

返回 request 中的 customer 属性的 name 属性值: #request.customer.name

返回域对象(按 request, session, application 的顺序)的 lastAccessDate 属性: #attr.lastAccessDate

  
6 OGNL通用标签


6.1 property标签


(1)从值栈中取值

<s:property value="[0].productName"/>

<s:property value="#request.rs.pt.productName"/>

(2)调用Java 类里的静态字段或方法  

<s:property value="java.lang.Math@PI"/> 

<s:property value="java.lang.Math@cos(0)"/>

(3) 调用被压入值栈的对象的字段或方法

<s:property value="[0].productName"/>

<s:property value="[0].printValue('hello')"/>

//调用方法为属性值赋值

<s:property value="[0].setProductName('abc')"/>

(4)访问数组类型的属性

<%

String[] names = new String[]{"aa","bb","cc","dd"};

request.setAttribute("names",names);

%>

<s:property value="request.names.length"/>

<s:property value="request.names[0]"/>

(5) 访问list类型的属性

<s:property value="request.names.size()"/>

<s:property value="request.names.get(0)"/>

(6) 访问Map类型的属性

<s:property value="[0].productName"/>

<s:property value="[0].['productName']"/>


6.2 url及param标签


>url 标签用来动态地创建一个 URL

>param 标签用来把一个参数传递给包含着它的那个标签

无论在给出 value 值时有没有使用 %{}, Struts 都会对它进行 ognl 求值

如果想传递一个 String 类型的字符串作为参数值, 必须把它用单引号括起来. 

可以把 value 属性的值写在开始标签和结束标签之间. 利用这种方式来传递一个 EL 表达式的值


(1)创建一个url字符串

<s:url value="/getProduct" var="url1">

<s:param name="productId" value="2002"></s:param>

</s:url>

${url1 }

<!-- 输出结果:  /HelloWord/getProduct?productId=2002  -->

    

(2)对value的值会自动进行OGNL解析

<s:url value="/getProduct" var="url2">

<s:param name="productName" value="productName"></s:param>

</s:url>

${url2 }

<!-- 输出结果:  /HelloWord/getProduct?productName=ttt  -->

    

(3)若不希望进行OGNL解析,则加上单引号

<s:url value="/getProduct" var="url3">

<s:param name="productName" value="'productName'"></s:param>

</s:url>

${url3 }

<!-- 输出结果:  /HelloWord/getProduct?productName=productName  -->


(4)action&method

<s:url action="Product" method="save" var="url4">

</s:url>

${url4 }

<!-- 输出结果:  /HelloWord/Product!save.action    -->

    

(5)includeParams

<s:url value="/getProduct" includeParams="all" var="url5">

</s:url>

${url5 }

<!-- 输出结果:  /HelloWord/getProduct?userName=admin&productName=&password=098765  -->


6.3 set标签:


>用来在以下 Map 对象里创建一个键值对:ContextMap 值栈(session、application、request、page)

<s:set name="productName" value="productName" scope="page"></s:set>

procuctName: ${pageScope.productName }

<!-- 输出结果:  procuctName: ttttt  -->


<s:set name="productName" value="'productName'" scope="page"></s:set>

procuctName: ${pageScope.productName }

<!-- 输出结果:  procuctName: productName  -->


6.4 push标签


>push 标签将把一个对象压入 ValueStack 而不是压入 ContextMap. 

>push 标签在标签起始时把一个对象压入栈, 标签结束时将对象弹出栈

<%

Person p = new Person();

p.setName("zhang");

p.setAge(20);

request.setAttribute("person",p);

%>

<s:push value="request.person">

name:${name}

</s:push>

name:${name}


6.5 if,else 和elseif标签


<s:if test="procuctPrice>1000">

i7处理器

</s:if>

<s:elseif test="procuctPrice>800">

i5处理器

</s:elseif>

<s:else>

i3处理器

</s:else>


6.6 iterator


>用来遍历一个数组, Collection 或一个 Map, 并把这个可遍历对象里的每一个元素依次压入和弹出 ValueStack 栈

<%

List<Person> persons = new ArrayList<Person>();

person.add(new Person("aa",22));

person.add(new Person("bb",33));

person.add(new Person("cc",44));

request.setAttribute("persons",persons);

%>

<s:iterator value="request.persons">

姓名:${name} <br>

年龄:${age}  <br>

</s:iterator>


6.7 sort标签

<%

PersonComparator pc = new PersonComparator();

request.setAttribute("comparator", pc);

%>

<s:sort comparator="request.comparator" source="persons" var="persons2">

<s:iterator value="attr.persons2">

姓名:${name} <br>

年龄:${age}  <br>

</s:iterator>


6.8 date标签


<s:date name="session.date" format="yyyy-MM-dd hh:mm:ss" var="data2"/>

date:${data2}


7 表单标签


<s:form action="save">

<s:textfield name="userName" label="userName"></s:textfield>

<s:password name="password" label="password"></s:password>

<s:textarea name="content" label="content"></s:textarea>

<%

List<City> cityList = new ArrayList<City>();

City city = null;

city = new City("0","wuhan");

cityList.add(city);

city = new City("1","xiangfan");

cityList.add(city);

city = new City("2","xiaogan");

cityList.add(city);

request.setAttribute("cites",cityList);

%>


<s:radio list="#{'1':'Male','2':'Female'}" label="Gender" name="gender"></s:radio>

<s:checkboxlist list="#request.cityList" label="cities" name="cities"

listKey="cityId" listValue="cityName"></s:checkboxlist>

<s:submit></s:submit>

<s:select list="{12,13,14,15,16}" label="Age" name="age"

headerKey="" headerValue="Please Select"></s:select>

</s:form>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值