Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

struts 1 - Java getter for non-existent attribute of class

I'm using some functionality in Java that I don't really understand so I want to read up on it so that I can use it more effectively. The problem is that I don't know what it is called so it makes it difficult to get more information on it:

I have a class Foo defined like this:

private String _name;
private Bar _bar;
//getters and setters

And Bar:

private String _code;

//getters and setters

public String get_isCodeSmith()
{
      boolean rVal =  _code.toLowerCase().contains("smith");        
      return rVal;
}

Somehow, in my JSP pages (when I have a Session variable called Foo) I am able to write logic tags like this:

<logic:equal name="Foo" property="_bar._isCodeSmith" value="true">

And even though there is no attribute _isCodeSmith in my class Bar, it runs the get_isCodeSmith() method automatically.

What is this called and where can I find out more?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the Javabeans mechanism. Properties are identified not by fields, but by getter (accessor) and / or setter (mutator) methods.

For more technical info, read the JavaBeans spec

Or have a look at this simple test class:

public class TestBean {

    private String complete;
    public String getComplete() { return complete; }
    public void setComplete(final String complete) { this.complete = complete; }

    private String getterOnly;
    public String getGetterOnly() { return getterOnly; }

    private String setterOnly;
    public void setSetterOnly(final String setterOnly) { this.setterOnly = setterOnly; }

    public String getNoBackingField() { return ""; }

}

and the simple JavaBeans analysis:

public class Test {
    public static void analyzeBeanProperties(final Class<?> clazz) throws Exception {
        for (final PropertyDescriptor propertyDescriptor
                : Introspector.getBeanInfo(clazz, Object.class).getPropertyDescriptors()) {
            System.out.println("Property name: " + propertyDescriptor.getName());
            System.out.println("Getter method: " + propertyDescriptor.getReadMethod());
            System.out.println("Setter method: " + propertyDescriptor.getWriteMethod());
            System.out.println();
        }
    }

    public static void main(final String[] args) throws Exception {
        analyzeBeanProperties(TestBean.class);
    }
}

Output:

Property name: complete
Getter method: public java.lang.String test.bean.TestBean.getComplete()
Setter method: public void test.bean.TestBean.setComplete(java.lang.String)

Property name: getterOnly
Getter method: public java.lang.String test.bean.TestBean.getGetterOnly()
Setter method: null

Property name: noBackingField
Getter method: public java.lang.String test.bean.TestBean.getNoBackingField()
Setter method: null

Property name: setterOnly
Getter method: null
Setter method: public void test.bean.TestBean.setSetterOnly(java.lang.String)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...