分类: 系统运维
2012-02-03 21:59:24
在Struts2中有一个称之为值栈的概念(ValueStack)。通过[N]和top访问元素。
在Struts2中 ,根对象就是ValueStack.在Struts2的任何流程中,ValueStack中的最顶层对象一定是Action对象
parameters , #parameters.username
request, #request.username
session, #session.username
application, #application.username
attr, #attr.username
以上几个对象叫做“命名对象”
访问静态方法或是静态成员变量的改进
@ValueStack@method
Cat.java
package com.shengsiyuan.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.java
package com.shengsiyuan.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
public Person(String name, int age, String address, String[] friends,
Cat cat, Map
{
this.name = name;
this.age = age;
this.address = address;
this.friends = friends;
this.cat = cat;
this.map = map;
}
public Person()
{
}
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
{
return map;
}
public void setMap(Map
{
this.map = map;
}
}
OgnlAction.java
package com.shengsiyuan.ognl;
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
{
private String username;
private String passwod;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPasswod()
{
return passwod;
}
public void setPasswod(String passwod)
{
this.passwod = passwod;
}
private Map
private Map
private Map
public void setRequest(Map
{
System.out.println("setRequest invoked!");
this.requestMap = arg0;
}
public void setApplication(Map
{
this.applicationMap = arg0;
}
public void setSession(Map
{
this.sessionMap = arg0;
}
@Override
public String execute() throws Exception
{
return SUCCESS;
}
}
Struts.xml部分
Ognl.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>