01.什么是OGNL
OGNL:Object Graph Navigation Language 对象导航图语言
开源项目,取代页面中Java脚本,简化数据访问
和EL同属于表达式语言,但功能更为强大
让我们通过简单一致的表达式语法来读取和设置Java对象的属性值,调用对象的方法,遍历整个对象的结构图,实现字段类型转换等功能。
02.OGNL在Struts 2中的作用
表达式语言
将表单或Struts 2标签与特定的Java数据绑定起来,用来将数据移入、移出框架
类型转换
数据进入和流出框架,在页面数据的字符串类型和Java数据类型之间进行转换
03.OGNL融入Struts 2
数据流入
数据流出
04.值栈与OGNL
值栈(ValueStack)
由Struts 2框架创建的存储区域,具有栈的特点
Action的实例会被存放到值栈中
OGNL访问值栈
按照从上到下的顺序,靠近栈顶的同名属性会被读取
案例:
页面遍历数据
实体类::
package cn.happy.struts08ognl.entity;
/**
* Created by linlin on 2017/10/27.
*/
public class UserInfo {
private String name;
private String pwd;
private Address address=new Address();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
package cn.happy.struts08ognl.entity;
/**
* Created by linlin on 2017/10/27.
*/
public class Address {
private String stream;
public String getStream() {
return stream;
}
public void setStream(String stream) {
this.stream = stream;
}
}
Action类:
package cn.happy.struts08ognl.action;
import cn.happy.struts08ognl.entity.UserInfo;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.ServletActionContext;
import java.util.ArrayList;
import java.util.List;
/**
* Created by linlin on 2017/10/27.
*/
public class FirstAction implements Action{
public String execute() throws Exception {
UserInfo info=new UserInfo();
info.setName("素素");
UserInfo info2=new UserInfo();
info.setName("素素2");
//将info队形推入值栈
//获取到值栈
ValueStack valueStack = ActionContext.getContext().getValueStack();
// valueStack.getRoot().push(info);
// valueStack.push(info);
/* List<UserInfo> list=new ArrayList<UserInfo>();
list.add(info);
list.add(info2);
valueStack.getContext().put("list",list);*/
ServletActionContext.getRequest().setAttribute("info",info);
return SUCCESS;
}
}
在父struts.xml中我们调用 struts-day08.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="day-08" namespace="/" extends="struts-default">
<action name="ognl" class="cn.happy.struts08ognl.action.FirstAction">
<result name="success">/day08/ognl.jsp</result>
</action>
</package>
</struts>
页面:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: linlin
Date: 2017/10/22
Time: 9:22
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>OGNL</title>
</head>
<body>
<h1>OGNL</h1>
<h1><s:property value="info"></s:property> </h1>
<%--<s:property value="#list"></s:property>
<s:iterator value="#list">
<s:property></s:property>
</s:iterator>--%>
<s:property value="#request.request"></s:property>
<s:debug></s:debug>
</body>
</html>