OGNL在struts2中的使用

下面是在JSP上使用struts2和OGNL读取Action中数据的例子。
Cat:


package com.action.ognl;

public class Cat {
	private String name;
	private int age;
	private String color;
	
	public Cat(String name,int age,String color)
	{
		this.name=name;
		this.age=age;
		this.color=color;
	}
	public Cat()
	{
		
	}
	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 String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	
}

Person:


package com.action.ognl;

import java.util.Map;

public class Person {
	private String name;
	private int age;
	private String address;
	private String[] friends;
	private Cat cat;
	private Map map;
	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 String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String[] getFriends() {
		return friends;
	}
	public void setFriends(String[] friends) {
		this.friends = friends;
	}
	public Cat getCat() {
		return cat;
	}
	public void setCat(Cat cat) {
		this.cat = cat;
	}
	public Map getMap() {
		return map;
	}
	public void setMap(Map map) {
		this.map = map;
	}
	public Person(String name, int age, String address, String[] friends,
			Cat cat, Map map) {
		super();
		this.name = name;
		this.age = age;
		this.address = address;
		this.friends = friends;
		this.cat = cat;
		this.map = map;
	}
	public Person() {

	}
	
}

OgnlAction:


package com.action.ognl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class OgnlAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {
	String username;
	private String password;
	private Map requestMap;
	private Map sessionMap;
	private Map applicationMap;
	private List list;
	
	public OgnlAction() {
		list=new ArrayList();
	}
	public List getList() {
		return list;
	}

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

	@Override
	public void setRequest(Map arg0) {
		// TODO Auto-generated method stub
		this.requestMap=arg0;
	}

	@Override
	public void setApplication(Map arg0) {
		// TODO Auto-generated method stub
		this.applicationMap=arg0;
	}

	@Override
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		this.sessionMap=arg0;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		System.out.println("setUsername");
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		requestMap.put("hello", "world");
		sessionMap.put("hello", "world");
		applicationMap.put("hello", "world");
		
		Cat cat1=new Cat("cat1",20,"red");
		Cat cat2=new Cat("cat2",30,"black");
		
		String[] friends1={"test1","test2","test3"};
		String[] friends2={"welcom1","welcom2","welcom3"};
		
		Map map1=new HashMap();
		Map map2=new HashMap();
		map1.put("test1", "test1");
		map1.put("test2", "test2");
		
		map2.put("hello1", "hello1");
		map2.put("hello2", "hello2");
		Person person1=new Person("zhangsan", 20, "beijing", friends1, cat1, map1);
		Person person2=new Person("lisi", 40, "hangzou", friends2, cat2, map2);
		System.out.println(person1);
		list.add(person1);
		list.add(person2);
		
		return SUCCESS;
	}
	
}

Ognl.jsp(OgnlAction的result定义转发)


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>




OGNL在struts2中的使用 | 应用开发笔记


取Action中的属性
username:
password:
取post,get参数:
username:
password:
取request,session,application属性
request:
session:
application:
取对象各种属性: Person1.address:
Person2.age:
Person2.map:
过滤:
name:
address:
投影(取指定属性):

© 2012, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记