《刨根问底--action属性赋值过程分析》概要的分析了action赋值的需要通过ognl,由于前面的文章中没有分析过ognl,如果上来直接分析struts2是怎么使用,嘿嘿,相信没有看过ognl的人,直接晕掉。这篇文章就先舍去struts2,单独来分析ognl。
测试用例:
import ognl.Ognl;
import ognl.OgnlException;
public class OgnlTest
{
public static void main(String[] args)
{
/* 创建一个Person对象 */
Person person = new Person();
person.setName("zhangsan");
try
{
/* 从person对象中获取name属性的值 */
//parseExpression(expression)
// Ognl.setValue(expression, person, "zhangsan");
String expression = "name";
Object value = Ognl.getValue(expression, person);
System.out.println(value);
}
catch (OgnlException e)
{
e.printStackTrace();
}
}
}
class Person
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
注释:这个类中简单的几行代码包含了ognl获取数据的代码和设置数据的代码。现在重点分析获取数据,设置数据后面的文章分析。
(1)创建了一个Person对象,并且设置属性name为zhangsan
(2)Ognl.getValue()获取person对象中name对应的值
下面贴出整个流程图:
1、Ognl.getValue(expression, person)代码:
public static Object getValue( String expression, Object root ) throws OgnlException
{
return getValue( expression, root, null );
} 注释:方法很简单,增加一个参数,并且调用getvalue方法。
2、getValue( expression, root, null )代码:
public static Object getValue( String expression, Object root, Class resultType ) throws OgnlException
{
return getValue( parseExpression(expression), root, resultType );
} 注释:解析表达式 expression,parseExpression(expression)代码:
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-32760-1.html
打
真正重要的东西用眼睛是看不见的
这种酒就是骗骗小孩子的