目录
1、 OGNL表达式(了解)
2、 栈值(重点)
3、 OGNL特殊符号(了解)
内容
1、OGNL表达式
1.1技术分析之OGNL表达式概述(了解)
1.1.1 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写
*所谓对象图,即以任意一个对象为根,通过OGNL可以访问与这个对象关联的其它对象
*通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性
1.1.2. Struts2框架使用OGNL作为默认的表达式语言
*OGNL是一种比EL强大很多倍的语言
*xwork提供 OGNL表达式
*ognl-3.0.5.jar
1.1.3. OGNL 提供五大类功能
* 支持对象方法调用
* 支持类静态的方法调用和值访问
* 访问OGNL上下文(OGNL context)和ActionContext
* 支持赋值操作和表达式串联
* 操作集合对象
1.1.4. 测试的代码
//访问对象的方法 @Test publicvoid run1() throws OgnlException{ OgnlContext context = new OgnlContext(); //获取对象的方法 Objectobj = Ognl.getValue("'helloworld'.length()", context,context.getRoot()); System.out.println(obj); }
//获取OGNL上下文件的对象 @Test publicvoid run3() throws OgnlException{ OgnlContext context = new OgnlContext(); context.put("name","美美"); //获取对象的方法 Objectobj = Ognl.getValue("#name", context, context.getRoot()); System.out.println(obj); }
//从root栈获取值 @Test publicvoid demo3() throws OgnlException{ OgnlContextcontext = new OgnlContext(); Customerc = new Customer(); c.setCust_name("haha"); context.setRoot(c); Stringname = (String) Ognl.getValue("cust_name", context,context.getRoot()); System.out.println(name); } ctrl+shift+T
1.2、技术分析之在Struts2框架中使用OGNL表达式
1.2.1. Struts2引入了OGNL表达式,主要是在JSP页面中获取值栈中的值
1.2.2. 具体在Struts2中使用。如下步骤
// 需要先引入Struts2的标签库 <%@ taglib prefix="s"uri="/struts-tags" %> // 使用Struts2提供的标签中的标签 <s:propertyvalue="OGNL表达式"/>
1.2.3. 在JSP页面使用OGNL表达式
//访问对象方法 <s:propertyvalue="'hello'.length()"/>
2、栈值
2.1栈值概述
值栈就相当于Struts2框架的数据的中转站,向值栈存入一些数据。从值栈中获取到数据。
ValueStack 是struts2 提供一个接口,实现类 OgnlValueStack ---- 值栈对象 (OGNL是从值栈中获取数据的 )
Servlet是单例的,Action是多例的,有一起请求,创建Action实例,创建一个ActionContext对象,代表的是Action的上下文对象,还会创建一个ValueStack对象。
每个Action实例都有一个ValueStack对象 (一个请求 对应 一个ValueStack对象 )
在其中保存当前Action 对象和其他相关对象
Struts 框架把ValueStack 对象保存在名为 “struts.valueStack” 的请求属性中,request中 (值栈对象 是 request一个属性)下面是最初的获取方式:
HttpServletRequest request =ServletActionContext.getRequest(); ValueStack vs =(ValueStack)request.getAttribute("struts.valueStack");
2.2栈值内部结构
2.2.1值栈由两部分组成
root -- Struts把动作和相关对象压入 ObjectStack中--List
context --Struts把各种各样的映射关系(一些 Map 类型的对象) 压入 ContextMap 中
2.2.2 Struts会默认把下面这些映射压入ContextMap(context)中
注意:request代表的是Map集合的key值,value的值其实也是一个Map集合。Map<request,<key,value>>
parameters: 该 Map 中包含当前请求的请求参数 ?name=xxx&password=123
request: 该 Map中包含当前 request 对象中的所有属性
session: 该 Map中包含当前 session 对象中的所有属性
application:该 Map中包含当前 application 对象中的所有属性
attr: 该 Map按如下顺序来检索某个属性: request, session, application
2.2.3 ValueStack中存在root属性(CompoundRoot)、context属性(OGNLContext)
CompoundRoot就是ArrayList
OgnlContext就是 Map
2.2.4 context 对应Map引入 root对象
context中还存在request、 session、application、 attr、 parameters 对象引用
OGNL表达式访问值栈中的数据
访问root中数据时 不需要 #
访问 request、 session、application、 attr、 parameters 对象数据 必须写 #
操作值栈 默认指 操作 root 元素
2.3值栈的创建和ActionContext对象的关系
值栈对象是请求时创建的
ActionContext是绑定到当前的线程上,那么在每个拦截器或者Action中获取到的ActionContext是同一个。
ActionContext中存在一个Map集合,该Map集合和ValueStack的context是同一个地址。
ActionContext中可以获取到ValueStack的引用,以后再开发,使用ActionContext来获取到值栈对象
2.4获取栈值对象s三种方式
ValueStackvs1 = (ValueStack)ServletActionContext.getRequest().getAttribute("struts.valueStack"); ValueStackvs2 = (ValueStack)ServletActionContext.getRequest().getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY); ValueStackvs3 = ActionContext.getContext().getValueStack();
2.5 向栈值中保存数据(主要是root栈)
valueStack.push(Objectobj);//push方法的底层调用root对象的push方法(把元素添加到0位置) // 源码获取map集合(map有可能是已经存在的,有可能是新创建的),把map集合push到栈顶,再把数据存入到map集合中。 valueStack.set(Stringkey, Object obj); //在jsp中通过 <s:debug /> 查看值栈的内容
2.6 在JSP中从栈值中获取值
2.6.1常识书写规范
访问root中数据不需要#
访问context其它对象数据加 #
如果向root中存入对象的话,优先使用push方法。
如果向root中存入集合的话,优先要使用set方法。
2.6.2在OgnlContext中获取数据
在Action中向域对象中存入值
request:<s:propertyvalue="#request.username"/>
session:<s:propertyvalue="#session.username"/>
application:<s:propertyvalue="#application.username"/>
attr:<s:propertyvalue="#attr.username"/>
parameters:<s:propertyvalue="#parameters.cid"/>
2.6.3 压栈字符串结合图像要深入理解[0].top代表着什么
// Action中put方法压栈 vs.push("美美"); // JSP获取到栈顶的值 <s:propertyvalue="[0].top"/> <!—Action中set方法压栈 //栈顶是map集合 vs.set("msg","小凤"); // 通过key获取值 <s:propertyvalue="[0].top.msg"/> -->
2.6.4 压栈对象
<!-- vs.push(user); //栈顶放user对象 <s:propertyvalue="[0].top.username"/> <s:propertyvalue="[0].top.password"/> //[0].top 关键字是可以省略的 findValue() <s:propertyvalue="username"/> --> <!--使用set的map集合方法 vs.set("user",user); <s:propertyvalue="[0].top.user.username"/> <s:propertyvalue="[0].top.user.password"/> //省略关键字 <s:propertyvalue="user.username"/> -->
2.6.5压栈list集合
<!-- 栈顶是list集合 vs.push(ulist); <s:propertyvalue="[0].top[0].username"/> <s:propertyvalue="[0].top[1].username"/> --> <!-- vs.set("ulist",ulist); <s:propertyvalue="ulist[0].username"/> -->
2.6.6 迭代查询标签
<!--迭代的标签 属性 *value 要迭代的集合,需要从值栈中获取 *var 迭代过程中,遍历的对象 *var编写上,把迭代产生的对象默认压入到context栈中,从context栈取值,加#号 *var不编写,默认把迭代产生的对象压入到root栈中 for(Useruser:ulist){} //编写var的属性 <s:iteratorvalue="ulist" var="u"> <s:propertyvalue="#u.username"/> <s:propertyvalue="#u.password"/> </s:iterator> //没有编写var关键字 <s:iteratorvalue="ulist"> <s:propertyvalue="username"/> <s:propertyvalue="password"/> </s:iterator> -->
2.6.7从context栈中获取值
<!-- 从context栈中获取值,加#号 HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("msg","美美"); request.getSession().setAttribute("msg","小风"); <s:propertyvalue="#request.msg"/> <s:propertyvalue="#session.msg"/> <s:propertyvalue="#parameters.id"/> <s:propertyvalue="#attr.msg"/> --> <!--在JSP页面上,查看值栈的内部结构 --> <s:debug></s:debug>
2.7 EL表达式也会获取到值栈中的数据
原因:StrutsPreparedAndExecuteFilter的doFilter代码中 request =prepare.wrapRequest(request); 对Request对象进行了包装,StrutsRequestWrapper 增强了request的 getAttribute Objectattribute = super.getAttribute(s); if(attribute == null) { attribute = stack.findValue(s); } 访问request范围的数据时,如果数据找不到,去值栈中找 request对象 具备访问值栈数据的能力 (查找root的数据)
3、OGNL表达式特殊符号(了解)
3.1 # 符号的用法
* 获得contextMap中的数据
><s:property value="#request.name"/>
><s:property value="#session.name"/>
><s:property value="#application.name"/>
><s:property value="#attr.name"/>
><s:property value="#parameters.id"/>
><s:property value="#parameters.name"/>
* 构建一个map集合
<s:radioname="sex" list="{'男','女'}"></s:radio>
<s:radioname="sex" list="#{'0':'男','1':'女'}"></s:radio>
3.2 % 符号的用法
*强制字符串解析成OGNL表达式。
>例如:在request域中存入值,然后在文本框(<s:textfield>)中取值,现在到value上。
><s:textfield value="%{#request.msg}"/>
*{ }中值用''引起来,此时不再是ognl表达式,而是普通的字符串
>例如:<s:property value="%{'#request.msg'}"/>
3.3 $ 符号的用法
*在配置文件中可以使用OGNL表达式,例如:文件下载的配置文件。
<actionname="download1" class="cn.itcast.demo2.DownloadAction">
<resultname="success" type="stream">
<paramname="contentType">${contentType}</param>
<paramname="contentDisposition">attachment;filename=${downFilename}</param>
</result>
</action>
案例:使用Struts的list压栈方法读取所有客户
1、编写Action方法,配置Struts。xml文件
2、service层
3、Hibernate查询返回list<Customer>集合
4、action类获得值栈,压栈集合一般是set方法。压入 的是对象,一般使用push对象
5、返回String交给result处理
6、jsp页面读取(struts2标签、jstl标签)