`
eric.zhang
  • 浏览: 124028 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

OGNL学习笔记

阅读更多
OGNL:
应用的场景:
标签中:
<s:property value="user.name">
配置文件中
<result type="redirect">/main.jsp?name=${name}</result>
调用值栈中对象的普通方法
<s:property value="user.get()">
调用Action中的静态方法 @
<s:property value="@cn.hhty.action.LoginAction@get()">

获取List集合:有序不重复
<s:property value="testList"/> 结果:[list1,list2,list3]
获取集合中的某一个元素
<s:property value="testList[0]">
(可以使用数组中的下标来获取List中的内容)

获取Set集合:无序可重复
获取Set集合
<s:property value="testSet"/>  结果:[set1,set2,set0]
获取Set中某一元素(由于Set无序,所有不能使用下标来取得)

获取Map集合
<s:property value="testMap"/>结果:[key1=value1,key2=value2]
获取Map中某一元素
<s:property value="testMap['key']">
获取大小
<s:property value="testMap.size">
返回map中所有的键:<s:property value="testMap.keys">
返回Map中所有的值:<s:property value="testMap.values">

获取List中所有的对象
<s:property value="listStudents">
利用投影获取List中对象的username属性值
<s:property value="listStudents.(username)">
利用投影获取List中第一个对象的username属性值
<s:property value="listStudents.(username)[0]">
利用投影获取List中成绩及格的对象
<s:property value="listStudents.(?#this.grade>=60).{username}[0]"/> 

OGNL中#的使用:
#可以取出堆栈中上下文中存放的对象

名称作用例子
parameters包含当前http请求参数的Map#parameters.id[0]相当于request.getParameter("id");
request包含当前HttpServletRequest的属性(attribute)的Map#request.userName相当于request.getRequest("userName");
session包含当前HttpSession的属性(attribute)的Map#session.userName相当于session.getAttribute("userName");
application包含当前ServletContext的属性(attribute)的Map#application.userName相当于application.getAttribute("userName");
attr依次request->session->application


后台:实现 RequestAware,SessionAware接口
Map request;
request.put("res","requestValue");
Map session;
session.put("ses","sessionValue");
前台获取:
获取request范围中的属性
<s:property value="#request.res">

获取Session范围中的属性值
<s:property value="#session.ses">

parameters方式
<action name="test" class="com.testAction">
		<result type="redirect">/test.jsp?username=aabbcc</result>
	</action>

在test.JSP里取值
获取Parameters对象的属性:(如果type!=redirect。则取不出来值)
<s:property value="#parameters.username">
注:默认的type是dispatcher, url后传的值前台取不到


OGNL中%的使用:
%可以取出存在值堆栈中的Action对象,直接调用它的方法
例如:如果Action继承了ActionSupport,那么在页面标签中
用%{getText('key')}的方式可以拿出国际化信息

        %{}里面的内容表示是一个表达式,

$有两个主要用途:
1,用于在国际化资源文件中,引用OGNL表达式
2.在Struts2配置文件中,引用OGNL表达式    url?name=${name}


ValueStack对象,这个对象贯穿整个Action的生命周期(每个Action类的对象实例化会
有一个ValueStack对象),当Struts2接收到一个.action的请求后,会先建立
Action类的对象实例,但并不会调用Action方法 ,而是先将Action类的相应属性放
        在ValueStack对象顶层节点(ValueStack对象相当于一个栈)
可以省写#号 如:<s:property value="user.username">
************************************
注:ValueStack需要使用服务器跳转才行
***************************************
前台:<input type="text" name="username">
      <input type="text" name="password">

Test1Action:
private String username;
private String password;
private Student student;
getter/setter
execute method just return success

Test2Action
private Student student;
execute method just return success
服务器跳转:
<action name="test1" class="com.test.test1Action">
			<result type="chain">
				<param name="actionName">test2</param>
			</result>
		</action>
		<action name="test2" class="com.test.test2Action">
服务器跳转		<result>
				test2.jsp
			</result>
		</action>

test2.jsp
从值栈中取用户名:<s:property value="username"/>

此时值栈里有Test1Action,Test2Action的信息,且Test2在顶层,先会在Test2Action里取值,如果没有,再到Test1Action中取
如果想取值栈中第二个对象,则使用Top获取法:<s:property value="[1].top.student">

Top语法:取栈中的对象(也即Action) 从0开始
N语法获取属性:<s:property value="[1].student">  从0开始
@语法:

栈:后进先出
======================================================
客户端跳转
<action name="test1" class="com.test.test1Action">
			<result type="redirectAction">
				<param name="actionName">test2</param>
			</result>
		</action>
		<action name="test2" class="com.test.test2Action">
服务器跳转		<result>
				test2.jsp
			</result>
		</action>


此时值栈里只有test2Action的信息了。

========================================================
<action name="test1" class="com.test.test1Action">
			<result type="redirectAction">
				<param name="actionName">test2?username=${username}</param>
			</result>
		</action>
		<action name="test2" class="com.test.test2Action">
			<result type="redirect">
				test2.jsp
			</result>
		</action>

此时值栈里没有信息

在Action里也可以往ValueStack里手动的添加对象
ActionContext.getContext().getValueStack().push(new Student());

调用Action中的静态方法
<s:property value="@test1@get()"/>

获取ValueStack中第一个学生的信息
<s:property value="[0].username"/>


分享到:
评论

相关推荐

    OGNL学习笔记,包含struts2中ognl的各种用法

    struts2 OGNL的主要用法 基本覆盖全部

    struts2 OGNL语言学习笔记

    本人在学习struts2框架时的学习笔记,主要是ognl表达式语言的运用。希望对大家有所帮助!

    Ognl_JSTL_学习笔记.zip

    Ognl_JSTL_学习笔记

    新手整理OGNL笔记.wps

    新手学习整理的OGNL笔记。可能有不正确之处,请谨慎。

    OGNL源代码以及个人笔记

    OGNL 源代码 jar 个人笔记 方便学习者使用广大JAVA爱好者学习使用

    Struts2_OGNL 笔记

    OGNL的用法 OGNL是通常要结合Struts 2的标志一起使用,如等。大家经常遇到的问题是#、%和$这三个符号的使用。下面我想通过例子讲述这个问题: 首先新建名为Struts2_OGNL的Web工程,配置开发环境。之前很多朋友在使用...

    OGNL表达式总结

    OGNL表达式总结,个人学习笔记,包含尽可能多的OGNL标签的使用方法,实例等,供大家学习使用

    struts2 学习重点笔记

    这是学习struts2时记得重点笔记,包括了一些原理,ognl语句的编写,以及如何设置拦截器等等一些基本知识,起到复习和巩固的作用

    Struts2.1学习笔记

    基于 Struts2.1.8 包括Struts2的基本应用、文件上传、拦截器、输入校验、国际化、OGNL表达式、Struts2标签等内容。

    Struts2 学习笔记

    03 OGNL表达式语言 23 04 Struts2-Tags 28 Struts2标签目录 28 一、 property标签 28 二、 set标签 29 三、 bean标签 29 四、 标签-少使用 29 五、 If elseif else 30 六、 Iterator标签 31 七、 Theme 31 05设计...

    struts学习笔记(4)

    15.OGNL表达式 对象图导航语言 Object Graph Navigation Language 是一种比较强大的表达式语言(比EL表达式的功能多很多) 1)ognl表达式只能写在Struts2标签的里面(任何一个struts2标签都行) EL表达式可以写在页面的...

    Struts学习笔记.txt

    Struts2 必备5个jar包: commons-logging-api-1.1.jar freemarker-2.3.8.jar ognl-2.6.11.jar struts2-core-2.0.8.jar xwork-2.0.3.jar

    struts2的学习笔记+测试源代码

    struts2的ognl表达式 博文链接:https://wuzhaohuixy-qq-com.iteye.com/blog/710102

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

    mybatis学习笔记

    文章目录Mybatismybatis的概述持久层技术解决方案mybatis的入门mybatis入门案例mybatis基于注解的入门案例:自定义mybatis的分析mybatis主配置文件的常用配置OGNL(Object Graphic Navigation Language)表达式mybatis...

    Struts 2.1.8_学习源码

    Struts 2.1.8 学习源码内容 Struts2_01FirstDemo : 跑通第一个Struts2的实例 Struts2_02CURD : 关于Struts2的增、删、改和查 实际业务中数据来自数据库,从DAO层查询,本实例使用静态资源的方式模拟, 主要是关于...

Global site tag (gtag.js) - Google Analytics