Struts2自学入门(五)——OGNL
2016-09-21 21:45
471 查看
一、概念
OGNL表达相当于JSP+Servlet模式中JSTL的地位,但是它的功能要比JSTL强大,在学习OGNL表达式时,你需要忘掉JSTL.OGNL能够访问系统中OgnlContext中的对象,
OgnlContext对象是OGNL表达式的下上文对象,即所有通过OGNL表达式取出的数据都是从上下文对象取出来的, OGNL的上下文环境是一个Map结构,称之为OgnlContext。
要想在JSP页面中使用ONGL,得在页头加上:
<%@taglib prefix="s" uri="/struts-tags" %>
二、用例
Action代码 在各个范围内定义值
ValueStack valueStack=actionContext.getValueStack();//值栈 valueStack.set("name", "张三(valueStack)"); valueStack.set("age", 11); Map<String,Object> session= actionContext.getSession(); session.put("name", "王五(session)"); session.put("age", 13); Map<String,Object> application= actionContext.getApplication(); application.put("name", "赵六(application)"); application.put("age", 14); students= new ArrayList<Student>(); students.add(new Student("老九",13)); students.add(new Student("老十",14)); studentMap = new HashMap<String,Student>(); studentMap.put("goodStudent", new Student("学霸",20)); studentMap.put("badStudent", new Student("学渣",20));
在JSP中设置request的VALUEKEY
<%
request.setAttribute("name", "李四(request)");
request.setAttribute("age", "12");
%>
JSP代码
获取狭义上的值栈数据:<s:property value="name"/> <s:property value="age"/> <br> 请求参数:<s:property value="#parameters.name"/> <s:property value="#parameters.age"/><br> request:<s:property value="#request.name"/> <s:property value="#request.age"/><br> session:<s:property value="#session.name"/> <s:property value="#session.age"/><br> application:<s:property value="#application.name"/> <s:property value="#application.age"/><br> ongl访问javaBean对象:<s:property value="student.name"/> <s:property value="student.age"/><br> ongl访问List集合对象:<s:property value="students[0].name"/> <s:property value="student[0].age"/><br> <s:property value="students[1].name"/> <s:property value="student[1].age"/><br> ongl访问Map集合对象:<s:property value="studentMap['goodStudent'].name"/> <s:property value="studentMap['goodStudent'].age"/><br> <s:property value="studentMap['badStudent'].name"/> <s:property value="studentMap['badStudent'].age"/><br>
三、ONGL访问静态方法
首先在Struts.xml中定义:
<!--ognl获取静态方法 --> <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
静态方法:
public class MyStatic {
public static final String str="Java知识分享网";
public static String printUrl(){
//System.out.println("http://www.java1234.com");
return "www.java1234.com";
}
}
页面代码:
<body>
访问静态属性:<s:property value="@com.java1234.common.MyStatic@str"/><!--访问静态成员变量-->
访问静态方法:<s:property value="@com.java1234.common.MyStatic@printUrl()"/><span style="font-family: Arial, Helvetica, sans-serif;"><!--访问静态方法--></span>
</body>
相关文章推荐
- Struts2自学入门(六)——OGNL数据标签和控制标签
- java struts2入门学习--OGNL语言基本用法
- Struts2 入门3:Ognl表达式语言与struts标签
- struts2入门(8)--OGNL
- struts2_day03_01_ognl概述_02_ognl入门案例
- Java自学笔记之struts2入门
- 20160502-struts2入门--ognl表达式
- Struts2自学入门(七)——Struts国际化
- Struts2入门(五)——OGNL和标签库
- 6、如何自学Struts2之Struts2值栈和OGNL语言[视频]
- Struts2自学入门(四)——简单拦截器
- Struts2自学入门(九)——文件上传
- Struts2 OGNL入门
- Struts2自学入门(二)
- Struts2自学入门(十)——多文件上传及文件下载
- java struts2入门学习--防止表单重复提交.OGNL语言学习
- Struts2自学入门(一)
- Web框架梳理:第一章:Struts2入门、传值、分页、对象、值栈、OGNL、响应方式、标签、拦截器
- Struts2自学入门(八)——表单验证
- OGNL, JSTL, STRUTS2标签中符号#,$,%的用法示例