struts2_11.2_OGNL(2)

Cat.java

public class Cat {
    private static String color="RED";
    private String name;
    private int age;
    private Dog dog;
    public Cat() {}
    public Cat(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    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;
    }

    public String miaomiao(){
        return "喵喵";
    }
    public static String miaomiao1(){
        return "static喵喵";
    }

    @Override
    public String toString() {
        return this.name;
    }
}

Dog.java

public class Dog {
    public static String color="GREEN";
    private String name;
    private int age;

    public Dog() {
        // TODO Auto-generated constructor stub
    }


    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }


    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 wangwang(){
        return "汪汪";
    }

    public static String wangwang1(){
        return "static汪汪";
    }

    @Override
    public String toString() {
        return this.name;
    }
}

User.java

public class User {
    private String name;
    private int age;

    public User() {

    }

    public User(String name) {
        this.name = name;
    }

    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;
    }

    @Override
    public String toString() {
        return this.name;
    }
}

animalAction .java

public class animalAction extends ActionSupport{

    private User user;
    private List users = new ArrayList<>();
    private Dog dog;
    private Set dogs = new HashSet<>();
    private Cat cat;
    private Map cats = new HashMap<>();

    public animalAction() {
        users.add(new User("小明"));
        users.add(new User("小红"));
        users.add(new User("小刚"));

        dogs.add(new Dog("金毛",1));
        dogs.add(new Dog("萨摩",2));
        dogs.add(new Dog("藏獒",3));

        cats.put("Jerry", new Cat("大脸猫",4));
        cats.put("Tom", new Cat("小奶猫",5));
        cats.put("Jack", new Cat("长尾猫",6));
    }



    public String excute(){
        return SUCCESS;
    }



    public List getUsers() {
        return users;
    }



    public void setUsers(List users) {
        this.users = users;
    }



    public Dog getDog() {
        return dog;
    }



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



    public Set getDogs() {
        return dogs;
    }



    public void setDogs(Set dogs) {
        this.dogs = dogs;
    }



    public Map getCats() {
        return cats;
    }



    public void setCats(Map cats) {
        this.cats = cats;
    }



    public User getUser() {
        return user;
    }



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



    public Cat getCat() {
        return cat;
    }



    public void setCat(Cat cat) {
        this.cat = cat;
    }



}

OGNL2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<hr>
cat age:<s:property value="cat.age"/>br>
dog age:<s:property value="dog.age"/>br>
值栈中对象的普通的方法:<s:property value="cat.miaomiao()"/>br>
值栈中对象的普通的方法:<s:property value="dog.wangwang()"/>br>
值栈中action的方法:<s:property value="excute()"/>br>
<hr>
访问普通类的静态方法:<s:property value="@com.test.dog.Dog@wangwang1()"/>br>
访问普通类的静态变量:<s:property value="@com.test.dog.Dog@color"/>br>
访问Math类的静态方法:<s:property value="@@max(2,3)"/>br>
<hr>
访问普通类的构造方法:<s:property value="new com.test.dog.Dog('阿黄',12)"/>br>

<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="cats"/>br>
访问Map中某个元素:<s:property value="cats.Jerry"/> | <s:property value="cats['Jerry']"/> | <s:property value="cats[\"Jerry\"]"/>br>
访问Map中所有的key:<s:property value="cats.keys"/>br>
访问Map中所有的value:<s:property value="cats.values"/>br>
访问容器的大小:<s:property value="cats.size()"/> | <s:property value="users.size"/>br>
<hr />
投影(过滤):<s:property value="cats.{?#this.age==4}[0]"/>br>
投影:<s:property value="cats.{^#this.age>1}.{age}"/>br>
投影:<s:property value="cats.{$#this.age>1}.{age}"/>br>
投影:<s:property value="cats.{$#this.age>1}.{age} == null"/>br>
<hr />
[]:<s:property value="[0]"/>
<s:debug>s:debug>
<hr>
body>
html>

struts.xml



<struts>
    <constant name="struts.devMode" value="true" />

    <constant name="struts.i18n.encoding" value="utf-8" />
    <constant name="struts.ognl.allowStaticMethodAccess" value="true" />
    <package name="OGNL2" namespace="/OGNL2" extends="struts-default">
        <action name="OGNL2" class="com.test.action.animalAction" method="excute">
            <result>/OGNL2.jspresult>
        action>
    package>
struts>

你可能感兴趣的:(structs)