【Struts2框架】第三节OGNL表达式-OGNL表达式例子

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" >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">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>

显示结果:
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" >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" >OGNL4</a><br/>

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

结果:
访问值栈中对象的普通属性:
cat-miaomiao:miaomiao

访问action的普通方法:
<a href="<%=basePath%>ognl/cat1" >OGNL5</a><br/>

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

结果:
访问值栈中acion的普通方法:
action-m():Hello

转载请注明出处:http://blog.csdn.net/acmman/article/details/47069953

你可能感兴趣的:(struts2,ognl表达式)