Accessing static variable using OGNL in Struts2

newbie picture newbie · Aug 2, 2011 · Viewed 12.1k times · Source

Good day!

I am reading Manning's struts2 book and one of the topic is accessing the static variable using OGNL using the syntax @[fullClassName]@[property or methodCall]

so I tried it on my program and my code is as follows:

BEAN:

public class ContactsBean {

    private static int count = 1;
    //getter and setter
}

ACTION:

private ContactsBean contacts;
//getters and setters

JSP:

   <s:property value="@com.demo.bean.ContactsBean@count" />

or
    <s:property value="@vs@count" />  //valuestack method

but it doesn't work. Am i missing something? Thank you.

Answer

lschin picture lschin · Aug 2, 2011

@see OGNL Basics : Accessing static properties

BEAN :

public class ContactsBean {
    private static int count = 1; 

    // static getter
    // setter
}

<s:property value="@com.demo.bean.ContactsBean@getCount()" />

other case

public class ContactsBean {
    public static final int SCALE = 50;
}

<s:property value="@com.demo.bean.ContactsBean@SCALE" />