zoukankan      html  css  js  c++  java
  • OGNL in Struts2 Tutorial

     

    OGNL(Object-Graph Navigation language) is an expression language inherited by Struts from WebWork.

    Use of OGNL

    • OGNL is used in struts to access model objects from a jsp page.
    • It can be used to bind GUI elements to model objects in struts framework.
    • It can be used to invoke methods of the model.
    • It can create lists,maps to be used with GUI.
    • Bind generic tags with model objects.

    Value Stack

    Value Stack is an object created prior to any struts action method is executed and is used to store actions and other objects. The struts framework stores the value stack in request attribute named “struts.valueStack” and is used by jsps to display action and other info.

    Value Stack contains two logical units, the Object Stack and the Context Map.

    valuestack

    Action and other objects are pushed into the Object Stack where as maps (parameters, request, session, application, attr) are stored in Context Map.

    Maps in the Context Map

    1. parameters. Is a map containing the parameters in the current request.
    2. request. Is a map containing attributes in the current request.
    3. session. Is a map containing the session attributes for the current user.
    4. application. Is a map containing the ServletContext attributes.
    5. attr. Is a map that searches for attributes in the the order: request, session, application.

    OGNL can be used to access  objects in the Object Stack and the Context Map. To access Context Map properties we use a # in front of the OGNL expression, if not used search will take place from Object Stack.

    eg #session.code returns the value of the session attribute code.

    Accessing Object Stack object properties

    [0].message,[0][“message”],or [0][‘message’] returns the message property value of the object on top.
    
    [1].duration,[1].[“duration”],or [1][‘duration’] returns the duration property of the second object.
    
    To print the duration property of the first stack object we can use <s:property value=”[0].duration”/>
    

      

    Searching of property in Object Stack

    [1].message–>starts searching for the message property from the 1st object in stack if not found searches in the next object at index position[2] and goes on.

    [0].message is same as writing only message. In both cases the searching starts from the 1st object in the Value Stack.

    Reading Object Properties in the Context Map

    To access properties of Objects in the Context Map we can use any of the following forms.

    #object.propertyName
    
    #object[‘propertyName’]
    
    #object[“propertyName”]
    
     
    

      

    The folowing expression returns the firstName property of an employee object stored as the request attribute.

    #request[“employee”][“firstName”]

    The following expression will search for the lastAccessedTime attribute in the request object. If it doesn’t find in the request will search in the session and consequently in the application object.

    #attr[‘lastAccessedTime’]

    Accessing Methods an Fields using OGNL

    Methods and fields can be accessed through OGNL. e.g @java.util.Calendar@DECEMBER is used to access the static property DECEMBER in Calendar class. To call a static function we use @pac1.pac2.Util@now(); if we have a static function called now() in the Util class in pac1.pac2 package.

    To call a nonstatic field or function we use. object.fieldname or we can use [0].datepattern where [0] refers to the first object in the value stack.

  • 相关阅读:
    170601、单例模式的三种水平代码(第三种最佳)
    解决打开pycharm有带图片md文件卡死问题
    Dockerfile 操作
    Docker 命令大全
    MAC
    mac 搭建selenium与ChromeDriver环境
    Mac进行 usr/bin 目录下修改权限问题,operation not permitted
    pytest文档6-fixture之yield实现teardown
    pytest文档5-fixture之conftest.py
    pytest文档4-测试用例setup和teardown
  • 原文地址:https://www.cnblogs.com/hephec/p/4590601.html
Copyright © 2011-2022 走看看