Difference between attribute variables and bean properties
posted 10 years ago
What is the difference between attribute variables and bean properties? In HFSJ p 445-446, it explains the <c:set>, where var is used for attribute variables and target is used for bean properties and map values. Aren't attribute variables and bean properties the same thing?
Using "var" would be the same as calling setAttribute. So you could set a value in the session scope, request scope, or any other. Simply think that it is the same as setAttribute, applied on a scope specified with the "scope" attribute. For example, <c:set var="name" scope="session" value="Aaron"/>
You use "property and "target" together, to set a bean's member value, or a Map entry. If the target is a bean, its setter method will be called. For example, MyBean has a member called "String lastName", with a setter called "setLastName(String)". Using the "target" will call the setter. If the "target" is a Map, the put(key,value) method will be called, where key is defined by "property" and value by "value".
To sum up: <c:set var="name" scope="session" value="Aaron"/> -> same as session.setAttribute("name", "Aaron")
<c:set target="myBean" property="lastName" value="John"/> -> same as myBean.setLastName("John")
<c:set target="myMap" property="lastName" value="John"/> -> same as myMap.put("lastName", "John")