Object Graph Navigation Language對象圖導航語言。
是Struts2默認的表達式語言,開源,功能更強大。和EL表達式有點相似
存取對象的屬性,調用對象的方法
訪問靜態方法,靜態屬性
訪問值棧及Stack Context
支持賦值、運算操作、字段類型轉化等。
二、簡單例子導入所需要的struts2的包
web.xml增加struts2過濾器
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPRepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
實體類
package com.pb.entity;/* * 街道實體類 */public class Street { private int streetId; //街道ID private String streetName; //街道名稱 public Street() { super(); // TODO Auto-generated constructor stub } public Street(int streetId, String streetName) { super(); this.streetId = streetId; this.streetName = streetName; } public int getStreetId() { return streetId; } public String getStreetName() { return streetName; } public void setStreetId(int streetId) { this.streetId = streetId; } public void setStreetName(String streetName) { this.streetName = streetName; } }
package com.pb.entity;/* * 房子實體類 */public class Hourse { private int hourseId; //房屋ID private String hourseName; //房屋名稱 private Street street; //房屋所在街道 public Hourse() { super(); // TODO Auto-generated constructor stub } public Hourse(int hourseId, String hourseName, Street street) { super(); this.hourseId = hourseId; this.hourseName = hourseName; this.street = street; } public int getHourseId() { return hourseId; } public String getHourseName() { return hourseName; } public Street getStreet() { return street; } public void setHourseId(int hourseId) { this.hourseId = hourseId; } public void setHourseName(String hourseName) { this.hourseName = hourseName; } public void setStreet(Street street) { this.street = street; } }
HourseAction
package com.pb.web.action;import com.opensymphony.xwork2.ActionSupport;import com.pb.entity.Hourse;import com.pb.entity.Street;public class HourseAction extends ActionSupport { private Hourse hourse; @Override public String execute() throws Exception { //聲明街道對象 Street street =new Street(001,"南新路"); hourse=new Hourse(11,"一房一廳",street); return SUCCESS; } public Hourse getHourse() { return hourse; } public void setHourse(Hourse hourse) { this.hourse = hourse; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="hourse" class="com.pb.web.action.HourseAction"> <result name="success"> /ognl1.jsp </result> </action> </package></struts>
ognl1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>OGNL一</title></head><body>房屋的名稱:<s:property value="hourse.hourseName"/>房屋的街道名稱:<s:property value="hourse.street.streetName"/><s:debug/></body></html>
在地址欄中輸入
http://localhost:8080/OGNLDemo1/hourse.action
發生頁面跳轉,并取出其中的值
三、訪問值棧中的值struts.xml
<action name="ognl" class="com.pb.web.action.OgnlAction"> <result name="success"> /ognllist.jsp </result> </action>
ognllistAction
package com.pb.web.action;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import com.opensymphony.xwork2.ActionSupport;import com.pb.entity.Street;public class OgnlAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private List<Street> streetList; private Map<String, Street> streetMap; private Set <Street> streetSet; public List<Street> getStreetList() { return streetList; } public void setStreetList(List<Street> streetList) { this.streetList = streetList; } public Map<String, Street> getStreetMap() { return streetMap; } public void setStreetMap(Map<String, Street> streetMap) { this.streetMap = streetMap; } public Set<Street> getStreetSet() { return streetSet; } public void setStreetSet(Set<Street> streetSet) { this.streetSet = streetSet; } @Override public String execute() throws Exception { streetList=new ArrayList<Street>(); streetList.add(new Street(1, "高新南一路")); streetList.add(new Street(2, "高新南二路")); streetList.add(new Street(3, "高新南三路")); streetMap=new HashMap<String,Street>(); streetMap.put("k1", new Street(4, "高新南四路")); streetMap.put("k2", new Street(5, "高新南五路")); streetMap.put("k3", new Street(6, "高新南六路")); streetSet =new HashSet<Street>(); streetSet.add(new Street(7, "高新南七路")); streetSet.add(new Street(8, "高新南八路")); streetSet.add(new Street(9, "高新南九路")); return SUCCESS; } }
jsp頁頁
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>OGNL一</title></head><body><h1>List</h1>訪問List<s:property value="streetList" /><br/>訪問List中第一個元素:<s:property value="streetList[0].streetName" /><br/>List中的大小:<s:property value="streetList.size()" /><br/>List是否為空:<s:property value="streetList.isEmpty()" /><br/>自定義集合List:<s:property value="{1,2,3,4,5}" /><br/>自定義集合List訪問第一個元素:<s:property value="{1,2,3,4,5}[0]" /><br/><hr/><h1>Map</h1>訪問Map<s:property value="streetMap" /><br/>訪問Map中第一個元素:<s:property value="streetMap['k1'].streetName" />|<s:property value="streetMap.k1.streetName"/><br/>訪問Map 中所有key:<s:property value="streetMap.keys" /><br/>訪問Map 中所有values:<s:property value="streetMap.values" /><br/>Map中的大小:<s:property value="streetMap.size()" /><br/>Map是否為空:<s:property value="streetMap.isEmpty()" /><br/>自定義集合Map:<s:property value="#{'k1':'v1','k2':'v2','k3':'v3'}" /><br/>自定義集合Mapt訪問第一個元素:<s:property value="#{'k1':'v1','k2':'v2','k3':'v3'}['k1']" />|<s:property value="#{'k1':'v1','k2':'v2','k3':'v3'}.k1" /><br/><hr/><h1>Set</h1>訪問Set<s:property value="streetSet" /><br/>訪問Set中第一個元素:<s:property value="streetSet.toArray()[0].streetName" />|<s:property value="streetSet.toArray()[0].streetName"/><br/>Set中的大小:<s:property value="streetSet.size()" /><br/>Set是否為空:<s:property value="streetSet.isEmpty()" /><br/><hr/><s:debug/></body></html>
在頁面中輸入:http://localhost:8080/OGNLDemo1/ognl.action訪問
五、訪問靜態方法和投影package com.pb.demo;public class Demo {public final static String STR="STATIC STR";public static String staticMethod(){String info="Demo Static Method";return info;}}
在ognl2.jsp 中可以通過如下代碼訪問該類的靜態屬性以及靜態方法:
<%@taglib uri="/struts-tags" prefix="s"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h1>OGNL 對象圖導航語言</h1>調用靜態方法:<s:property value="@com.pb.demo.Demo@staticMethod()"/> <br>調用靜態常量:<s:property value="@com.pb.demo.Demo@STR"/><s:debug/></body></html>
struts.xml
//開啟靜態方法<constant name="struts.ognl.allowStaticMethodaccess" value="true" />
其中<constant>常量你可以去default.properties 文件中查找并復制使用。
5.2、訪問投影選擇就是過濾滿足選擇條件的集合元素。選擇操作的語法是:collection.{X YYY},其中X是一個選擇操作符,后面則是選擇用的邏輯表達式,而選擇操作符有三種:?選擇滿足條件的所有元素^選擇滿足條件的第一個元素$選擇滿足條件的最后一個元素
package com.pb.entity;/* * 用戶名 */public class User { private Integer age; private String name; private String pasWord; public User() { super(); // TODO Auto-generated constructor stub } public User(String name, String pasword, Integer age) { super(); this.name = name; this.pasword = pasword; this.age = age; } public Integer getAge() { return age; } public String getName() { return name; } public String getPasword() { return pasword; } public void setAge(Integer age) { this.age = age; } public void setName(String name) { this.name = name; } public void setPasword(String pasword) { this.pasword = pasword; }}
package com.pb.web.action;import java.util.ArrayList;import java.util.List;import com.opensymphony.xwork2.ActionSupport;import com.pb.entity.User;public class UserAction extends ActionSupport { private User user; private List<User> users=new ArrayList<User>(); public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } @Override public String execute() throws Exception { User u1 = new User(); u1.setName("zhangsan"); u1.setPasword("zhangsan"); u1.setAge(10); this.users.add(u1); User u2 = new User(); u2.setName("lisi"); u2.setPasword("lisi"); u2.setAge(23); this.users.add(u2); User u3 = new User(); u3.setName("wangwu"); u3.setPasword("wangwu"); u3.setAge(22); this.users.add(u3); return SUCCESS; } }
struts.xml
<package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.pb.web.action.UserAction"> <result name="success"> /ognl.jsp </result> </action> </package>
jsp頁面
<%@taglib prefix="s" uri="/struts-tags" %><!-- 年齡大于18 的所有用戶的姓名 -->集合元素過濾:<s:property value="users.{?#this.age>18}.{name}"/><br/><!-- 年齡大于18 的所有用戶的第一個用戶的姓名 -->集合元素過濾:<s:property value="users.{^#this.age>18}.{name}"/><br/><!-- 年齡大于18 的所有用戶的最后一個用戶的姓名 -->集合元素過濾:<s:property value="users.{$#this.age>18}.{name}"/><br/>
新聞熱點
疑難解答