1 OGNL overview
OGNL is the abbreviation of object graph navigation language. It is a powerful Expression Language (EL). Through its simple and consistent expression syntax, it can access object properties, call object methods, go through the structure diagram of the whole object, and realize field type conversion. It uses the same expression to access the properties of the object.
The name used in the name input box on the login page is the expression of OGNL, such as:
"account number:<input type="text" name="account">", Use on the welcome page“<s:property value="account"/>"
The two account expressions are the same, but the first one saves the value of the object attribute, and the latter one obtains the value of the object attribute. The expression language is simple, easy to understand, but powerful
2 basic use of ognl
2.1 constants and operators
In OGNL, you can use Java constant operations and mathematical operations. It should be noted that String constants can be enclosed in single quotation marks or double quotation marks, but single characters also use single quotation marks. Therefore, only when there is more than one character, the constants defined in single quotation marks are of String type. If you need to define only a String constant containing a single character, You need to use escape.
For example: < s: property value = '"account' / > the account in this sentence is a string because it is enclosed in single quotes.
Or < s: property value = "7 + 8" / > in this sentence, the output is 15, that is, the value after mathematical operation.
2.2 method call
OGNL supports method calls, which will be demonstrated later, such as accessing static methods, methods accessing collection objects, etc.
2.3 list of set values and expressions
OGNL allows you to execute multiple comma separated expressions in a single statement, and the return value of the last expression is the output of the whole statement.
2.4 accessing ValueStack using OGNL
In ognl, no prefix represents access to the current value stack. For example, < s: property value = "account" used in the welcome page in the previous example />In this sentence, the value of the value attribute of the < s: Property > tag is the ognl used. It does not have any prefix, which means that you can directly access the value stack. After accessing the value stack, you will find the first matching object in the order from the top of the stack to the bottom of the stack, and then you will find the account attribute in the Action and get the value
2.5 accessing ActionContext using OGNL
In ognl, you can access various values in ActionContext other than the value stack through symbols # such as:
- #Parameters: parameters in the current request, corresponding to request.getParameter(name)
- #Request: attribute in the request scope, corresponding to request.getAttribute(name)
- #Session: attribute in the session scope, corresponding to session.getAttribute(name)
- #Application: properties of the application scope
- #attr: returns the first qualified attribute in the order of page, request, session and application
When referencing, you need to prefix #, specify the range, and then write out which attribute to reference, such as: #paramters.account
Explanation of various symbols in struts 2
2.6 application examples
Here we will try this knowledge with a little modification on the login example.
Modify the action, and then set the attribute values of Session and Application. An example is as follows:
public class HelloWorldAction extends ActionSupport { private String account; private String password; private String submitFlag; public String execute() throws Exception { this.businessExecute(); ActionContext c = ActionContext.getContext(); c.getSession().put("account", "session Medium account"); c.getApplication().put("account", "application Medium account"); return "toWelcome"; } /** * An example method that represents a method that can perform business logic processing, */ public void businessExecute(){ System.out.println("The parameters entered by the user are==="+"account="+account+",password="+password+",submitFlag="+submitFlag); } //The getter/setter method corresponding to the property is omitted }
Modify the welcome page and obtain these values through OGNL on the page. An example is as follows:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <%@ taglib prefix="s" uri="/struts-tags"%> Welcome to<s:property value="account"/>My friends are visiting <br> Account number in request parameters:<s:property value="#parameters.account"/> <br> <%request.setAttribute("account","request of account");%> Account number in request properties:<s:property value="#request.account"/> <br> Account in session properties:<s:property value="#session.account"/> <br> Account number in application properties:<s:property value="#application.account"/> <br> attr Account number in:<s:property value="#attr.account"/> </body> </html>
Note: there is another slightly troublesome way to access attributes in ognl, such as #request ['account '], which is equivalent to using #request.account. When accessing more complex objects, the two methods can be mixed. General recommendations: if accessing object attributes, it is recommended to use "." to access; if accessing complex object structures such as Map, it is recommended to use [] The way to access
2.6.1 accessing static methods and static properties
In OGNL, static methods and static properties in any class can be accessed through the keyword @ in the form of @ full path name of class @ property name, or @ full path name of class @ method name (parameter list) It should be noted here that in the lower version of struts 2, the static methods of accessing classes are enabled by default, but in the higher version of struts 2, such as the version 2.1.8 used in this book, the static methods of accessing classes are disabled by default. In other words, to access the static methods of classes, you need to enable the settings. The configuration is very simple. Add struts.xml Enable the configuration of the static method of the access class. An example is as follows:
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
Write a class with a static method and a static attribute for external access. An example is as follows:
package cn.javass.hello.struts2impl.action; public class MyStatic { public static String staticTest = "staticTestValue"; // Static properties //Static method public static void testMethod() { System.out.println("+++++++++++++++++++static test method.++++++++++++++++++"); } }
Add a statement to access them in the welcome page. An example is as follows:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <%@ taglib prefix="s" uri="/struts-tags"%> Welcome to<s:property value="account"/>My friends are visiting <br> Static attribute values are:<s:property value="@cn.javass.hello.struts2impl.action.MyStatic@staticTest"/> <br> Call static method <s:property value="@cn.javass.hello.struts2impl.action.MyStatic@testMethod()"/> </body> </html>