上一篇算是看着学习了下ValueStack、ActionContext,如果还没明白他们之间是怎么回事,没关系,可以先学下Ognl,然后再回头看前面的可能就比较好理解了,我昨天也是一头雾水,现在把Ognl的学习了下,慢慢的有了感觉。

一、概念

OGNL表达式是(Object-Graph Navigation Language)是对象图形化导航语言。OGNL是一个开源的项目,struts2中默认使用OGNL表达式语言来显示数据。与serlvet中的el表达式的作用是一样的。OGNL表达式有下面以下特点:

1.支持对象方法调用,例如:objName.methodName();
2.支持类静态的方法调用和值访问,表达式的格式为
     @[类全名(包括包路经)]
     @[方法名 |  值名]
     例如:
         @java.lang.String@format('foo%s','bar')
         @tutorial.MyConstant@APP_NAME;
3支持赋值操作和表达式串联,例如: 
       price=100, discount=0.8, calculatePrice(),这个表达式会返回80;
 4.访问OGNL上下文(OGNL context)和ActionContext
 5.操作集合对象

二、实践

这里定义了Address、User类

package com.cyw.test;

public class Address {

    public Address(String city, String street) {
        super();
        this.city = city;
        this.street = street;
    }

    private String city;
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }
    private String street;
}
View Code

相关文章:

  • 2021-11-27
  • 2021-12-14
  • 2021-06-13
  • 2021-11-29
  • 2021-09-26
  • 2021-12-24
  • 2021-08-26
猜你喜欢
  • 2021-08-03
  • 2021-04-04
  • 2021-08-16
  • 2022-12-23
  • 2021-06-15
相关资源
相似解决方案