<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>
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" >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>
例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>
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; } }
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; } }
<a href="<%=basePath%>ognl/user2?user.name=tom&&user.age=21">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%>"> <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>
例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>
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"; } }
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"; } }
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; } }
<a href="<%=basePath%>ognl/cat1?cat.friend.name=littleBoy" >OGNL3</a><br/>
<li>访问值栈中对象的普通属性(get set 方法 ): <br/> cat-friend-name:<s:property value="cat.friend.name"/></li>
观察[Debug],发现只有cat在值栈中,说明通过cat联系到dog,取得dog中的属性
访问cat方法:
<a href="<%=basePath%>ognl/cat1" >OGNL4</a><br/>
<li>访问值栈中对象的普通方法: <br/> cat-miaomiao:<s:property value="cat.miaomiao()"/></li>
<a href="<%=basePath%>ognl/cat1" >OGNL5</a><br/>
<li>访问值栈中action的普通方法: <br/> action-m():<s:property value="m()"/></li>
转载请注明出处:http://blog.csdn.net/acmman/article/details/47069953