zoukankan      html  css  js  c++  java
  • Struts_OGNL(Object Graph Navigation Language) 对象图导航语言

    1、访问值栈中的action的普通属性:

    请求:

     1 <a href="ognl.action?username=u&password=p">访问属性</a> 

    对应的action代码,getter和setter方法一定要加上:

     1     private String username;
     2     
     3     private String password;
     4 
     5         public String getUsername() {
     6         return username;
     7     }
     8 
     9     public void setUsername(String username) {
    10         this.username = username;
    11     }
    12 
    13     public String getPassword() {
    14         return password;
    15     }
    16 
    17     public void setPassword(String password) {
    18         this.password = password;
    19     }
    View Code

    页面进行访问:

    1 username = <s:property value="username" /> | password = <s:property value="password" />

    2、访问值栈中对象的普通属性(get set方法)

    如果请求没有将 user的值传过去,那么在值栈就会看到 user=null,只有为user赋值了,struts2才会构造一个User对象然后赋给user。

    只有传才会构造

    想初始化 domain model,可以 new,也可以传参数值,但这时候需要提供参数为空的构造方法。

    请求:

    action 中的代码:

    1         private User user;
    2 
    3     public User getUser() {
    4         return user;
    5     }
    6 
    7     public void setUser(User user) {
    8         this.user = user;
    9     }
    View Code

    属性访问:

     1 <s:property value="user.age" /> 

    3、案例三

    现在定义了 Dog 类:

     1 package com.bjsxt.struts2.ognl;
     2 
     3 public class Dog {
     4     
     5     private String name;
     6     
     7     public Dog(){}
     8     
     9     public Dog(String name){
    10         this.name = name;
    11     }
    12     
    13     public String getName() {
    14         return name;
    15     }
    16     public void setName(String name) {
    17         this.name = name;
    18     }
    19     
    20     public String toString(){
    21         return "dog:" + this.name;
    22     }
    23     
    24 }
    View Code

    Cat 类,类中有个friend 是 Dog 类

     1 package com.bjsxt.struts2.ognl;
     2 
     3 public class Cat {
     4     
     5     private Dog friend;
     6 
     7     public Dog getFriend() {
     8         return friend;
     9     }
    10 
    11     public void setFriend(Dog friend) {
    12         this.friend = friend;
    13     }
    14     
    15     public String miaomiao(){
    16         return "miaomiao";
    17     }
    18 }
    View Code

    在 Action 中定义了 Cat 对象

    1     private Cat cat;
    2 
    3     public Cat getCat() {
    4         return cat;
    5     }
    6 
    7     public void setCat(Cat cat) {
    8         this.cat = cat;
    9     }
    View Code

    请求:

    显示:

    4、案例四:OGNL表达式调用值栈中对象的方法

    action中是有定义password属性的。

    类似案例:

    Cat类中是由miaomiao()方法的。

    5、调用action的普通方法

    action 先定义一个普通方法:

    public String m(){
    return "hello";
    }

    页面调用:

     1 <s:property value="m()"/> 

    ========================================================================

    访问静态方法:

    先在struts.xml设置允许访问静态方法

     1 <constant name="struts.ognl.allowStaticMethodAccess" value="true"/> 

    定义的示例类:

     1 package com.bjsxt.struts2.ognl;
     2 
     3 public class S {
     4     
     5     public static String STR = "STATIC STRING";
     6     
     7     public static String s(){
     8         return "this is a static method().";
     9     }
    10     
    11 }
    View Code

    访问代码:

    1 访问静态方法:<s:property value="@com.bjsxt.struts2.ognl.S@s()"/>
    2 访问静态变量:<s:property value="@com.bjsxt.struts2.ognl.S@STR"/>
    3 访问Math类的静态方法:<s:property value="@@max(2,3)"/>

    6、访问普通类的构造方法

     1 访问普通类的构造方法:<s:property value="new com.bjsxt.struts2.ognl.User(8)" /> 

    结果:

    7、访问集合元素

    数组访问方式跟访问List一样 

    8、投影

    User 代码:

     1 package com.bjsxt.struts2.ognl;
     2 
     3 public class User {
     4     
     5     private Integer age = 10;
     6     
     7     public User(){}
     8     
     9     public User(int age){
    10         this.age = age;
    11     }
    12 
    13     public Integer getAge() {
    14         return age;
    15     }
    16 
    17     public void setAge(Integer age) {
    18         this.age = age;
    19     }
    20     
    21     public String toString(){
    22         return "user" + age;
    23     }
    24     
    25 }
    View Code

    名称为 users 的List

    1 private List<User> users = new ArrayList<User>();
    2 users.add(new User(1));
    3 users.add(new User(2));
    4 users.add(new User(3));
    5 users.add(new User(4));
    View Code

    页面代码:

    1 <li>投影(过滤):<s:property value="users.{?#this.age==1}.{age}"/></li><!-- ?#后面接过滤条件 这句话是取所有age大于1的所有值 -->
    2 <li>投影:<s:property value="users.{^#this.age>1}.{age}" /></li><!-- ^#开头 这句话是取age大于1的第一个值 -->
    3 <li>投影:<s:property value="users.{$#this.age>1}.{age}" /></li><!-- $#结尾 这句话是取age大于1的最后那个值-->
    4 <li>投影:<s:property value="users.{^#this.age>1}.{age} == null" /></li>

    结果:

    9、[]

    [] 访问的是值栈

    [索引] 访问对应位置的对象

    [索引].对象名 访问索引对象包含的Property

     1 <s:property value="[0].locale"/> 

    小问题:一个请求从一个Action容器内跳转到另一个Action,那么在值栈中就会有两个Action。例子如下:

    struts.xml

    TestAction

     1 package com.bjsxt.struts2.ognl;
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 
     5 public class TestAction extends ActionSupport {
     6 
     7     private static final long serialVersionUID = 7420682335394931465L;
     8 
     9     public String execute(){
    10         return SUCCESS;
    11     }
    12 }
    View Code

    OgnlAction

     1     public OgnlAction(){
     2         users.add(new User(1));
     3         users.add(new User(2));
     4         users.add(new User(3));
     5         users.add(new User(4));
     6         
     7         dogs.add(new Dog("dog1"));
     8         dogs.add(new Dog("dog2"));
     9         dogs.add(new Dog("dog3"));
    10         
    11         dogMap.put("dog100", new Dog("100"));
    12         dogMap.put("dog101", new Dog("101"));
    13         dogMap.put("dog102", new Dog("102"));
    14     }
    15     
    16     public String execute() throws Exception {
    17         return super.execute();
    18     }
    View Code

    调用了两个Action后,ValueStack中的内容 TestAction 先进栈,OgnlAction 后进栈:

    OGNL 常用的就上面这些。

    链接: http://pan.baidu.com/s/1c8Hima 密码: gkch

  • 相关阅读:
    安装adobe,路径My Pictures或卷无效。请重新输入。
    PrintDocument打印、预览、打印机设置和打印属性的方法(较完整) .
    C# 生成CODE128条码
    SQL2005 安装时 “性能监视器计数器要求(错误)” 解决方案
    Siebel escript学习笔记
    siebel 界面搭建
    Siebel Tools 开发笔记
    Siebel Tools配置
    Oracle:environment variable "PATH" does not exceed the recommended length
    IOS开发入门实例
  • 原文地址:https://www.cnblogs.com/ShawnYang/p/6676811.html
Copyright © 2011-2022 走看看