OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,
它是一个开源项目。 Struts2框架使用OGNL作为默认的表达式语言。
作用:取值,获取javaBean中的属性,获取List或者数组元素,获得map的键值对,还可以执行逻辑运算。
要求:我们必须把ognl表达式写在struts的标签中。
1.ognl对普方法的调用
<s:property value="'zhang'"/>
<s:property value="'zhang'.toUpperCase()"/>
2.ognl对静态变量和静态方法的调用
<s:property value="@java.lang.Integer@MAX_VALUE"/>
<s:property value="@java.lang.Math@abs(-100)"/>
静态变量和静态方法的调用都要使用@类的全路径@[静态变量或静态方法]
但是如果是静态方法的调用必须要先开启
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
3.ActionContext
context map:是每次请求访问时存储数据的对象,每一次请求都会创建context map,我们可以把数据来存放到map中。
Key | Value | 说明 |
Value stack | List集合 | 以栈的方式来存储数据 |
Request | Map<String,Object>结构 | 以键值对的方式存储请求范围的数据 |
session | Map<String,Object>结构 | 以键值对的方式存储会话范围的数据 |
Application | Map<String,Object>结构 | 以键值对的方式存储应用范围的数据 |
Action | Object类型 | 当前访问的Action动作类对象 |
Parameters | Map<String,Object>结构 | 存储请求的参数 |
Attr | Map<String,Object>结构 | 根据key从page,request,session,application范围依次查找属性的值。 |
每一个请求访问时都会创建一个contextMap的对象,ValueStack和contextmap的数据是关联的可以相互转化
我们的数据采用两个存储的结构来存储一个valueStack(set(map)),contextMap(map)
1.在context map中存取数据
在请求范围内存储数据
public String execute() throws Exception {
System.out.println(username);
ActionContext ac = ServletActionContext.getContext();
//在contextmap中存储数据,默认的相当于request,因为ActionContext的生命周期和request一样都是一次请求
ac.put("name", "zzzz");
return super.execute();
}
在contextmap中取数据要使用#(success.jsp页面)
<s:property value="#name"/>
<s:debug></s:debug>
在会话范围存储数据
ac.getSession().put("user", "aaaaaaaaaaaaa");
<s:property value="#session.user"/>
<s:debug></s:debug>
在应用级别的存储数据
ac.getApplication().put("pv", 12345);
<s:property value="#application.pv"/>
<s:debug></s:debug>
2.在值栈中存取数据
会把请求传递的参数存储在值栈中,同时把Action对象也压入栈中,属性对象在上面Action在下面,对象的值既可以从栈顶取,也可以从Action的person属性中来取
新建model
package com.zy.model;
public class Person {
private int personId;
private String personName;
private int gender;
public Person() {
super();
}
public Person(int personId, String personName, int gender) {
super();
this.personId = personId;
this.personName = personName;
this.gender = gender;
}
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
}
personActon
package com.zy.action;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;
import com.zy.model.Person;
public class PersonAction extends ActionSupport implements ModelDriven<Person>{
String username;
private Person person = new Person();
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String execute() throws Exception {
System.out.println(username);
//获得动作类的上下文ActionContext包含了contextmap和valuestack
ActionContext ac = ServletActionContext.getContext();
Person person1 = new Person(2, "wangwu", 2);
//获得值栈
ValueStack vs = ac.getValueStack();
//把person1压栈
vs.push(person1);
return super.execute();
}
@Override
public Person getModel() {
return person;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--
开发模式
-->
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<package name="person" extends="struts-default" >
<action name="hello" class="com.zy.action.PersonAction" >
<result name="success">/success.jsp
</result>
</action>
</package>
</struts>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:property value="#name"/>
<s:property value="#session.user"/>
<s:property value="#application.pv"/>
<s:debug></s:debug>
<hr>
<h3>从栈顶取Action对象中的model的值</h3>
<s:property value="username"/>
<s:property value="person.personId"/>
<s:property value="person.personName"/>
<s:property value="person.gender"/>
<hr>
<h3>从栈顶model对象中去值</h3>
<s:property value="personId"/>
<s:property value="personName"/>
<s:property value="gender"/>
<hr>
<h3>取栈中相同属性从栈顶数第二个值</h3>
<s:property value="[1].personId"/>
<s:property value="[1].personName"/>
<s:property value="[1].gender"/>
</body>
</html>