一、OGNL表达式
1、java环境使用OGNL
public class OGNLDemo1 {
@Test
public void demo1() throws OgnlException {
// 获得context
OgnlContext context = new OgnlContext();
// 获得根对象
Object root = context.getRoot();
// 执行表达式
// 1、调用对象方法
Object value = Ognl.getValue("'nihao'.length()", context, root);
// 2、调用对象静态方法
Object value2 = Ognl.getValue("@java.lang.Math@random()", context, root);
System.out.println(value2.toString());
}
@Test
public void demo2() throws OgnlException {
OgnlContext context = new OgnlContext();
User user = new User("zhangsan ", 21, "male");
// 向root中存入数据
context.setRoot(user);
Object root = context.getRoot();
Object username = Ognl.getValue("username", context, root);
// OGNL获取root中的数据
Object age = Ognl.getValue("age", context, root);
Object sex = Ognl.getValue("sex", context, root);
System.out.println(username.toString()+" "+age.toString()+" "+sex.toString());
}
@Test
public void demo3() throws OgnlException {
// 获得context
OgnlContext context = new OgnlContext();
// 获得根对象
Object root = context.getRoot();
// 向context里存入数据
context.put("name", "wangqiang");
// 执行表达式
// OGNL获取context里的数据
Object value = Ognl.getValue("#name", context, root);
System.out.println(value.toString());
}
}
2、struts标签环境使用OGNL
*注意:
要引入struts标签库
<%@ taglib uri="/struts-tags" prefix="s" %>
访问静态方法要修改静态常量值(在struts.xml中配置)
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
二、值栈
1、valuestack的获得和操作
jsp页面:
action代码:
/**
* 操作valuestack
* 方式一获取:利用action在值栈中的特性
* @author xuyao
*
*/
public class ValueStackDemo3 extends ActionSupport {
private User user;
@Override
public String execute() throws Exception {
// 方式一、通过ActionContext获得
ValueStack valueStack = ActionContext.getContext().getValueStack();
// 方式二、通过request获得
ValueStack valueStack2 = (ValueStack) ServletActionContext.getRequest()
.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
user = new User("zhangsan ", 21, "male");
return "success";
}
public User getUser() {
return user;
}
}
/**
* 操作valuestack
* 方式二获取:调用值栈本身的方法
* @author xuyao
*
*/
public class ValueStackDemo4 extends ActionSupport {
@Override
public String execute() throws Exception {
ValueStack valueStack = ActionContext.getContext().getValueStack();
User user = new User("lisi", 31, "male");
// user在栈顶的位置
valueStack.push(user);
// valueStack.set("name", "wangwu");
return "success";
}
}
通过ActionContext获得的方式更常用
方式二获取:调用栈中的对象获得更常用
2、获得valuestack中的数据
(1)对象方式写入
<!-- 方式一获取:利用action在值栈中的特性 -->
<s:property value="user.username"/>
<s:property value="user.age"/>
<s:property value="user.sex"/>
<!-- 方式二获取:调用栈中的对象获得-->
<s:property value="username"/>
<s:property value="age"/>
<s:property value="sex"/>
<s:property value="name"/>
(2)多个对象封装
<s:property value="list[0].username"/>
<s:property value="list[0].age"/>
<s:property value="list[0].sex"/><br>
设置方式:
(3)servlet原始方式
<s:property value="#request.name1"/>
<s:property value="#session.name2"/>
<s:property value="#application.name3"/>
设置方式:
3、el获取值栈的数据
el也可以获取值栈的数据,获取方式为:
${值的名称}
4、ONGL的特殊字符
#号:
构建map集合
<s:radio list="#{'1':'男','2':'女'}" name="sex" label="性别"></s:radio>
获取context数据
<s:property value="#request.name"/>
%号:
强制解析OGNL
<%
request.setAttribute("name", "marry");
%>
<s:textfield name="username" value="%{#request.name}" label="姓名"></s:textfield>
request的值只能用s:property标签显示,但是我们想让数据回显,就要用%强制解析
$号:
用在.property和.xml文件中,存在域中的数据只能在servlet中获得,值栈对此功能作了改善,使用$符号,在配置文件中也可获得动态数据