栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

SSH之ognl

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SSH之ognl

一个例题开始今天的学习:

问题:哪个uname获得了ognl这个值?①、user1.uname  ②、uname

package com.mwy.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.mwy.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class Demo1Action extends ActionSupport implements ModelDriven,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;
	}
	

}

ognl
Modeldriver接口传参;

结果:①、user1.uname 获得了

今天目标:

了解Struts的传值的优先级;

一、ognl 1、ognl是什么?

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

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

OgnlContext(ongl上下文)其实就是Map (教室、老师、学生)

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

注:context:英文原意上下文,环境/容器

2、Struts的传值的初步了解

①、导入需要使用的类

 ②、Onglexpression:用于OGNL表达计算的一个工具类

       (1)getValue:获取值

       (2)setValue:取值

 

package com.mwy.test;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

public class Onglexpression {
	private Onglexpression() {
	}
	
	public static Object getValue(String expression, OgnlContext ctx,
			Object rootObject) {
		try {
			return Ognl.getValue(expression, ctx, rootObject);
		} catch (OgnlException e) {
			throw new RuntimeException(e);
		}
	}
	
	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.mwy.test;
import ognl.OgnlContext;
import ognl.OgnlException;
public class Demo1 {
	
	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);
	}
}

④、结果展示:

3、 值栈的使用

①、Demo7

package com.mwy.test;
import com.opensymphony.xwork2.ActionContext;
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());
		e = (Employee) vs.pop();
		System.out.println(e.getName());
		e = (Employee) vs.pop();
		System.out.println(e.getName());
		return "bookEdit";
	}

	
	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"));

		ActionContext ac = ActionContext.getContext();
		return "bookEdit";
	}
}

②、struts-sy.xml中配置




	

	
	         /bookEdit.jsp
	    
	    
	

③、值栈是的特点:先进后出

 结果展示

④、值栈是怎么赋值与取值的

符合先进后出的特点:如果不是同一个对象,执行第二个方法;

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

 

Employee类有salary属性,但是Student类没有salary属性,运行时张雇员在底部,小明在顶部

要打印salary从Student类开始拿值,没有就继续往下直到拿到为止,所以就会有下面结果

salary为张雇员的2000,但是name为小明同学

结果展示

strust2中ognl结构图

举例图: 

 

 今天到这里就结束了~~

掰掰

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/320824.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号