• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between attribute variables and bean properties

 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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")
 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much! I hope I remember that for my upcoming exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic