search
HomeJavajavaTutorialJAVA: OGNL expression practice

JAVA: OGNL expression practice

Jun 26, 2017 am 11:23 AM
javaeestruts2studyexpression

1. OGNL expression

1. Introduction

OGNL: Object view navigation language. ${user.addr.name} This way of writing is called an object View navigation.
OGNL can not only view navigation, but also supports richer functions than EL expressions.

2. Use OGNL to prepare work

2.1 Import package

The struts2 package is already included, so there is no need to import additional jar packages

 2.2 Code preparation

 

##
    @Test//准备工作public void fun1() throws Exception{//准备OGNLContext//准备RootUser rootUser = new User("tom",18);//准备ContextMap<string> context = new HashMap<string>();
            context.put("user1", new User("jack",18));
            context.put("user2", new User("rose",22));
        OgnlContext oc = new OgnlContext();//将rootUser作为root部分        oc.setRoot(rootUser);//将context这个Map作为Context部分        oc.setValues(context);//书写OGNLOgnl.getValue("", oc, oc.getRoot());
    }</string></string>
Preparation work

 3. Basic syntax demonstration

        //取出root中user对象的name属性String name = (String) Ognl.getValue("name", oc, oc.getRoot());
        Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());
        System.out.println(name);
        System.out.println(age);
Get the attribute value in root
        //取出context中键为user1对象的name属性String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
        String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());
        Integer age = (Integer) Ognl.getValue("#user2.age", oc, oc.getRoot());
        System.out.println(name);
        System.out.println(name2);
        System.out.println(age);
Get the attribute value in context
        //将root中的user对象的name属性赋值Ognl.getValue("name='jerry'", oc, oc.getRoot());
        String name = (String) Ognl.getValue("name", oc, oc.getRoot());
        
        String name2 = (String) Ognl.getValue("#user1.name='郝强勇',#user1.name", oc, oc.getRoot());
        System.out.println(name);
        System.out.println(name2);
Assign a value to the attribute
        //调用root中user对象的setName方法Ognl.getValue("setName('lilei')", oc, oc.getRoot());
        String name = (String) Ognl.getValue("getName()", oc, oc.getRoot());
        String name2 = (String) Ognl.getValue("#user1.setName('lucy'),#user1.getName()", oc, oc.getRoot());
        
        System.out.println(name);
        System.out.println(name2);
Call method
        String name = (String) Ognl.getValue("@cn.itheima.a_ognl.HahaUtils@echo('hello 强勇!')", oc, oc.getRoot());//Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());Double pi = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());
        System.out.println(name);
        System.out.println(pi);
Call static method
        //创建list对象Integer size = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());
        String name = (String) Ognl.getValue("{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot());
        String name2 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}.get(1)", oc, oc.getRoot());    /*System.out.println(size);
        System.out.println(name);
        System.out.println(name2);*///创建Map对象Integer size2 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()", oc, oc.getRoot());
        String name3  = (String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc, oc.getRoot());
        Integer age  = (Integer) Ognl.getValue("#{'name':'tom','age':18}.get('age')", oc, oc.getRoot());
        System.out.println(size2);
        System.out.println(name3);
        System.out.println(age);
ognlCreate object-list|map

 

二、OGNL与Struts2的结合

 

  1.结合原理

  

  ValueStack中的两部分

  

 

  2.栈原理

  

  栈是由ArrayList模拟的

  

  栈中的两个方法的实现

  

  访问栈中属性的特点.由上到下

  

 

  3.查看值栈中两部分内容(使用DEBUG标签)

   3.1Root

  默认情况下,栈中放置当前访问的Action对象

  

  3.2Context

  

  

  Context部分就是ActionContext数据中心

 

   4.struts2与ognl结合体现

    4.1参数接收

     

  

    如何获得值栈对象,值栈对象与ActionContext对象是互相引用的

        //压入栈顶//1获得值栈ValueStack vs = ActionContext.getContext().getValueStack();//2将u压入栈顶vs.push(u);

    4.2配置文件中

<action><result><param>Demo1Action<param>/<!-- 如果添加的参数struts"看不懂".就会作为参数附加重定向的路径之后.
                     如果参数是动态的.可以使用${}包裹ognl表达式.动态取值                 --><param>${name}</result></action>
语法:${ognl表达式}

 

  5.扩展:request对象的getAttribute方法

   查找顺序:

  

 

三、练习:客户列表

    public String list() throws Exception {//1 接受参数String cust_name = ServletActionContext.getRequest().getParameter("cust_name");//2 创建离线查询对象DetachedCriteria dc =DetachedCriteria.forClass(Customer.class);//3 判断参数拼装条件if(StringUtils.isNotBlank(cust_name)){
            dc.add(Restrictions.like("cust_name", "%"+cust_name+"%"));
        }//4 调用Service将离线对象传递List<customer> list = cs.getAll(dc);//5 将返回的list放入request域.转发到list.jsp显示        //ServletActionContext.getRequest().setAttribute("list", list);// 放到ActionContextActionContext.getContext().put("list", list);        return "list";
    }</customer>
Action代码(新增ActionContext存放数据)
<iterator>
    <tr>
        <td>
            <property></property>
        </td>
        <td>
        <property></property>
        </td>
        <td>
        <property></property>
        </td>
        <td>
        <property></property>
        </td>
        <td>
        <property></property>
        </td>
        <td>
        <property></property>
        </td>
        <td>
        <a>修改</a>
          <a>删除</a>
        </td>
    </tr>
    </iterator>
    
    <tr>
        <td>
            <property></property>
        </td>
        <td>
        <property></property>
        </td>
        <td>
        <property></property>
        </td>
        <td>
        <property></property>
        </td>
        <td>
        <property></property>
        </td>
        <td>
        <property></property>
        </td>
        <td>
        <a>修改</a>
          <a>删除</a>
        </td>
    </tr>
     --%>
JSP显示数据的代码(注释的是另一种方法)

 注意: 每次都会把cust存在ActionContext中

 

The above is the detailed content of JAVA: OGNL expression practice. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools