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 ]
posted 18 years ago
only TYPE attribute is not sufficient..u also have to supply either beanName or class attribute
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.
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.
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