Struts2——OGNL(一)

来源:互联网 发布:wayv 源码 编辑:程序博客网 时间:2024/06/02 04:00

二十、OGNL

1、直接访问值栈中的action属性

<s:property value=v/>

2、访问值栈中对象普通属性(set/get方法)

在OGNL中访问对象的属性时,必须传入属性才会初始化一个Domain Model对象,但是要保持Domain Model中有默认的构造方法

http://localhost:8080/OGNL/ognl?username=u&password=p&user.age=25

Username属性为25

http://localhost:8080/OGNL/ognl?username=u&password=p&user.age

如果不传入属性值,则会使用默认的属性值。此时获取到的age属性就是Domain Model的默认值。

3、OGNL的定义:Object Graph Navigation Language(对象图导航语言)

http://localhost:8080/OGNL/ognl?username=u&password=p&cat.friend.name=zhangsan

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

可以访问到zhangsan

4、ognl的全名是 Object-Graph Navigation Language 表示的是图对象导航语言...我觉得它最厉害的一点是,通过"."来实现对象的导航...下面看他他的具体例子.我们应该就可以理解这个意思了

注意,在说例子以前先说三点

 

(1)我们这里的例子都是访问普通属性和静态方法..获取的也只是普通属性的值或者静态方法返回的值..所以这里一般是用<s:property value="ognl expression">来展示..其中,value里面的值才是ognl表达式.

 

(2)action里面我们可以有两种方式从页面中获取值.一种是直接在action里面写属性.这个是叫属性驱动.还有一种是在action里面放一个JavaBean的对象.这个叫模型驱动..具体的内容不多说了.下面说的action里面的属性,表示的是属性驱动里面的内容,如果说action里面的对象,那就表示是模型驱动里面的对象引用.

 

(3)最后说一点是值栈..值栈是一个存放对象的堆栈.是用Map来存放的,存放在值栈里的内容我们可以通过ognl表达式来获取...至于值栈都存放些什么内容.可以通过我之前说的<s:debug>标签来读取..

 

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

 

<s:property value="username"/>

 

这个不多说了.直接用对应的属性名就OK

 

2 访问值栈中的action的对象的普通属性(必须有对应的get set方法)

 

(1)<s:property value="user.name"/>

 

表示的是访问action里面的user对象里面的name属性

 

(2)<s:property value="student.class.size"/>

 

表示的是访问action里面的student对象里面的class对象里面的size属性...这句话说的有点绕,但是其实很好理解.

 

只要有需要,里面可以嵌套任意多层.只要中间用"."来分隔就行了

 

3访问值栈中对象的普通方法

 

(1)<s:property value="name.length()">

 

这里访问的是String对象(也就是name)里面的length()这个方法..

 

(2)<s:property value="user.abc()"/>

 

这里调用的是user对象里面的abc()方法.访问这个方法的返回值..如果没有返回值,则为空.

 

4访问action中的普通方法

 

<s:property value="abc()">

 

访问的是action中定义的方法...其实都很类似的...

 

5 访问静态方法

 

<s:property value="@com.test.util.S@abc()">

 

注意,两个@是约定..也就是必须这么写.第一个@后面跟的是类的全名.第二个@后面跟的是对应的方法名.当然,这个方法必须是静态的

 

 这个访问静态方法在struts2.1以后的版本里面需要设置一个属性,否则系统默认是不支持访问静态方法的(struts2.0版本默认是支持访问静态方法的).具体的方法是在struts.xml里面添加这么一句

 

 <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>

 

6 访问静态属性

 

<s:property value="@com.test.util@parm"/>

 

这个和上面也类似...访问的是静态的属性..

范例:

(1)创建index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">    

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

  </head>

  

  <body>

    <a href="ognl?username=u&password=p">1.访问OGNL</a>

  </body>

</html>

(2)配置ognl.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

 

<struts>

    <constant name="struts.devMode" value="true" />

    <package name="ognl" extends="struts-default" namespace="/">

        <action name="ognl" class="com.zgy.ognl.OgnlAction">

            <result>/ognl.jsp</result>

        </action>

    </package>

</struts>

 

 

 

(3)配置struts.xml,引入ognl.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

 

<struts>

    <constant name="struts.devMode" value="true" />

    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>

    <include file="com/zgy/ognl/ognl.xml"></include>

</struts>

(4)编写OgnlAction.java

package com.zgy.ognl;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class OgnlAction extends ActionSupport {

private String username;

private String password;

private User user;

private Cat cat;

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;

}

public String execute(){

return SUCCESS;

}

public User getUser() {

return user;

}

public void setUser(User user) {

this.user = user;

}

public Cat getCat() {

return cat;

}

public void setCat(Cat cat) {

this.cat = cat;

}

public String m(){

return "hello world!";

}

}

(5)编写ognl.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@taglib uri="/struts-tags" prefix="s"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme() + "://"

+ request.getServerName() + ":" + request.getServerPort()

+ path + "/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

 

<title>My JSP 'ognl.jsp' starting page</title>

 

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/csshref="styles.css">

-->

 

</head>

 

<body>

<ul>

<li>访问值栈中的action的普通属性:username = <s:property value="username"></s:property>

</li>

<li>访问值栈中对象的普通属性(get set方法):<s:property value="user.age" /> | <s:property

value="user['age']" /> | <s:property value="user[\"age\"]" /> |

wrong: <%--<s:property value="user[age]"/>--%>

</li>

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

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

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

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

<hr />

<li> 访问静态方法:<s:property value="@com.zgy.ognl.S@s()"></s:property></li>

<li> 访问静态属性:<s:property value ="@com.zgy.ognl.S@STR"></s:property></li>

<li> 访问Math类的静态方法:<s:property value="@@max(2,3)"></s:property></li>

<s:debug></s:debug>

</ul>

</body>

</html>

(6)创建Cat.java

package com.zgy.ognl;

 

public class Cat {

private Dog friend;

 

public Dog getFriend() {

return friend;

}

 

public void setFriend(Dog friend) {

this.friend = friend;

}

public String miaomiao(){

return "miaomiao";

}

}

(7)创建Dog.java

package com.zgy.ognl;

 

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;

}

public String toString(){

return "dog:"+name;

}

}

(8)创建S.java

package com.zgy.ognl;

 

public class S {

public static String s(){

return "Static Method";

}

public static String STR = "Static String";

}

(9)浏览器端访问,查看结果




0 0
原创粉丝点击