OGNL Struts2 framework expressions quick literacy

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_41855420/article/details/102754440

A, OGNL Profile

OGNL is Object Graphic Navigation Language (object graph navigation language) acronym, is struts2 integration of an open source project, and Struts2 framework OGNL use as the default expression language. In struts2 in order to use OGNL expression, you must import Struts2 tag library.

OGNL EL expressions similar to the previously described function of the JSP, for fetching data from the main scope.
About the JSP EL expressions, refer to my blog EL expression in JSP Java Web of

Second, the simple use of OGNL

s:property类型于JSP的表达式,把value的值直接输出到页面
jsp表达式:【<jsp:setProperty property="" name=""/>】  	 
value属性的值就是一个OGNL表达式
如果想把value属性的值当字符串输出,加单引号即可

Here Insert Picture Description

<%--
  Created by IntelliJ IDEA.
  User: hestyle
  Date: 2019/10/26
  Time: 11:19 上午
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 使用OGNL表达式必须导入struts标签库 --%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>OGNL表达式</title>
</head>
<body>
    <%-- value属性表示需要取出的变量对应的名称 --%>
    <s:property value="OGNL"/><br>
    <%-- value属性中含有单引号,表示直接输出字符串"GNL表达式" --%>
    <s:property value="'OGNL表达式'"/>
</body>
</html>

Browser access index.jsp page
Here Insert Picture Description

Three, OGNL function

①、访问对象方法
②、访问静态属性
③、访问静态方法

Here Insert Picture Description

<%--
  Created by IntelliJ IDEA.
  User: hestyle
  Date: 2019/10/26
  Time: 11:19 上午
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 使用OGNL表达式必须导入struts标签库 --%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>OGNL表达式</title>
</head>
<body>
    <%-- value属性表示需要取出的变量对应的名称 --%>
    <s:property value="'OGNL表达式 长度'"/>
    <%-- value属性中含有单引号,访问字符串的length()方法 --%>
    <s:property value="'OGNL表达式'.length()"/><br>
    <%-- 获取Long包装类中的常量,最大值 --%>
    <s:property value="'long类型的最大值'"/>
    <s:property value="@java.lang.Long@MAX_VALUE"/><br>
    <%-- 获取Math类中的静态常量,π --%>
    <s:property value="'π = '"/>
    <s:property value="@java.lang.Math@PI"/><br>
    <%-- 获取Math类中的静态生成随机数方法 --%>
    <s:property value="'随机数'"/>
    <s:property value="@java.lang.Math@random()"/><br>
</body>
</html>

Here Insert Picture Description
Note meaning \color{red}注意: by OGNL access static method, the constant need tostruts.xmlturn this function.
Here Insert Picture Description

<!-- 开启ognl访问静态方法、常量的功能 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>

In the default default.propertiesfile is closed.
Here Insert Picture Description

④、封装list数据
⑤、封装Map数据

Here Insert Picture Description

<%--
  Created by IntelliJ IDEA.
  User: hestyle
  Date: 2019/10/26
  Time: 11:19 上午
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 使用OGNL表达式必须导入struts标签库 --%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>OGNL表达式</title>
</head>
<body>
    <!-- label属性是浏览器显示的标签,name是表单后端的名称(如果是表单) -->
    <%-- 封装一个list --%>
    <s:radio list="{'女','男'}" label="性别" name="gender"/><br>
    <%-- 封装一个map(注意有一个#号) --%>
    <s:radio list="#{'true':'已婚','false':'未婚'}" label="是否已婚" name="married"/><br>
    <s:checkboxlist list="#{'编码':'编码','撩妹':'撩妹','开车':'开车'}" label="爱好" name="hobbies"/><br>
</body>
</html>

Here Insert Picture Description

Fourth, the use OGNL expression in struts.xml

In a previous blog files downloaded Struts2 framework demonstrates a dynamic set download filename case.
Here Insert Picture DescriptionHere Insert Picture DescriptionModify the configuration file is called java.net.URLEncoderstatic methods of the class encode().
Here Insert Picture Description

Here Insert Picture Description Note meaning \color{red}注意: using OGNL expression format in struts.xml in${变量名}, like JSP EL expressions in a hair.

Guess you like

Origin blog.csdn.net/qq_41855420/article/details/102754440