之前说了访问值栈中普通对象的属性。今天简单说一下访问action的属性的方法,访问静态的属性,访问静态的方法,和访问Math类。先看一下项目构成。
各种访问的方法都在ognl.jsp里。
ognl.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>Ognl表达式学习</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <ol> <li>访问值栈中的action的普通属性:username=<s:property value="username"/></li> <li>访问值栈中对象的普通属性:<s:property value="user.age"/></li> <li>访问值栈中对象的普通属性:<s:property value="cat.friend.name"/> <hr/> <li>访问action的属性的方法:<s:property value="password.length()"/> <li>访问静态的属性:<s:property value="@com.tfj.struts2.ognl.S@STR"/> <li>访问静态的方法:<s:property value="@com.tfj.struts2.ognl.S@s()"/> <li>访问Math类:<s:property value="@@max(2,3)"/> </ol> <s:debug></s:debug> </body> </html>
这部分都比较简单,我就不详述了,值得注意一下的是在访问静态方法的时候,需要在struts.xml里配置一下 <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>允许访问静态方法。
<s:property value="@@max(2,3)"/>是只能来访问Math类。