Struts2框架之OGNL表达式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ip_JL/article/details/82800540

OGNL(对象图导航语言)表达式

OGNL表达式是Struts2框架默认采用的表达式语言

OGNL的作用: 从JavaBean/List/数组/Map中取值

要求: 必须把OGNL表达式写在Struts的标签

form.jsp表单:

<%@ 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">
</head>
<body>
<!-- 在s:property的value属性是ognl的表达式
    如果要输出字符串的话 需要加上单引号
    <s:property value="'zhangsan'"/>
 -->
 
    <s:property value="'zhangsan'"/><br>
    
<!-- ognl对函数的调用
    可以直接使用java的api
 -->
    <s:property value="'zhangsan'.toUpperCase()"/><br>
    
<!-- 对静态方法和静态变量的调用
注意: 对静态方法的调用需要在"struts.xml"中开启"struts.ognl.allowStaticMethodAccess"
 -->
    <s:property value="@java.lang.Integer@MAX_VALUE"/><br>
    <s:property value="@java.lang.Math@abs(-100)"/><br>
    
</body>
</html>

"struts.xml"配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constant name="struts.devMode" value="true"></constant>
    
    <!-- 开启ognl对静态方法的调用
     -->
    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
    
</struts>

页面展示:

猜你喜欢

转载自blog.csdn.net/ip_JL/article/details/82800540