您好,登錄后才能下訂單哦!
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表達式實例詳解的全部內容,希望對大家有所幫助。有什么問題可以隨時留言,歡迎大家交流討論。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。