OGNL表达式的使用方法
OGNL表达式的使用方法
定义:
OGNL是对象图导航语言(即该表达式访问的是对象及对象的属性)
知识点:
1.基本属性的访问
1.1 访问值栈中action的普通属性
- <s:property value="username"/>
1.2 访问值栈中对象的普通属性
- <s:property value="student.age"/>
1.3 访问值栈中对象(对象包含对象)的普通属性
- <s:property value="cat.friend.name"/>
1.4 访问值栈中对象的普通方法
- <s:property value="cat.friend.say().length()"/>
1.5 访问值栈中action的普通方法
- <s:property value="execute()"/>
1.6 访问静态方法
- <s:property value="@com.wj.struts2.util.OGNLUtil@getString()"/>
1.7 访问静态属性
- <s:property value="@com.wj.struts2.util.OGNLUtil@URL"/>
1.8 访问Math类的属性
- <s:property value="@@max(5,3)"/>
1.9 访问普通类的构造方法
- <s:property value="new com.wj.struts2.action.model.Dog()"/>
2.访问容器
2.1 访问List
2.1.1 访问list
- <s:property value="list"/>
2.1.2 访问list中的某个元素
- <s:property value="list[2]"/>
2.1.3 访问list中某个元素属性的集合
- <s:property value="list.{no}"/>
2.1.4 访问list中某个元素的属性
- <s:property value="list[2].no"/>
2.2 访问Set
2.2.1 访问set
- <s:property value="set"/>
2.2.2 访问set中的某个元素(不可访问,set无顺序)
2.3 访问Map
2.3.1 访问Map
- <s:property value="map"/>
2.3.2 访问Map中的某个元素
- <s:property value="map['key']"/>
2.3.3 访问Map中的所有key
- <s:property value="map.keys"/>
2.3.4 访问Map中的所有value
- <s:property value="map.values"/>
2.3.5 访问容器的大小
- <s:property value="map.size()"/>
3.投影
3.1 通过投影访问list中年龄为25的学生的姓名
- <s:property value="list.{?#this.age==25}[0].{no}[0]"/>
3.2 通过投影访问list中年龄大于25的集合中的首元素的姓名
- <s:property value="list.{^#this.age>20}.{no}"/>
3.3 通过投影访问list中年龄大于25的集合中的尾元素的姓名
- <s:property value="list.{$#this.age>20}.{no}"/>
3.4 通过投影判断list中年龄大于25的集合是否为空
- <s:property value="list.{?#this.age>25}==null"/>
4.栈
用[]来访问栈中的元素,注意:[0]表示从栈中的开始位置进行遍历其中的元素
- <s:property value="[1][0].execute()"/>
示例代码
OGNLAction.java
- /**
- * OGNL表达式
- * @author 健
- */
- public class OGNLAction extends ActionSupport{
- /**
- * 序列化
- */
- private static final long serialVersionUID = 1L;
- /**
- * 猫
- */
- private Cat cat;
- /**
- * List
- */
- private List <Student>list;
- /**
- * Map
- */
- private Map <String,Dog>map;
- /**
- * 密码
- */
- private String password;
- /**
- * Set
- */
- private Set <Dog>set;
- /**
- * 学生
- */
- private Student student;
- /**
- * 用户名
- */
- private String username;
- public OGNLAction(){
- this.list = new ArrayList<Student>();
- Student student1 = new Student("zhangsan",20);
- Student student2 = new Student("lisi",25);
- Student student3 = new Student("wangwu",28);
- Student student4 = new Student("zhaoliu",24);
- Student student5 = new Student("xiaotaoqi",18);
- list.add(student1);
- list.add(student2);
- list.add(student3);
- list.add(student4);
- list.add(student5);
- this.set = new HashSet<Dog>();
- Dog dog1 = new Dog("bandeng");
- Dog dog2 = new Dog("zhuozi");
- Dog dog3 = new Dog("yizi");
- set.add(dog1);
- set.add(dog2);
- set.add(dog3);
- this.map = new HashMap<String, Dog>();
- map.put("0001", dog1);
- map.put("0002", dog2);
- map.put("0003", dog3);
- }
- /**
- * 控制器
- * @return 跳转的页面
- */
- @Override
- public String execute(){
- return "success";
- }
- /**
- * 取得cat
- * @return cat
- */
- public Cat getCat() {
- return cat;
- }
- /**
- * 取得list
- * @return list
- */
- public List<Student> getList() {
- return list;
- }
- /**
- * 取得map
- * @return map
- */
- public Map<String, Dog> getMap() {
- return map;
- }
- /**
- * 取得password
- * @return password
- */
- public String getPassword() {
- return password;
- }
- /**
- * 取得set
- * @return set
- */
- public Set<Dog> getSet() {
- return set;
- }
- /**
- * 取得student
- * @return student
- */
- public Student getStudent() {
- return student;
- }
- /**
- * 取得username
- * @return username
- */
- public String getUsername() {
- return username;
- }
- /**
- * 设置cat
- * @param cat cat
- */
- public void setCat(Cat cat) {
- this.cat = cat;
- }
- /**
- * 设置list
- * @param list list
- */
- public void setList(List<Student> list) {
- this.list = list;
- }
- /**
- * 设置map
- * @param map map
- */
- public void setMap(Map<String, Dog> map) {
- this.map = map;
- }
- /**
- * 设置password
- * @param password password
- */
- public void setPassword(String password) {
- this.password = password;
- }
- /**
- * 设置set
- * @param set set
- */
- public void setSet(Set<Dog> set) {
- this.set = set;
- }
- /**
- * 设置student
- * @param student student
- */
- public void setStudent(Student student) {
- this.student = student;
- }
- /**
- * 设置username
- * @param username username
- */
- public void setUsername(String username) {
- this.username = username;
- }
- }
ChainAction.java
- /**
- * OGNL表达式
- * @author 健
- */
- public class ChainAction extends ActionSupport{
- /**
- * 序列化
- */
- private static final long serialVersionUID = 1L;
- /**
- * 控制器
- * @return 跳转的页面
- */
- @Override
- public String execute(){
- return "success";
- }
- }
OGNLUtil.java
- /**
- * 工具类
- * @author 健
- */
- public class OGNLUtil {
- /**
- * 静态常量
- */
- public static final String URL = "static property";
- /**
- * 静态方法
- * @return 字符串
- */
- public static String getString(){
- return "static method";
- }
- }
Cat.java
- /**
- * 猫
- * @author 健
- */
- public class Cat {
- /**
- * 名字
- */
- private String name;
- /**
- * 朋友
- */
- private Dog friend;
- /**
- * 取得name
- * @return name
- */
- public String getName() {
- return name;
- }
- /**
- * 设置name
- * @param name name
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * 取得friend
- * @return friend
- */
- public Dog getFriend() {
- return friend;
- }
- /**
- * 设置friend
- * @param friend friend
- */
- public void setFriend(Dog friend) {
- this.friend = friend;
- }
- /**
- * 类方法
- * @return 返回字符串
- */
- public String say(){
- return "miaomiao";
- }
- /**
- * 构造
- */
- public Cat(){
- System.out.println("cat constructor");
- }
- /**
- * 重写toString方法
- */
- public String toString(){
- return "cat name is" + this.name;
- }
- }
Dog.java
- /**
- * 狗
- * @author 健
- */
- public class Dog {
- /**
- * 名字
- */
- private String name;
- /**
- * 构造
- */
- public Dog(){
- System.out.println("dog constructor");
- }
- /**
- * 构造
- */
- public Dog(String name){
- this.name = name;
- }
- /**
- * 取得name
- * @return name
- */
- public String getName() {
- return name;
- }
- /**
- * 设置name
- * @param name name
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * 类方法
- * @return 返回字符串
- */
- public String say(){
- return "miaomiao";
- }
- /**
- * 重写toString方法
- */
- public String toString(){
- return "dog name is" + this.name;
- }
- }
Student.java
- /**
- * 学生类
- * @author 健
- */
- public class Student {
- /**
- * 学号
- */
- private String no;
- /**
- * 年龄
- */
- private int age;
- /**
- * 构造方法
- */
- public Student(){
- System.out.println("进入学生类的构造方法");
- }
- /**
- * 构造方法
- */
- public Student(String no,int age){
- this.age = age;
- this.no = no;
- }
- /**
- * 取得no
- * @return no
- */
- public String getNo() {
- return no;
- }
- /**
- * 设置no
- * @param no no
- */
- public void setNo(String no) {
- this.no = no;
- }
- /**
- * 取得age
- * @return age
- */
- public int getAge() {
- return age;
- }
- /**
- * 设置age
- * @param age age
- */
- public void setAge(int age) {
- this.age = age;
- }
- /**
- * 重写toString方法
- */
- public String toString(){
- return "Student : " + this.no + " , " + this.age;
- }
- }
struts.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>
- <!-- 配置开发模式:修改不用重启服务器 -->
- <constant name="struts.devMode" value="true"/>
- <!-- 配置允许静态方法的访问 -->
- <constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
- <!-- 包含的文件 -->
- <include file="ognl.xml"></include>
- </struts>
ognl.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="ognl" namespace="/ognl" extends="struts-default">
- <action name="ognl" class="com.wj.struts2.action.OGNLAction" >
- <result name="success">/result.jsp</result>
- </action>
- <action name="chain" class="com.wj.struts2.action.ChainAction">
- <result type="chain" >
- <param name="actionName">ognl</param>
- <param name="namespace">/ognl</param>
- </result>
- </action>
- </package>
- </struts>
index.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib uri="/struts-tags" prefix="s" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>Struts2_OGNL</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
- -->
- </head>
- <body>
- <div>
- 访问基本元素
- <li><a href="ognl/ognl?username=wangjian&password=123456" mce_href="ognl/ognl?username=wangjian&password=123456">1.访问值栈中action的普通属性</a></li>
- <li><a href="ognl/ognl?student.age=20&student.no=0001" mce_href="ognl/ognl?student.age=20&student.no=0001">2.访问值栈中对象的普通属性</a></li>
- <li><a href="ognl/ognl?cat.friend.name=oudy" mce_href="ognl/ognl?cat.friend.name=oudy">3.访问值栈中对象(对象包含对象)的普通属性</a></li>
- <li><a href="ognl/ognl?cat.friend.name=oudy" mce_href="ognl/ognl?cat.friend.name=oudy">4.访问值栈中对象的普通方法</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">5.访问值栈中action的普通方法</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">6.访问静态方法</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">7.访问静态属性</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">8.访问Math类的属性</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">9.访问普通类的构造方法</a></li>
- </div>
- <br>
- <br>
- <div>
- 访问容器<br>
- (一)访问List<br>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">1.访问list</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">2.访问list中的某个元素</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">3.访问list中某个元素属性的集合</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">4.访问list中某个元素的属性</a></li><br>
- (二)访问Set<br>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">1.访问set</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">2.访问set中的某个元素(不可访问,set无顺序)</a></li><br>
- (三)访问Map<br>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">1.访问Map</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">2.访问Map中的某个元素</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">3.访问Map中的所有key</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">4.访问Map中的所有value</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">5.访问容器的大小</a></li>
- <br>
- </div>
- <br>
- <br>
- <div>
- 投影<br>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">1.通过投影访问list中年龄为25的学生的姓名</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">2.通过投影访问list中年龄大于25的集合中的首元素的姓名</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">3.通过投影访问list中年龄大于25的集合中的尾元素的姓名</a></li>
- <li><a href="ognl/ognl" mce_href="ognl/ognl">4.通过投影判断list中年龄大于25的集合是否为空</a></li><br>
- </div>
- <br>
- <br>
- <div>
- 栈<br>
- <li><a href="ognl/chain" mce_href="ognl/chain">用[]来访问栈中的元素,注意:[1]表示从栈中的开始位置进行遍历其中的元素</a></li>
- </div>
- </body>
- </html>
result.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib uri="/struts-tags" prefix="s" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>Struts2_OGNL</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
- -->
- </head>
- <body>
- 访问值栈中action的普通属性:<br>
- <li>username:<s:property value="username"/></li>
- <li>password:<s:property value="password"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问值栈中对象的普通属性<br>
- <li>student:<s:property value="student"/></li>
- <li>no:<s:property value="student.no"/></li>
- <li>age:<s:property value="student.age"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问值栈中对象(对象包含对象)的普通属性<br>
- <li>friendName:<s:property value="cat.friend.name"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问值栈中对象的普通方法<br>
- <li>method01:<s:property value="cat.friend.say().length()"/></li>
- <li>method02:<s:property value="cat.say()"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问值栈中action的普通方法<br>
- <li>method:<s:property value="execute()"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问静态方法<br>
- <li>static method:<s:property value="@com.wj.struts2.util.OGNLUtil@getString()"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问静态属性<br>
- <li>static property:<s:property value="@com.wj.struts2.util.OGNLUtil@URL"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问静态属性<br>
- <li>Math method:<s:property value="@@max(5,3)"/></li>
- <br>
- ----------------------------------------
- <br>
- 访问普通类的构造方法<br>
- <li>Constructer method:<s:property value="new com.wj.struts2.action.model.Dog()"/></li>
- <br>
- ----------------------------------------
- <br>
- <br>
- <br>
- 访问容器<br>
- (一)访问List<br>
- <li>1.访问list----<s:property value="list"/></li>
- <li>2.访问list中的某个元素----<s:property value="list[2]"/></li>
- <li>3.访问list中某个元素属性的集合----<s:property value="list.{no}"/></li>
- <li>4.访问list中某个元素的属性----<s:property value="list[2].no"/></li><br>
- (二)访问Set<br>
- <li>1.访问set----<s:property value="set"/></li>
- <li>2.访问set中的某个元素(不可访问,set无顺序)----<s:property value="set[1]"/></li><br>
- (三)访问Map<br>
- <li>1.访问Map----<s:property value="map"/></li>
- <li>2.访问Map中的某个元素----<s:property value="map['0001']"/></li>
- <li>3.访问Map中的所有key----<s:property value="map.keys"/></li>
- <li>4.访问Map中的所有value----<s:property value="map.values"/></li>
- <li>5.访问容器的大小----<s:property value="map.size()"/></li>
- <br>
- ----------------------------------------
- <br>
- <br>
- <br>
- <div>
- 投影<br>
- <li>1.通过投影访问list中年龄为25的学生的姓名----<s:property value="list.{?#this.age==25}[0].{no}[0]"/></li>
- <li>2.通过投影访问list中年龄大于25的集合中的首元素的姓名----<s:property value="list.{^#this.age>20}.{no}"/></li>
- <li>3.通过投影访问list中年龄大于25的集合中的尾元素的姓名----<s:property value="list.{$#this.age>20}.{no}"/></li>
- <li>4.通过投影判断list中年龄大于25的集合是否为空----<s:property value="list.{?#this.age>25}==null"/></li><br>
- </div>
- ----------------------------------------
- <br>
- <br>
- <br>
- <div>
- 栈<br>
- <li>用[]来访问栈中的元素,注意:[0]中0表示从栈中的开始位置进行遍历其中的元素----<s:property value="[1][0].execute()"/></li>
- </div>
- <s:debug></s:debug>
- </body>
- </html>
OGNL表达式的使用方法相关推荐
- 技术分析之OGNL表达式概述
1. OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写* 所谓对象图,即以任意一个对象为根,通过OGNL可以访问与这个对象关联的其它对象* 通过它 ...
- Struts2中使用OGNL表达式语言访问静态方法和静态属性以及我遇到的问题和解决方法
1.上一篇文章,我介绍了OGNL表达式语言,本来想在那篇文章上加上一个使用OGNL访问静态方法和静态属性的例子,但是昨天晚上我写代码时,报了一个警告,获取得到静态属性的值,却获取不到静态方法的返回值, ...
- 深入理解Struts2中的OGNL表达式
Struts 2中的表达式语言 Struts 2支持以下几种表达式语言: OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言: JST ...
- 为了熟练掌握动态SQL你必须要知道Mybatis中的OGNL表达式
前言 OGNL是个什么东西?很多刚入门Java的同学会有点陌生.但是在Structs流行的时代OGNL可是必会的数据渲染技术.它全称Object Graph Navigation Language,作 ...
- OGNL表达式语言中的#和$的区别
Struts2使用标准的Context来进行OGNL表达式语言求值,OGNL的顶级对象是一个Context,这个Context对象就是一个Map类型实例,其根对象就是 ValueStack,如果需要访 ...
- Struts(十):OGNL表达式(一)
Struts2 用s:porperty标签和OGNL表达式来读取值栈中的属性值: I.值栈中的属性值: 1.对象栈:读取对象栈中的某一个对象的属性值: 2.Map栈 :request,session, ...
- Struts2漏洞分析之Ognl表达式特性引发的新思路
摘要 在Ognl表达式中,会将被括号"()"包含的变量内容当做Ognl表达式执行.Ognl表达式的这一特性,引发出一种新的攻击思路.通过将恶意代码存储到变量中,然后在调用Ognl表 ...
- struts2教程(9)--OGNL表达式使用
OGNL表示式使用和值栈 一.介绍 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts2框架使用OGNL作为默认的 ...
- struts2的OGNL表达式(二)
下面说说OGNL表达式的访问情况: OGNL表达式能访问:访问属性,访问方法,访问字体对象,访问静态属性和方法,访问构造方法,访问数组,访问集合 – 投影.选择(? ^ $) 1,OGNL输出常量值 ...
最新文章
- 文件格式 tar.gz zip zp2 zip rar
- tomcat服务器配置及加固
- APDU命令的结构和处理【转】
- axis2 调用webservice
- 全国教学交流研讨会“教学为本”主题总结
- 面试题36:数组中的逆序对
- 2评分标准多少分_突发!财政部刚刚通知!2020年中级考试题目分值及评分标准大变!...
- opencv python 基于分水岭算法的图像分割
- 解决ubuntu下不能mount windows 盘的问题
- 原始图像扩展_Resize Sense for Mac(图像处理软件)
- java高级进阶面试题
- 使用 DISM 工具检查并修复 Windows 系统文件
- BUUCTF中的reverse1
- 蓝桥杯练习题——数列求和
- 2019浙江ACM省赛部分题解-ABDEFGHIJK
- 12.10中兴通讯科技园研发大楼发生42岁工程师跳楼事件
- win10开始菜单打不开,使用startmenu.diagcab进行修复
- 电脑桌面上怎么找计算机,电脑自带计算器快捷键 如何快速打开电脑上的计算器...
- 零售行业标签类目体系
- Linux上使用docker运行酷Q机器人