ognl表达式系列3--ognl在struts2环境的基本使用

本文详细介绍了在Struts2框架中如何使用OGNL表达式来访问对象的方法及静态方法。主要内容包括启用OGNL对静态方法的访问、配置示例以及如何在页面上显示静态常量和调用静态方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引用自:https://www.cnblogs.com/ends-earth/p/10714068.html

jsp中需要引入ognl标签库才能用

<%@ taglib uri="/struts-tags" prefix=“s” %>

1)访问对象中的方法

<%@taglib uri ="/struts-tags" prefix="s"%>




2.访问静态方法
struts2中默认静态访问方法是关闭的(默认设置在struts2-core-xxx.jar 包中的 /org/apache/struts2---->default.properties文件中,内容如下)

### Whether to allow static method access in OGNL expressions or not
struts.ognl.allowStaticMethodAccess=false

所以首先需要在struts-xxx.xml中添加如下配置:

<struts>
    <!-- 开启静态方法访问常量 -->
    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
</struts>
<!-- 类的静态常量的访问 -->
PI = <s:property value = "@java.lang.Math@PI"/><br> 
<!-- 需要设置 struts.ognl.allowStaticMethodAccess = true,才可以直接使用类的静态方法 -->
random = <s:property value = "@java.lang.Math@random() * 100"/><br> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值