Object Graph Navigation Language:对象图导航语言,就是用点来访问成员变量
1
|
< s :property value = "cat.name" />
|
例1:
struts.xml:
1
2
3
4
5
|
< 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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();
}
}
|
访问链接:
1
|
|
结果界面:
1
2
3
4
5
6
|
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:
1
2
3
4
5
|
< 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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属性
1
|
< a href="<%=basePath%>ognl/user2?user.name=tom&&user.age=21" rel="external nofollow" >OGNL2</ a >< br />
|
(只有你传user.XXXX才能构造)
结果页面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@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
1
2
3
4
5
|
< 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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属性
1
|
< a href="<%=basePath%>ognl/cat1?cat.friend.name=littleBoy" rel="external nofollow" >OGNL3</ a >< br />
|
结果页面:
1
2
3
|
< 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方法:
1
|
< a href="<%=basePath%>ognl/cat1" rel="external nofollow" rel="external nofollow" >OGNL4</ a >< br />
|
结果页面:
1
2
3
|
< li >访问值栈中对象的普通方法:
< br />
cat-miaomiao:< s:property value = "cat.miaomiao()" /></ li >
|
结果:
访问值栈中对象的普通属性:
cat-miaomiao:miaomiao
访问action的普通方法:
1
|
< a href="<%=basePath%>ognl/cat1" rel="external nofollow" rel="external nofollow" >OGNL5</ a >< br />
|
结果页面:
1
2
3
|
< li >访问值栈中action的普通方法:
< br />
action-m():< s:property value = "m()" /></ li >
|
结果:
访问值栈中acion的普通方法:
action-m():Hello
总结
以上就是本文关于Struts2 OGNL表达式实例详解的全部内容,希望对大家有所帮助。有什么问题可以随时留言,欢迎大家交流讨论。
原文链接:http://blog.csdn.net/acmman/article/details/47069953