OGNL中文名对象图导航语言,是一个比EL表达式更加强大很多倍的语言
EL:从域对象中获取数据,从EL的11个对象中获取数据
OGNL:可以调用对象的方法,可以获取Struts2的值栈数据
public void demo1() throws OgnlException {
//获得context
OgnlContext context = new OgnlContext();
//获得根对象
Object root = context.getRoot();
//执行表达式
Object value = Ognl.getValue("'helloword'.length()", context, root);
System.out.println(value);
}
访问静态资源的表达式:
@类名@调用的方法
public void demo2() throws OgnlException {
//获得context
OgnlContext context = new OgnlContext();
//获得根对象
Object root = context.getRoot();
//执行表达式:@类名@方法名
Object value = Ognl.getValue("@java.lang.Math@random()", context, root);
System.out.println(value);
}
public void demo3() throws OgnlException {
//获得context
OgnlContext context = new OgnlContext();
//执行表达式
context.setRoot(new User("aaa","21231"));
//获得根对象
Object root = context.getRoot();
Object username = Ognl.getValue("username", context, root);
Object password = Ognl.getValue("password", context, root);
System.out.println(username+"..."+password);
}
public void demo4() throws OgnlException {
//获得context
OgnlContext context = new OgnlContext();
//获得根对象
Object root = context.getRoot();
context.put("name","张三");
Object value = Ognl.getValue("#name", context, root);
System.out.println(value);
}
导包
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:property value="'hello world'.length()">s:property>
配置struts.xml,修改常量struts.ognl.allowStaticMethodAccess的值,因为struts2默认是关闭静态资源访问的功能的
<constant name="struts.ognl.allowStaticMethodAccess" value="true">constant>
访问静态资源
<s:property value="@java.lang.Math@random()">s:property>
注意: 有的时候在struts.xml里配置常量的时候没有效果,我们也可以在web.xml里面配置常量的值
<filter>
<filter-name>strutsfilter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
<init-param>
<param-name>struts.ognl.allowStaticMethodAccessparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>strutsfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
查看值栈的结构:
<%@taglib prefix="s" uri="/struts-tags" %>
<s:debug>s:debug>
代码实现:
前端:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Titletitle>
head>
<body>
<s:debug>s:debug>
<s:property value="user.username">s:property>
<s:property value="user.password">s:property>
body>
html>
后端:
public class ValueStackDemo1 extends ActionSupport {
public User getUser() {
return user;
}
private User user;
@Override
public String execute() throws Exception {
user = new User("张三","2222");
return SUCCESS;
}
}
代码实现:
前端获取数据
<s:debug>s:debug>
<s:property value="username">s:property>
<s:property value="password">s:property>
<s:property value="name">s:property>
后端:
User user = new User("张三","2222");
ValueStack valueStack = ActionContext.getContext().getValueStack();
valueStack.push(user);
//set一般用来传集合
valueStack.set("name","李四");
后端存储数据:
ServletActionContext.getRequest().setAttribute("name","张三");
ServletActionContext.getRequest().getSession().setAttribute("name","李四");
ServletActionContext.getServletContext().setAttribute("name","王五");
前端获取数据(EL表达式可以获取到request对象的值):
<s:property value="#request.name">s:property>
<s:property value="#session.name">s:property>
<s:property value="#application.name">s:property>
${name}
获取context的数据
构建map集合
例如:
使用s:iterator 标签遍历list集合{‘aa’,‘bb’,‘cc’},结果表示使用i和#i都可以取到值
<s:iterator value="{'aa','bb','cc'}" var="i">
<s:property value="i">s:property> -- <s:property value="#i">s:property> <br />
s:iterator>
使用s:iterator 标签遍历map集合#{‘aa’:‘11’,‘bb’:‘22’,‘cc’:‘33’},结果表示使用key–value和#i.key–i.value都可以取到值
<s:iterator value="#{'aa':'11','bb':'22','cc':'33'}" var="i">
<s:property value="key">s:property> -- <s:property value="value">s:property> <br />
<s:property value="#i.key">s:property> -- <s:property value="#i.value">s:property> <br />
s:iterator>
demo
比较下面两个代码的区别:
<s:radio list="{'男','女'}" name="sex" label="性别" /> <br />
<s:radio list="#{'1':'男','2':'女'}" name="sex2" label="性别" /> <br>
强制解析成OGNL,在有些标签中是不识别OGNL的,需要使用%强制解析
demo:
<%
request.setAttribute("name","张三");
%>
姓名:<s:textfield name="text" value="%{#request.name}">s:textfield>
强制不解析OGNL表达式,在有些标签遇到OGNL表达式会自动解析,使用%可以让其不解析
<s:property value="%{'#request.name'}">s:property>
在配置文件中使用OGNL
属性文件:
demo
user.welcome=欢迎,${#session.user.username}
xml文件
demo
配置文件下载
<action name="download" class="xxx.DownloadAction">
<result type="stream">
<param name="Content-Type">文件类型param>
<param name="Content-Disposition">attachment;filename=${文件名}param>
result>
action>