欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Struts2 OGNL表达式实例详解

程序员文章站 2024-03-03 15:22:34
object graph navigation language:对象图导航语言,就是用点来访问成员变量

object graph navigation language:对象图导航语言,就是用点来访问成员变量

<s:property value="cat.name"/> 

例1:

struts.xml:

<package name="ognl" namespace="/ognl" extends="struts-default"> 
  <action name="og1" class="cn.edu.hpu.action.ognlaction1"> 
  <result>/ognl.jsp</result> 
  </action> 
</package> 

ognlaction1.java:

package cn.edu.hpu.action; 
import com.opensymphony.xwork2.actionsupport; 
public class ognlaction1 extends actionsupport{ 
  private string username; 
  private string password; 
  public string getusername() { 
    return username; 
  }  
  public void setusername(string username) { 
    this.username = username; 
  } 
  public string getpassword() { 
    return password; 
  } 
  public void setpassword(string password) { 
    this.password = password; 
  } 
  @override 
  public string execute() throws exception { 
    // todo auto-generated method stub 
    return super.execute(); 
  } 
} 

访问链接:

<a href="<%=basepath%>ognl/og1?username=jack&&password=111" rel="external nofollow" >ognl</a><br/> 

结果界面:

ognl success!!<br/> 
  <ol> 
    <li>访问值栈中的action的普通属性1=<s:property value="username"/></li> 
    <li>访问值栈中的action的普通属性2=<s:property value="password"/></li> 
  </ol> 
  <s:debug></s:debug> 

结果显示:

ognl success!!

访问值栈中的action的普通属性1=jack

访问值栈中的action的普通属性2=111

[debug]
ps:点击[debug]可以查看到栈值中有username与password

例2:

struts.xml:

<package name="ognl" namespace="/ognl" extends="struts-default"> 
 <action name="user2" class="cn.edu.hpu.action.useraction2" method="add"> 
   <result name="success">/ognl.jsp</result> 
  </action> 
</package> 

useraction2.java:

package cn.edu.hpu.action;  
import cn.edu.hpu.mode.user; 
public class useraction2 {  
  private user user;    
  public string add(){ 
    system.out.println("name="+user.getname()); 
    system.out.println("age="+user.getage()); 
    return "success"; 
  } 
  public user getuser() { 
    return user; 
  } 
  public void setuser(user user) { 
    this.user = user; 
  }   
} 

user.java:

package cn.edu.hpu.mode;  
public class user {      
  private string name; 
  private int age;   
  public string getname() { 
    return name; 
  }  
  public void setname(string name) { 
    this.name = name; 
  }   
  public int getage() { 
    return age; 
  }   
  public void setage(int age) { 
    this.age = age; 
  }    
} 

链接:

访问user属性

<a href="<%=basepath%>ognl/user2?user.name=tom&&user.age=21" rel="external nofollow" >ognl2</a><br/> 

(只有你传user.xxxx才能构造)

结果页面:

<%@taglib uri="/struts-tags" prefix="s" %> 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
 <head> 
  <base href="<%=basepath%>" rel="external nofollow" > 
  <title>my jsp 'ognl.jsp' starting page</title> 
 </head> 
  
 <body> 
  ognl success!!<br/> 
  <ol> 
    <li>访问值栈中对象的普通属性(get set 方法 ): 
    <br/> 
 user-age:<s:property value="user.age"/>|<s:property value="user['age']"/></li> 
  </ol> 
  <s:debug></s:debug> 
 </body> 
</html> 

显示结果:

ognl success!!

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

user-age:21|21

[debug]

点击[debug]可以查看到栈值中有user对象的存在

例3:

struts.xml

<package name="ognl" namespace="/ognl" extends="struts-default"> 
  <action name="cat1" class="cn.edu.hpu.ognl.ognlaction2"> 
   <result name="success">/ognl.jsp</result> 
 </action> 
</package> 

ognlaction2.java:

package cn.edu.hpu.ognl; 
 
import cn.edu.hpu.mode.cat;  
import com.opensymphony.xwork2.actionsupport; 
public class ognlaction2 extends actionsupport{   
  private cat cat; 
  public cat getcat() { 
    return cat; 
  } 
  public void setcat(cat cat) { 
    this.cat = cat; 
  } 
  public string execute(){ 
    return "success"; 
  } 
  public string m(){ 
    return "hello"; 
  } 
} 

cat.java:

package cn.edu.hpu.mode;  
public class cat {  
  private dog friend; 
  public dog getfriend() { 
    return friend; 
  } 
  public void setfriend(dog friend) { 
    this.friend = friend; 
  }  
  public string miaomiao(){ 
    return "miaomiao"; 
  } 
} 

dog.java:

package cn.edu.hpu.mode;   
public class dog {    
  private string name;    
  public dog(){      
  }    
  public dog(string name){ 
    super(); 
    this.name=name; 
  }   
  public string getname() { 
    return name; 
  } 
  public void setname(string name) { 
    this.name = name; 
  }  
  @override 
  public string tostring() { 
    // todo auto-generated method stub 
    return "dog:"+name; 
  } 
} 

链接:

 访问cat属性

<a href="<%=basepath%>ognl/cat1?cat.friend.name=littleboy" rel="external nofollow" >ognl3</a><br/>

结果页面:

<li>访问值栈中对象的普通属性(get set 方法 ): 
    <br/> 
    cat-friend-name:<s:property value="cat.friend.name"/></li> 

结果:

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

cat-friend-name:littleboy

观察[debug],发现只有cat在值栈中,说明通过cat联系到dog,取得dog中的属性

访问cat方法:

<a href="<%=basepath%>ognl/cat1" rel="external nofollow" rel="external nofollow" >ognl4</a><br/>

结果页面:

<li>访问值栈中对象的普通方法: 
    <br/> 
    cat-miaomiao:<s:property value="cat.miaomiao()"/></li> 

结果:

访问值栈中对象的普通属性:

cat-miaomiao:miaomiao

访问action的普通方法:

<a href="<%=basepath%>ognl/cat1" rel="external nofollow" rel="external nofollow" >ognl5</a><br/> 

结果页面:

<li>访问值栈中action的普通方法: 
    <br/> 
    action-m():<s:property value="m()"/></li> 

结果:

访问值栈中acion的普通方法:

action-m():hello

总结

以上就是本文关于struts2 ognl表达式实例详解的全部内容,希望对大家有所帮助。有什么问题可以随时留言,欢迎大家交流讨论。