Struts中的ognl

本期目标:了解struts的传值的优先级     

 

package com.wmy.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.wmy.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
 *子控制器
 */
public class Demo1Action extends ActionSupport implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
	
	private User user1=new User(); 
	
	private String uname;
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	
	public String list() throws Exception {
		System.out.println("list-----");
		System.out.println(user1);
		
		return "bookEdit";
	}
	
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user1;
	}
	
 
}

<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uname=ognl">set2方法传参</a><br>
 

 

一.ognl的简介 

OGNL的全称:Object Graph Navigation Language(对象图导航语言),

                         它是一种强大的表达式语言

OgnlContext(ongl上下文) 

 举例:其实就是Map (教室、老师、学生)

      教室:map
      OgnlContext=根对象(1)+非根对象(N)
      老师:跟对象    1
      学生:非根对象    n

注:context:英文原意上下文 

 我们在struts核心包里面,有依赖(freemarker,ognl)

 

二.Struts的传值

第一步:导入需要的资料(新建一个test包)

 第二步:OnglExpression:用于OGNL表达计算的一个工具类 

package com.wmy.test;
 
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
 
/**
 * 用于OGNL表达计算的一个工具类
 * 
 */
public class OnglExpression {
	private OnglExpression() {
	}
 
	/**
	 * 根据OGNL表达式进行取值操作
	 * 
	 * @param expression
	 *            ognl表达式
	 * @param ctx
	 *            ognl上下文
	 * @param rootObject
	 *            ognl根对象
	 * @return
	 */
	public static Object getValue(String expression, OgnlContext ctx,
			Object rootObject) {
		try {
			return Ognl.getValue(expression, ctx, rootObject);
		} catch (OgnlException e) {
			throw new RuntimeException(e);
		}
	}
 
	/**
	 * 根据OGNL表达式进行赋值操作
	 * 
	 * @param expression
	 *            ognl表达式
	 * @param ctx
	 *            ognl上下文
	 * @param rootObject
	 *            ognl根对象
	 * @param value
	 *            值对象
	 */
	public static void setValue(String expression, OgnlContext ctx,
			Object rootObject, Object value) {
		try {
			Ognl.setValue(expression, ctx, rootObject, value);
		} catch (OgnlException e) {
			throw new RuntimeException(e);
		}
	}
}

 第三步:打开demo1.java 

 

package com.wmy.test;
 
import ognl.OgnlContext;
import ognl.OgnlException;
 
public class Demo1 {
 
	/**
	 * @param args
	 * @throws OgnlException
	 */
	public static void main(String[] args)  {
		Employee e = new Employee();
		e.setName("小李");
 
		Manager m = new Manager();
		m.setName("张经理");
 
		// 创建OGNL下文,而OGNL上下文实际上就是一个Map对象
		OgnlContext ctx = new OgnlContext();
 
		// 将员工和经理放到OGNL上下文当中去
		ctx.put("employee", e);
		ctx.put("manager", m);
		ctx.setRoot(e);// 设置OGNL上下文的根对象
 
		/** ********************** 取值操作 *************************** */
		// 表达式name将执行e.getName(),因为e对象是根对象(请注意根对象和非根对象表达式的区别)
		String employeeName = (String) OnglExpression.getValue("name", ctx, e);
		System.out.println(employeeName);//小李
 
		// 表达式#manager.name将执行m.getName(),注意:如果访问的不是根对象那么必须在前面加上一个名称空间,例如:#manager.name
		String managerName = (String) OnglExpression.getValue("#manager.name",
				ctx, e);
		System.out.println(managerName);//张经理
 
		// 当然根对象也可以使用#employee.name表达式进行访问
		employeeName = (String) OnglExpression.getValue("#employee.name", ctx,
				e);
		System.out.println(employeeName);//小李
 
		/** ********************** 赋值操作 *************************** */
		OnglExpression.setValue("name", ctx, e, "小明");
		employeeName = (String) OnglExpression.getValue("name", ctx, e);
		System.out.println(employeeName);//小明
 
		OnglExpression.setValue("#manager.name", ctx, e, "孙经理");
		managerName = (String) OnglExpression.getValue("#manager.name", ctx, e);
		System.out.println(managerName);//孙经理
 
		OnglExpression.setValue("#employee.name", ctx, e, "小芳");
		employeeName = (String) OnglExpression.getValue("name", ctx, e);
		System.out.println(employeeName);//小芳
	}
 
}

展示效果:

三.值栈的使用

第一步:Demo7.java

package com.wmy.test;
 
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ognl.OgnlValueStack;
import com.opensymphony.xwork2.util.ValueStack;
 
public class Demo7 extends ActionSupport{
	/**
	 * 
	 * 值栈的使用
	 * 
	 */
	public String ognl1(){
		// 栈:表示一个先进后出的数据结构
		ValueStack vs = ActionContext.getContext().getValueStack();
		// push方法把项压入栈顶
		vs.push(new Employee("zs", 22));
		vs.push(new Employee("ls", 22));
		vs.push(new Employee("ww", 22));
 
		// pop方法移除栈顶对象并作为此函数的值返回该对象
		Employee e = (Employee) vs.pop();
		System.out.println(e.getName());//ww
		e = (Employee) vs.pop();
		System.out.println(e.getName());//ls
		e = (Employee) vs.pop();
		System.out.println(e.getName());//zs
	    return "bookEdit";
	}
 
	/**
	 * 此例用于模拟struts2的值栈计算过程
	 * 
	 * @param args
	 */
	public String ognl2(){
		ValueStack vs =ActionContext.getContext().getValueStack();
		vs.push(new Employee("张雇员", 2000));// 1
		vs.push(new Student("小明同学", "s001"));// 0
		System.out.println(vs.findValue("name"));
		System.out.println(vs.findValue("salary2"));
	    return "bookEdit";
 
	}
}

 第二步:struts-sy.xml配置

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="sy" extends="base" namespace="/sy">
	<action name="/demo7_*" class="com.pjl.test.Demo7" method="{1}">
		<result name="bookEdit">/bookEdit.jsp</result>
	</action>
	
	</package>
</struts>

第三步:值栈是怎么赋值与取值的

第一种方法:

先进后出(值栈的特点)

 如图效果:

 第二种方法:

值栈取值是从上至下的取到为止

注: Employee类有salary属性,但是Student类没有salary属性,从上往下取,取到为止

所以name为小明,salary为2000

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值