Get/Set a bean property
2014-01-22 09:30
369 查看
This is an example of how to get and set a bean property. We are using theStatement class.
A Statement object represents a primitive statement in which a single method is applied to a target and a set of arguments. In order to get and set a bean property you should:
Create a simple class,like
setters for the properties.
Create a new object of
Create a new Statement object
for the specified object to invoke the
Call the
and invokes the method on the Bean object.
Create a new Expression.
An Expression object represents a primitive expression in which a single method is applied to a target and a set of arguments to return a result. The new Expression is created with the specified value for the specified
to invoke the
Call
and invokes the method on the
Call
Let’s take a look at the code snippet that follows:
view
source
print?
Output:
This was an example of how to get and set a bean property in Java.
A Statement object represents a primitive statement in which a single method is applied to a target and a set of arguments. In order to get and set a bean property you should:
Create a simple class,like
Beanclass in the example. It has two String properties and getters and
setters for the properties.
Create a new object of
Beanclass.
Create a new Statement object
for the specified object to invoke the
setProperty1method and by a String array of arguments.
Call the
execute()API method of Statement. It finds a method whose name is the same as the
methodNameproperty,
and invokes the method on the Bean object.
Create a new Expression.
An Expression object represents a primitive expression in which a single method is applied to a target and a set of arguments to return a result. The new Expression is created with the specified value for the specified
Beanobject
to invoke the
getProperty1method by the array of arguments.
Call
execute()API method of Expression. It finds a method whose name is the same as the
methodNameproperty,
and invokes the method on the
Beanobject.
Call
getValue()API method of Expression to get the value of the property set in the Expression.
Let’s take a look at the code snippet that follows:
view
source
print?
01 | package com.javacodegeeks.snippets.core; |
02 |
03 | import java.beans.Expression; |
04 | import java.beans.Statement; |
05 |
06 | public class GetSetBeanProperty { |
07 |
08 | public static void main(String[] args) throws Exception { |
09 |
10 | Object o = new Bean(); |
11 |
12 | Statement stmt; |
13 | Expression expr; |
14 |
15 | // Set the value of property1 |
16 | stmt = new Statement(o, "setProperty1" , new Object[]{ "My Prop Value" }); |
17 | stmt.execute(); |
18 |
19 | // Get the value of property1 |
20 | expr = new Expression(o, "getProperty1" , new Object[ 0 ]); |
21 | expr.execute(); |
22 | System.out.println( "Property1 value: " + (String)expr.getValue()); |
23 |
24 | ///////////////////////////////////////////// |
25 |
26 | // Set the value of property2 |
27 | stmt = new Statement(o, "setProperty2" , new Object[]{ new Integer( 345 )}); |
28 | stmt.execute(); |
29 |
30 | // Get the value of property2 |
31 | expr = new Expression(o, "getProperty2" , new Object[ 0 ]); |
32 | expr.execute(); |
33 | System.out.println( "Property2 value: " + (Integer)expr.getValue()); |
34 |
35 | } |
36 |
37 | public static class Bean { |
38 |
39 | // Property property1 |
40 | private String property1; |
41 | // Property property2 |
42 | private int property2; |
43 |
44 | public String getProperty1() { |
45 | return property1; |
46 | } |
47 | public void setProperty1(String property1) { |
48 | this .property1 = property1; |
49 | } |
50 |
51 | public int getProperty2() { |
52 | return property2; |
53 | } |
54 | public void setProperty2( int property2) { |
55 | this .property2 = property2; |
56 | } |
57 |
58 | } |
59 |
60 | } |
Property1 value: My Prop Value Property2 value: 345
This was an example of how to get and set a bean property in Java.
相关文章推荐
- JSP使用get/setProperty时,Bean的scope 改变引起的问题
- jsp的6个动作(<jsp:include><jsp:forward><jsp:param><jsp:useBean><jsp:setProperty><jsp:getProperty>)
- jsp:usebean setProperty getProperty
- javaBean=====jsp标签之<jsp:setProperty> <jsp:getProperty>
- jsp动作标签(userBean get/setProperty)
- JSP使用get/setProperty时,Bean的scope 改变引起的问题
- 关于jsp中使用 <jsp:userBean/> <jsp:setProperty/> <jsp:getProperty/>
- useBean setProperty getPropertyl理解
- JSP标签的<jsp:useBean>和<jsp:setProperty><jsp:getProperty>的使用
- JavaBean与Jsp、在JSP中使用JavaBean(<jsp:useBean>标签、<jsp:setProperty>标签、<jsp:getProperty>标签)
- JavaBean中的get/set 的命名规范
- property_get / property_set
- 对于Name属性没有get/set方法引起的:target is null for setProperty(null, "x", [Ljava.lang.String;@1a48515)异常
- beanutils通过SimpleProperty使用get或set方法赋值
- (9)Tag:property、set、push、bean、if、iterate、date
- property_get/property_set
- property_get/property_set
- 老生常谈--GetROProperty,GetTOProperty,SetTOProperty的区别
- JavaBean中的get/set 的命名规范
- copyPropertie和PropertyUtils.copyProperties() 代替get、set方法及ReflectASM提高效率