OGNL

package com.al.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.al.entity.Dog;
import com.al.entity.User;
import com.opensymphony.xwork2.ActionSupport;

public class OgnlAction extends ActionSupport {
	
	
	
	private String username;
	
	
	public   static int temp = 10;
	
	private User user;
	
	
	
	
	private List<User> list = new ArrayList<User>();
	
	private Set<User> set = new HashSet<User>();
	
	private Map<String,User> map = new HashMap<String, User>();
	
	public String test(){
		
		User u1 = new User();
		u1.setName("tom");
		u1.setAge(1);
		Dog d1 = new Dog();
		d1.setType("aaa");
		u1.setDog(d1);
		
		
		User u2 = new User();
		u2.setName("jerry");
		u2.setAge(2);
		Dog d2 = new Dog();
		d2.setType("bbb");
		u2.setDog(d2);
		
		User u3 = new User();
		u3.setName("kiki");
		u3.setAge(3);
		Dog d3 = new Dog();
		d3.setType("ccc");
		u3.setDog(d3);
		
		list.add(u1);
		list.add(u2);
		list.add(u3);
		
		set.add(u1);
		set.add(u2);
		set.add(u3);
		
		map.put("u1", u1);
		map.put("u2", u2);
		map.put("u3", u3);
		
		
		return "success";
	}
	
	
	public int add(int a,int b){
		
		return a+b;
	}
	
	
	public static String getString(){
		
		return "aaa";
	}
	
	

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}



	public User getUser() {
		return user;
	}



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


	public List<User> getList() {
		return list;
	}


	public void setList(List<User> list) {
		this.list = list;
	}


	public Set<User> getSet() {
		return set;
	}


	public void setSet(Set<User> set) {
		this.set = set;
	}


	public Map<String, User> getMap() {
		return map;
	}


	public void setMap(Map<String, User> map) {
		this.map = map;
	}





	/*public static int getTemp() {
		return temp;
	}


	public static void setTemp(int temp) {
		OgnlAction.temp = temp;
	}
	*/
	

}
package com.zl.entity;

public class Dog {
	
	private String type;
	
	public String talk(){
		
		return "i am a dog";
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}

package com.al.entity;

public class User {
	
	
	
	
	public User(){

		System.out.println("i am a user");
	}
	
	public User(String a){
		System.out.println("i am a user"+a);
	}
	
	
	private String name;
	
	private int age;
	
	public static double dd = 1.2;
	
	private Dog dog;
	
	
    public static String getString(){
		
		return "aaa";
	}
	
	
	
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

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

	public Dog getDog() {
		return dog;
	}

	public void setDog(Dog dog) {
		this.dog = dog;
	}
	

}

<?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.ognl.allowStaticMethodAccess" value="true"></constant>
  
   <package name="test" namespace="/test" extends="struts-default">
   
       <action name="ognlAction" class="com.zl.action.OgnlAction">
          
            <result>/ognl.jsp</result>
       
       </action>
   </package>
</struts>

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
      <filter-name>ActionFilter</filter-name>
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  
  <filter-mapping>
      <filter-name>ActionFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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="test/ognlAction!test?username=tom&user.name=jerry&user.age=1&user.dog.type=aaa">测试ognl</a>
  </body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
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>
    <h1>测试ognl</h1>
    <ul>
       <li>从Action取得普通属性的值:<s:property value="username"/></li>
       <li>从Action取得对象的普通属性的值:<s:property value="user.name"/>|<s:property value="user.age"/></li>
       <li>从Action取得对象的普通属性的值:<s:property value="user.dog.type"/></li>
       <li>调用Action的普通方法:<%-- <s:property value="test()"/> --%>
       <li>调用Action的普通方法:<s:property value="add(3,5)"/>
       <li>调用Action的对象的普通方法:<s:property value="user.name.length()"/>
       <li>调用Action的对象的普通方法:<s:property value="user.dog.talk()"/>
       
       <hr>
       <li>得到Action的静态属性:<s:property value="@com.zl.action.OgnlAction@temp" /></li>
       <li>得到普通对象的静态属性:<s:property value="@com.zl.entity.User@dd" /></li>
       <li>调用Action的静态方法:<s:property value="@com.zl.action.OgnlAction@getString()" /></li>
       <li>调用普通对象的静态方法:<s:property value="@com.zl.action.OgnlAction@getString()" /></li>
       <li>调用普通对象的静态方法:<s:property value="@java.lang.Math@max(2,5)" /></li>
      
       <hr>
       <li>调用普通对象的构造方法:<s:property value="new com.zl.entity.User()" /></li>
       <li>调用普通对象的构造方法:<s:property value="new com.zl.entity.User(username).getString()" /></li>
    
       <hr>
       <li>得到list集合:<s:property value="list"/></li>
       <li>得到list集合中某个元素:<s:property value="list[0].name"/></li>
       <li>得到list集合中元素的属性的集合:<s:property value="list.{name}[0]"/></li>
       <li>得到list集合中元素的属性的集合:<s:property value="list.{dog}[0].type"/></li>
       <li>得到list集合中元素的属性的集合:<s:property value="list.{dog}.{type}[0]"/></li>
       <li>得到list集合的长度:<s:property value="list.size()"/></li>
       
       <hr>
       <li>得到set集合:<s:property value="set"/></li>
       <li>得到set集合中某个元素: <s:property value="set.iterator().next()"/></li>
       <li>得到set集合中元素的属性的集合:<s:property value="set.{name}[0]"/></li>
       <li>得到set集合中元素的属性的集合:<s:property value="set.{dog}.{type}[0]"/></li>
       <li>得到set集合的长度:<s:property value="set.size()"/></li>
    
       <hr>  
       <li>得到map集合:<s:property value="map"/></li>
       <li>得到map集合中所有key:<s:property value="map.keys"/></li>
       <li>得到map集合中某个key:<s:property value="map.keys.iterator().next()"/></li>
       <li>得到map集合中所有value:<s:property value="map.values"/></li>
       <li>map集合中通过key得到value:<s:property value="map.u1"/></li>
       <li>map集合中通过key得到value:<s:property value="map['u1']"/></li>
       <li>得到map集合的长度:<s:property value="map.size()"/></li>
   
       <hr>
       <li>带条件的集合:<s:property value="list.{?#this.age>=2}.{dog}.{?#this.type=='ccc'}.{type}[0]"/></li>
       <li>带条件的集合:<s:property value="list.{^#this.age>=2}.{name}"/></li>
       <li>带条件的集合:<s:property value="list.{$#this.age>=2}.{name}"/></li>
   
    </ul>
    
    <s:debug></s:debug>
  </body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值