OGNL 表达式
- ognl 表达式: 它可以访问普通方法,s:property 它就类似于jsp 的表达式(在页面上打印值)
${}
<%= %>
<s:property value="OGNL-Expression"/>
value 属性中的内容不再是我们看到字符串,它是一个OGNL 表达式(!)
如果想要让它变成一个普通的字符串,在值外面套上单引号
<s:property value="'OGNL-Expression'"/>
- 在struts.xml 中使用ognl 表达式${} 在括号中写ognl 表达式
- OGNL 访问静态属性:@全类名@静态属性名称
<s:property value="@java.lang.Integer@MAX_VALUE"/>
- OGNL 访问静态方法:@全类名@静态方法名,在struts2 框架中默认是禁用静态方法调用的,我们可以通过在struts2 中配置开启。
//在struts.xml 中添加
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
访问静态方法
<s:property value="@java.lang.Math@random()"/>
- OGNL 和List
<s:radio name="gender" list="{'man','women'}" />
{}相当用创建了一个List集合,List属性中的取值是一个OGNL 表达式
- OGNL 和 map
<s:radio name="gender1" list="#{'1':'男','0':'女'}"/>
#{} 表示创建了一个map,写法和json数据的格式相似(1为Key ,’男’为Value),值作为Radio 到页面显示的内容,Key 做为Radio 的Value