Struts2——OGNL表达式

一、简介

OGNL(对象图导航语言:Object Graphic Navigation Language),是Struts2框架默认的表达式语言。
作用:取值,获取javaBean中的属性,获取List或者数组元素,获得Map的键值对,还可以执行逻辑运算。

二、OGNL三要素

1.表达式
2.根对象root
3.上下文context

三、例子

导入13个jar包
在这里插入图片描述

三个类,User为根对象,User类中包含group作为成员变量,Group类中包含branch作为成员变量,分别提供对应变量的get,set方法

package com.ognl;
public class Branch {
	private String branchid;
	public String getBranchid() {
		return branchid;
	}
	public void setBranchid(String branchid) {
		this.branchid = branchid;
	}
}
package com.ognl;
public class Group {
	private String groupid;
	private Branch branch;	
	public String getGroupid() {
		return groupid;
	}
	public void setGroupid(String groupid) {
		this.groupid = groupid;
	}
	public Branch getBranch() {
		return branch;
	}
	public void setBranch(Branch branch) {
		this.branch = branch;
	} 
}
package com.ognl;
public class User {
	private String userid;
	private Group group;
	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
}

测试OGNL表达式

package com.test;

import java.util.HashMap;
import java.util.Map;

import ognl.Ognl;
import ognl.OgnlException;

import com.ognl.Branch;
import com.ognl.Group;
import com.ognl.User;

public class OgnlTest01 {
	public static void main(String[] args) throws Exception {
		//Ognl.getValue("expession", context,root);
		//expession,context,root是Ognl表达式的三要素
		//expession可以是属性,方法,属性.属性,方法.方法,方法.属性,#context对象
		//root是根对象,可以通过根对象访问到包含对象及包含子对象的任意属性和方法
		//root是根对象,但不一定是顶层包含对象
		Branch branch=new Branch();
		Group group=new Group();
		User user=new User();
		user.setUserid("user123");
		user.setGroup(group);
		group.setGroupid("group123");
		group.setBranch(branch);
		branch.setBranchid("branch123");
		//访问属性
		System.out.println("userid="+Ognl.getValue("userid", user));
		System.out.println("groupid="+Ognl.getValue("groupid", group));
		//链式访问属性,前面是对象属性,后面是普通属性
		System.out.println("groupid="+Ognl.getValue("group.groupid", user));
		//group.branch.branchid中group属性是user根对象的属性,找到group对象,branch属性是group对象中的属性,找到branch对象,branchid是branch对象的属性
		System.out.println("branchid="+Ognl.getValue("group.branch.branchid", user));
		//访问方法
		System.out.println("userid="+Ognl.getValue("getUserid()", user));
		//链式访问方法,获取的前面是对象属性,后面是普通属性
		System.out.println("branchid="+Ognl.getValue("getGroup().getBranch().getBranchid()", user));
		
		//上下文环境context对象是一个Map类型的对象,在表达式中使用"#"对context对象集合中的对象进行访问
		//context对象里面存放的不一定是根对象,context可以放不同类型的对象
		User user1=new User();
		User user2=new User();
		Group group1=new Group();
		user1.setUserid("user1id123");
		user2.setUserid("user2id123");
		group1.setGroupid("groupid123");
		Map context=new HashMap();
		context.put("u1", user1);
		context.put("u2", user2);
		context.put("g1", group1);
		System.out.println("获取context对象中user1的userid结果为:"+Ognl.getValue("#u1.getUserid()", context,user1));
		System.out.println("获取context对象中user2的userid结果为:"+Ognl.getValue("#u2.getUserid()", context,user2));
		System.out.println("获取context对象中group1的groupid结果为:"+Ognl.getValue("#g1.getGroupid()", context,group1));
		
	}
	
	//访问静态方法和静态属性
	public static String staticvalue="staticvalue";
	public static void testStaticFun(){
		System.out.println("我是一个静态方法");
	}
}

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常量,开启静态方法,因为struts2默认是关闭这项支持的 -->
	<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
	<package name="struts2" namespace="/" extends="struts-default">
		<action name="valueStack1" class="com.test.ValueStackTest01">
			<result name="success">/valueStack1.jsp</result>
		</action>
		<action name="attrDriverValueStack" class="com.test.AttrDriverValueStack">
			<result name="success">/attrDriverValueStack.jsp</result>
		</action>
		<action name="modelDriverValueStack" class="com.test.ModelDriverValueStack">
			<result name="success">/modelDriverValueStack.jsp</result>
		</action>
	</package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值