struts2之OGNL表达式

OGNL表达式一般用来取值,下面通过这个例子程序,来看看ognl表达式

(1)使用ognl表达式的时候需要在struts.xml文件中配置这句话

<span style="font-size:18px;"> <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant></span>

struts.xml

<span style="font-size:18px;"><?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="true"/>
   <constant name="struts.devMode" value="true"/>
   <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
   <include file="/com/dqpi/eonline/action/ognl.xml"/>
</struts></span>
ognl.xml

<span style="font-size:18px;"><?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>
    <package name="ognl"  extends="struts-default">
        <action name="ognl" class="com.dqpi.eonline.action.OgnlAction">
			<result>/ognl.jsp</result>        
        </action>
    </package>
</struts></span>
OgnlAction.java

<span style="font-size:18px;">package com.dqpi.eonline.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;

public class OgnlAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private Cat cat;
	private Map<String, Dog> dogMap = new HashMap<String, Dog>();

	private Set<Dog> dogs = new HashSet<Dog>();

	private String name;

	private String password;

	private User user;
	// private User user = new User();

	private List<User> users = new ArrayList<User>();

	@Override
	public String execute() throws Exception {
		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("dog101", new Dog("dog101"));
		dogMap.put("dog102", new Dog("dog102"));
		return SUCCESS;
	}

	public Cat getCat() {
		return cat;
	}

	public Map<String, Dog> getDogMap() {
		return dogMap;
	}

	public Set<Dog> getDogs() {
		return dogs;
	}

	public String getName() {
		return name;
	}

	public String getPassword() {
		return password;
	}

	public User getUser() {
		return user;
	}

	public List<User> getUsers() {
		return users;
	}

	public String m() {
		return "miaomiao";
	}

	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 setName(String name) {
		this.name = name;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}
}
</span>
Dog.java

<span style="font-size:18px;">package com.dqpi.eonline.action;

public class Dog {
	private String name;

	public Dog() {
		super();
	}

	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() {
		return "dog" + name;
	}
}
</span>
Cat.java

<span style="font-size:18px;">package com.dqpi.eonline.action;

public class Cat {
	
	private Dog friend;

	public Dog getFriend() {
		return friend;
	}

	public void setFriend(Dog friend) {
		this.friend = friend;
	}
	
	public String miaomiao(){
		return "miaomiao";
	}
}
</span>
S.java

<span style="font-size:18px;">package com.dqpi.eonline.action;

public class S {
	public static String STR = "STATIC STRING";
	public static String s(){
		return "static method";
	}
}
</span>
User.java

<span style="font-size:18px;">package com.dqpi.eonline.action;

public class User {
	private int age=9;
	
	public User() {
		System.out.println("user constructor");
	}

	public User(int age) {
		super();
		this.age = age;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "user" + age;
	}

}
</span>
index.jsp

<span style="font-size:18px;"><%@ 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>resultType.jsp</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?name=a&password=p">访问OGNL属性</a><br/>
  </body>
</html>
</span>
ognl.jsp

<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!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>
   		访问值栈中action的普通属性:username=<s:property value="name"/><br/>
   		访问值栈中对象的普通属性(get/set方法):<s:property value="user.age"/><br/>
   		访问值栈中对象的普通属性(get/set方法):<s:property value="cat.friend.name"/><br/>
   		访问值栈中的普通方法:<s:property value="password.length()"/><br/>
   		访问值栈中对象的普通方法:<s:property value="cat.miaomiao()"/><br/>
   		访问action中的普通方法:<s:property value="m()"/>
   		<hr/>
   		<!--第一个@后面加包名类名,第二个@后面加方法名  -->
   		访问静态方法:<s:property value="@com.dqpi.eonline.action.S@s()"/><br/>
   		<!--第一个@后面加包名类名,第二个@后面加属性名  -->
   		访问静态属性:<s:property value="@com.dqpi.eonline.action.S@STR"/><br/>
   		访问Math类的静态方法:<s:property value="@@max(2,3)"/>
   		<hr/>
   		访问普通类的构造方法:<s:property value="new com.dqpi.eonline.action.User(8)"/>
   		<hr/>
   		访问List:<s:property value="users"/><br/>
   		访问List中的某个元素:<s:property value="users[1]"/><br/>
   		访问List中某个属性的集合:<s:property value="users.{age}"/><br/>
   		访问List中某个属性集合中特定的值:<s:property value="users.{age}[0]"/>|<s:property value="users.[0].age"/><br/>
   		访问set:<s:property value="dogs"/><br/>
   		访问set中某个元素:<s:property value="dogs[1]"/><br/>
   		访问map:<s:property value="dogMap"/><br/>
   		访问map中某个元素:<s:property value="dogMap.dog101" /><br/>
   		访问map中的所有Key:<s:property value="dogMap.keys"/><br/>
   		访问map中的所有value:<s:property value="dogMap.values"/><br/>
   		访问容器的大小:<s:property value="dogMap.size()"/>
   		<hr/>
   		投影:<s:property value="users.{?#this.age==1}.{age}"/><br/>
   		投影:<s:property value="users.{?#this.age==1}"/><br/>
   		投影:<s:property value="users.{?#this.age==1}[0]"/><br/>
   		投影:<s:property value="users.{^#this.age>1}.{age}"/><br/>
   		投影:<s:property value="users.{$#this.age>1}.{age}"/><br/>
   		投影:<s:property value="users.{$#this.age>1}.{age}==null"/>
   		<hr/>
   		[]:<s:property value="[0]"/>
   		<s:debug></s:debug>
		
  </body>
</html>
</span>










你可能感兴趣的:(struts2之OGNL表达式)