OGNL(Object Graph Navigation Language)对象图导航语言,以下为演示代码。
OgnlAction.java
package com.bebig.struts2.ognl;
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;
public class OgnlAction extends ActionSupport {
private String username;
private String password;
private User user;
private Cat cat;
private List<User> users = new ArrayList<User>();
private Set<Dog> dogs = new HashSet<Dog>();
private Map<String,Dog> dogMap = new HashMap<String,Dog>();
public OgnlAction() {
users.add(new User(1));
users.add(new User(2));
users.add(new User(3));
dogs.add(new Dog("dog1"));
dogs.add(new Dog("dog2"));
dogs.add(new Dog("dog3"));
dogMap.put("dog100", new Dog("dog100"));
dogMap.put("dog200", new Dog("dog200"));
dogMap.put("dog300", new Dog("dog300"));
}
@Override
public String execute() {
return SUCCESS;
}
public Cat getCat() {
return cat;
}
public Map<String, Dog> getDogMap() {
return dogMap;
}
public Set<Dog> getDogs() {
return dogs;
}
public String getPassword() {
return password;
}
public User getUser() {
return user;
}
public String getUsername() {
return username;
}
public List<User> getUsers() {
return users;
}
public String m()
{
return "m method";
}
public void setCat(Cat cat) {
this.cat = cat;
}
public void setDogMap(Map<String, Dog> dogMap) {
this.dogMap = dogMap;
}
public void setDogs(Set<Dog> dogs) {
this.dogs = dogs;
}
public void setPassword(String password) {
this.password = password;
}
public void setUser(User user) {
this.user = user;
}
public void setUsername(String username) {
this.username = username;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
User.java
package com.bebig.struts2.ognl;
public class User {
private int age = 8;
public User()
{
}
public User(int age)
{
super();
this.age = age;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
@Override
public String toString()
{
return "user's age is "+age;
}
}
Cat.java
package com.bebig.struts2.ognl;
public class Cat {
private Dog friend;
public void setFriend(Dog friend) {
this.friend = friend;
}
public Dog getFriend() {
return friend;
}
public String sayHi() {
return "miaomiao";
}
}
Dog.java
package com.bebig.struts2.ognl;
public class Dog {
private String name;
public Dog() {
}
public Dog(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Dog's name is " + name;
}
}
OgnlAction2.java
package com.bebig.struts2.ognl;
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;
public class OgnlAction2 extends ActionSupport {
private String username;
private String password;
private User user;
private Cat cat;
private List<User> users = new ArrayList<User>();
private Set<Dog> dogs = new HashSet<Dog>();
private Map<String,Dog> dogMap = new HashMap<String,Dog>();
public OgnlAction2() {
users.add(new User(1));
users.add(new User(2));
users.add(new User(3));
dogs.add(new Dog("dog1"));
dogs.add(new Dog("dog2"));
dogs.add(new Dog("dog3"));
dogMap.put("dog100", new Dog("dog100"));
dogMap.put("dog200", new Dog("dog200"));
dogMap.put("dog300", new Dog("dog300"));
}
@Override
public String execute() {
return SUCCESS;
}
public Cat getCat() {
return cat;
}
public Map<String, Dog> getDogMap() {
return dogMap;
}
public Set<Dog> getDogs() {
return dogs;
}
public String getPassword() {
return password;
}
public User getUser() {
return user;
}
public String getUsername() {
return username;
}
public List<User> getUsers() {
return users;
}
public String m()
{
return "m method";
}
public void setCat(Cat cat) {
this.cat = cat;
}
public void setDogMap(Map<String, Dog> dogMap) {
this.dogMap = dogMap;
}
public void setDogs(Set<Dog> dogs) {
this.dogs = dogs;
}
public void setPassword(String password) {
this.password = password;
}
public void setUser(User user) {
this.user = user;
}
public void setUsername(String username) {
this.username = username;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
test.java
package com.bebig.struts2.ognl;
public class test {
public static String STR = "This is a static field.";
public static String s() {
return "This is a static method.";
}
}
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>
<!-- 允许访问静态方法,Struts2.2.1默认为false,改变值后,需要重新布署或者重启服务才能生效 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<package name="ognl" namespace="/ognl" extends="struts-default">
<action name="ognl" class="com.bebig.struts2.ognl.OgnlAction">
<result>/ognl.jsp</result>
</action>
<action name="ognl2" class="com.bebig.struts2.ognl.OgnlAction2">
<result type="chain">ognl</result>
</action>
</package>
</struts>
struts.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" />
<include file="/com/bebig/struts2/ognl/ognl.xml"></include>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
以下为演示文件源代码:
index.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<base href="<%=basePath%>" />
</head>
<body>
<ol>
<li><a href="ognl/ognl?username=Don&password=123&cat.friend.name=oudy&user.age=27">访问OGNL(只有一个Action)</a></li>
<li><a href="ognl/ognl2?username=Don&password=123&cat.friend.name=oudy&user.age=27">访问OGNL2(存在多个Action)</a></li>
</ol>
</body>
</html>
ognl.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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=GB18030">
<title>OGNL</title>
</head>
<body>
<ol>
<li>访问值栈中的action的普通属性: <s:property value="username" /> | <s:property
value="password" />
</li>
<li>
访问值栈中对象的普通属性(get set方法):<s:property value="user.age"/>
</li>
<li>
访问值栈中对象的普通属性(get set方法):
<s:property value="cat.friend.name" />
</li>
<li>访问值栈中对象的普通方法:<s:property value="password.length()" /></li>
<li>访问值栈中对象的普通方法:<s:property value="cat.sayHi()"/></li>
<li>访问值栈中action的普通方法:<s:property value="m()"/></li>
<hr />
<li>访问静态方法:<s:property value="@com.bebig.struts2.ognl.test@s()"/></li>
<li>访问静态属性:<s:property value="@com.bebig.struts2.ognl.test@STR"/></li>
<li>访问Math类的方法:<s:property value="@@max(2,3)"/></li>
<hr />
<li>访问普通类的构造方法:<s:property value="new com.bebig.struts2.ognl.User(8)"/></li>
<hr>
<li>访问List:<s:property value="users"/></li>
<li>访问List中某个元素:<s:property value="users[2]"/></li>
<li>访问List中元素某个属性的集合:<s:property value="users.{age}"/></li>
<li>访问List中元素某个属性的集合中的特定值:<s:property value="users.{age}[0]"/> | <s:property value="users[0].age"/></li>
<li>访问Set:<s:property value="dogs"/></li>
<li>访问Set中某个元素:<s:property value="dogs[1]"/></li>
<li>访问Map:<s:property value="dogMap"/></li>
<li>访问Map中某个元素:<s:property value="dogMap.dog100"/> | <s:property value="dogMap['dog100']"/> | <s:property value="dogMap[\"dog100\"]"/> </li>
<li>访问Map中所有的Key:<s:property value="dogMap.keys"/></li>
<li>访问Map中所有的Value:<s:property value="dogMap.values"/></li>
<li>访问容器的大小:<s:property value="dogMap.size()"/> | <s:property value="users.size" /> </li>
<hr>
<li>投影(过滤):<s:property value="users.{?#this.age==1}.{age}[0]"/></li>
<li>投影:<s:property value="users.{^#this.age>1}.{age}"/></li>
<li>投影:<s:property value="users.{$#this.age>1}.{age}"/></li>
<li>投影:<s:property value="users.{$#this.age>1}.{age}==null"/></li>
<hr>
<li>[]:<s:property value="[0].users"/></li>
</ol>
<s:debug></s:debug>
</body>
</html>
在投影演示中,^ 表示开头,$ 表示结尾,? 表示过滤条件。