Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaBean

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I think that I can create a javabean only whit properties id and type like this:
<jsp:useBean id="pref" tye="com.abc.Preferences"/>
But I found this question:
Assuming that com.abc.Preferences is not an interface or an abstract class and that 'pref' is not already instantiated in any scope, which of the given options correctly declare a useBean tag?
And <jsp:useBean id="pref" tye="com.abc.Preferences"/> is a bad option. Why?? I am wrong??
Thank you in advance.
[ April 18, 2002: Message edited by: Jordi Marqu�s ]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only TYPE attribute is not sufficient..u also have to supply either beanName or class attribute
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With only type-attribute you can only get allready instantiated beans
(for example instantiate the bean in a helper-class of the controller and assign it to the id variable in the jsp).
To instantiate a new bean, you need class or beanName also.
This sounds logical to me: To really instantiate an object you need the real class and type might be a supertype of the class of the object you want to instantiate.
 
Garann Means
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When a page uses <jsp:useBean>, when does the bean get instantiated? At that line?
I am trying to use an overloaded constructor to set instance variables from the constructor. Am I allowed to do that with a bean, or will I need to call setters in the bean class?
Thanks!
g.
 
Jignesh Malavia
Author
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garann Rose Means:
When a page uses <jsp:useBean>, when does the bean get instantiated? At that line?

Yes, the bean gets created at the point where the useBean tag occurs, provided
a) The bean with the specified id is not already present in the specified scope and
b) Either the class attribute or the beanName attribute is provided
Originally posted by Garann Rose Means:
I am trying to use an overloaded constructor to set instance variables from the constructor. Am I allowed to do that with a bean, or will I need to call setters in the bean class?

useBean action uses the default constructor. But you can initialize a newly created bean either by using the setter methods in a scriptlet or by using the setProperty action within the body of the useBean action
<jsp:useBean id="aBean" class="MyBeanClass">
<jsp:setProperty name="aBean" property="aProperty" value="aValue"/>
</jsp:useBean>
For a detailed explanation of the useBean, setProperty & getProperty actions, have a look at the sample chapter at http://www.manning.com/deshmukh/chap14.pdf Initialization is explained in section "Initializing bean properties" on page 262
HTH
-j
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read it, you'll not regret if you do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic