OGNL表达式(转载)

时间:2023-03-09 18:22:46
OGNL表达式(转载)

OGNL表达式(转载)

1.什么是OGNL

OGNL:Object Graphic Navigation Language(对象图导航语言)

它是Struts2中默认的表达式语言。使用表达式需要借助Struts2的标签.

OGNL与EL的区别:

EL/JSTL 数学运算等等表达式: 都有一个明确的返回值,都只能取值。

     OGNL表达式: 它不仅可以取值,还可以赋值(赋值操作只能由Struts来完成)

OGNL表达式的写法:   属性.属性.属性的方式   <input type="text" name="user.user"/>

OGNL表达式的基本使用:

  • 使用s:property标签输出内容
  • 访问普通方法
  • 访问静态方法
  • 访问静态属性
  • 操作集合(List AND Map)
OGNL表达式(转载)
<title>Struts2OGNL表达式的基本使用</title>
</head>
<body>
<!-- a、使用s:property标签输出内容:要想输出内容到页面得使用Struts2的标签库
value属性:把value取值所对应的内容输出到页面
若想直接输出文本,则需要用引号引起来。
-->
输出基本内容:<s:property value="'OGNLExpresession'"/>
<hr/>
<!-- b、访问普通方法 -->
输出字符串的长度:<s:property value="'OGNLExpresession'.length()"/> <br/>
输出转大写字符:<s:property value="'OGNLExpresession'.toUpperCase()"/> <br/>
切割字符串:<s:property value="'OGNLExpresession'.split('L')"/>
<hr/>
<!-- c、访问静态属性
语法 :
@类的全路径@静态字段
-->
输出整数的最大值:<s:property value="@java.lang.Integer@MAX_VALUE"/>
<hr/>
<!-- d、访问静态方法
语法:
@类的全路径@静态方法
注意:
使用静态方法时,需要在struts.xml中开启静态方法调用的开关。
-->
输出一个随机数:<s:property value="@java.lang.Math@random()"/>
<hr/>
<!-- e、操作集合(List和Map)
语法:
声明list集合:{"aa","bb"}
声明map集合:#{"key":"value","key":"value"}
-->
输出html普通标签的单选按钮:<br/>
<input type="radio" name="sex" value="男"/>男
<input type="radio" name="sex" value="女"/>女<br/>
使用Struts2标签输出单选按钮:list集合方式<br/>
<s:radio list="{'男','女'}" name="sex"></s:radio><br/>
使用Struts2标签输出单选按钮:map方式<br/>
<s:radio list="#{'男':'男','女':'女' }" name="sex"/> </body>
OGNL表达式(转载)
OGNL表达式(转载)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
使用EL表达式:<br/>
${c.username}<br/>
${requestScope.p }<br/>
${sessionScope.p }<br/>
${applicationScope.p }<br/>
<hr/>
使用OGNL表达式:<br/>
<!-- 如果是在action动作类中直接声明的变量,OGNL直接取值即可。 -->
<!-- 在域对象中的数据需要使用#开头 -->
<!--
注:如果对象在action动作类中直接声明,则取值需要使用c.username
如果对象在方法中手动添加到valueStack对象中,则取值不需要c.username,直接username即可。
-->
<s:property value="c.username"/><br/>
<%-- <s:property value="[1].username"/><br/> --%>
<s:property value="#request.p"/><br/>
<s:property value="#session.p"/><br/>
<s:property value="#application.p"/><br/>
<s:property value="#attr.p"/><br/>
<hr/>
<!-- 不写value属性:获得的是栈顶数据 -->
<s:property/>
<s:debug/>
</body>
</html>
OGNL表达式(转载)
OGNL表达式(转载)
<%@page import="com.opensymphony.xwork2.util.ValueStack"%>
<%@page import="com.opensymphony.xwork2.ActionContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 在页面查看调试信息 -->
${ username}<br/>
<s:debug/>
<s:property value="username"/><br/> == vs.findValue("username");
<s:property value="[1].username"/><br/>
<s:property value="#session.p"/><br/>
<s:property value="#application.p"/>
<hr/>
<% ActionContext ac = ActionContext.getContext();
ValueStack vs = ac.getValueStack();
String name = (String)vs.findValue("username");
out.print(name);
String p = (String)vs.findValue("#session.p");
out.print(p);
%>
</body>
</html>
OGNL表达式(转载)