Struts2 (three) - Ognl value stack and

What is OGNL

OGNL is Object-Graph Navigation Language abbreviation, it is a powerful expression language, through its simple and consistent expression syntax, you can access any of the properties of the object, the object method calls to traverse the entire structure diagram objects, field type conversion functions to realize.

Comparison of the EL expression OGNL

  • OGNL object graph navigation language, many times more powerful than the EL expression language

  • EL expression can only get data from the domain

  • OGNL can call the object's methods, data acquisition value of the stack of the struts.

  • OGNL a third-party expression language, use it to get the data struts in the value stack

OGNL function

  • Support operators (e.g. + - * /)

  • Supports object method calls, such as xxx.doSomeSpecial ();

  • Support class static method calls and access value

  • Support the assignment and expressions series

  • Access context OGNL

  • Operation collection of objects

  • May direct a new target

OGNL use elements

  • expression

  • The root object

  • Context object (non-root object)

Getting OGNL

OGNL core OgnlContext, essentially a map

java program uses ognl

@Test
public void test(){
    OgnlContext ognlContext = new OgnlContext();
    ognlContext.setRoot("aaa");
    Object obj = Ognl.getRoot(ognlContext);
    System.out.println(obj);
}

Get Data root of

You can store Java objects to the root of them

@Test
public void test2() throws OgnlException {
    OgnlContext ognlContext = new OgnlContext();
    User user = new User();
    user.setUsername("Ryan");
    user.setAge(12);
    ognlContext.setRoot(user);
    Object root = ognlContext.getRoot();
    Object username = Ognl.getValue("username", ognlContext, root);
    System.out.println(username);
}

Only take root element stack, it is desirable field

Acquiring data in context

Non can not take root object field, need to add data fetch #

@Test
public void test3() throws OgnlException {
    OgnlContext ognlContext = new OgnlContext();
    User user = new User();
    user.setUsername("Ryan");
    user.setAge(12);
    ognlContext.put("UserRyan",user);
    Object root = ognlContext.getRoot();
    User obj = (User)Ognl.getValue("#UserRyan", ognlContext, root);
    System.out.println(obj.getUsername());
    System.out.println(obj.getAge());
}

Get Object method

Object value = Ognl.getValue("'Ryan'.length()", ognlContext, root);
System.out.println(value);

Gets the object static method

Object value1 = Ognl.getValue("@java.lang.Math@random()", ognlContext, root);
System.out.println(value1);

In jsp default method does not allow access to static, constant need to set struts.ognl.allowStaticEmthodAccessopen

Jsp used Ognl

<s:property value=""/>

What is the value of the stack

  • ValueStack is actually a container. Is an interface, implementation class is OgnlValueStack

  • Every time a user access to business method action objects, will create ActionContext object that contains OgnlValueStack object that contains the Action object

  • It is created by the Struts framework, such as the current page ends jsp sends a request, the default data Struts request interceptor will encapsulate incorporated into the stack ValueStack

  • Struts framework data are saved on to the ValueStack, which does not exist to the domain. Among the fields stored in the page can take it out, the stored data value stack which can be removed at any place, such as taken in the page, in the action taken, remove the configuration file.

  • ValueStack throughout the life cycle of action

  • action Once created, it will create a valuestack objects

  • When a request came, doFilter method for performing filter among StrutsPrepareAndExecuteFilter. In this method ActionContext create them. In ActionContext creation process, create ValueStack object. The valueStack ActionContext object to object. By ActionContext get the value of the stack object. The reason why the object to be ActionContext access the servlet API (data field object)

Analysis of the internal structure of stack value

  • Gets the value of the stack ValueStack valueStack = ActionContext.getContext().getValueStack();

  • Root zone: storing data related to the current Action

  • Non-root zone: The entire program-related data req session application

Value stack to store data

// valueStack.getRoot().push(new Object());
// valueStack.getRoot().pop();
valueStack.push(new Object());
valueStack.pop();
// to req
ActionContext.getContext().put("reqName","reqValue");
// to session
ActionContext.getContext().getSession().put("sessionName","sessionValue");
//to application

Value stack debugging

Constant struts

<constant name="struts.devMode" value="true" />

JSP

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

Guess you like

Origin www.cnblogs.com/mdz3201/p/12637511.html