天天看點

Struts2——OGNL(一)

二十、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

User的name屬性為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="@[email protected]()">

注意,兩個@是約定..也就是必須這麼寫.第一個@後面跟的是類的全名.第二個@後面跟的是對應的方法名.當然,這個方法必須是靜态的

 這個通路靜态方法在struts2.1以後的版本裡面需要設定一個屬性,否則系統預設是不支援通路靜态方法的(struts2.0版本預設是支援通路靜态方法的).具體的方法是在struts.xml裡面添加這麼一句

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

6 通路靜态屬性

<s:property value="@[email protected]"/>

這個和上面也類似...通路的是靜态的屬性..

範例:

(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/css" href="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/css" href="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="@[email protected]()"></s:property></li>

<li> 通路靜态屬性:<s:property value ="@[email protected]"></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)浏覽器端通路,檢視結果

Struts2——OGNL(一)

繼續閱讀