BBS水木清华站∶精华区

发信人: agents (智能代理), 信区: Java        
标  题: JSP语法(14) 
发信站: BBS 水木清华站 (Tue Jan  2 14:56:06 2001) 
 
 
【<jsp:useBean>】   
  
Locates or instantiates a Bean with a specific name and scope. 
 
▲[JSP Syntax] 
 
<jsp:useBean 
  id="beanInstanceName" 
  scope="page|request|session|application" 
 { class="package.class" | 
  type="package.class" | 
  class="package.class" type="package.class" | 
  beanName="{ package.class | <%= expression %> }" type="package.class" 
 } 
 {  /> | 
  > other tags </jsp:useBean> 
 } 
 
▲[Examples] 
 
<jsp:useBean id="cart" scope="session" class="session.Carts" /> 
<jsp:setProperty name="cart" property="*" /> 
<jsp:useBean id="checking" scope="session" class="bank.Checking" > 
<jsp:setProperty name="checking" property="balance" value="0.0" /> 
</jsp:useBean> 
 
▲[Description] 
The <jsp:useBean> tag attempts to locates a Bean, or if the Bean does not  
exist, instantiates it from a class or serialized template. To locate or  
instantiate the Bean, <jsp:useBean> takes the following steps, in this order: 
Attempts to locate a Bean with the scope and name you specify. 
Defines an object reference variable with the name you specify. 
 
If it finds the Bean, stores a reference to it in the variable. If you  
specified type, gives the Bean that type. 
 
If it does not find the Bean, instantiates it from the class you specify,  
storing a reference to it in the new variable. If the class name represents  
a serialized template, the Bean is instantiated by java.beans.Beans. 
instantiate. 
 
If it has instantiated (rather than located) the Bean, and if it has body  
tags (between <jsp:useBean> and </jsp:useBean>), executes the body tags. 
 
The body of a <jsp:useBean> tag often contains a <jsp:setProperty> tag that  
defines property values in the object. As described in Step 5, the body tags 
are only processed if <jsp:useBean> instantiates the Bean. If the Bean  
already exists and <jsp:useBean> locates it, the body tags have no effect. 
 
In JSP 1.0, <jsp:useBean> works with JavaBeans components, but not with ente 
rprise beans. If you want to use enterprise beans, you can write a JSP file  
that constructs a JavaBean component, and have the JavaBean component call t 
he enterprise bean. 
【Attributes and Usage】 
 
(1) id="beanInstanceName" 
Names a variable that identifies the Bean in the scope you specify. You can  
use the variable name in expressions or scriptlets in the same JSP file. 
The name is case sensitive and must conform to the naming conventions of the 
page scripting language (if you use the Java programming language, the  
conventions in the Java Language Specification). If the Bean has already been  
created by another <jsp:useBean> tag, the value of id must match the value of  
id used in the original <jsp:useBean> tag. 
 
(2) scope="page|request|session|application" 
Defines a scope in which the Bean exists and the variable named in id is  
available. The default value is page. 
 
(3)class="package.class" 
Instantiates a Bean from a class, using the new keyword and the class constr 
uctor. The class must not be abstract and must have a public, no-argument co 
nstructor. The package and class name are case sensitive. 
 
(4)class="package.class" type="package.class" 
Instantiates a Bean from a class, using the new keyword and the class  
constructor, and gives the Bean the type you specify in type. The class you  
specify in class must not be abstract and must have a public, no-argument  
constructor. The package and class names you use with both class and type are  
case sensitive. 
The value of type can be the same as class, a superclass of class, or an int 
erface implemented by class. 
 
(5) beanName="{ package.class | <%= expression %> }" type="package.class" 
Instantiates a Bean from either a class or a serialized template, using the  
java.beans.Beans.instantiate method, and gives the Bean the type specified  
in type. Beans.instantiate checks whether a name represents a class or a seri 
alized template. If the Bean is a serialized template, Beans.instantiate rea 
ds the serialized form (with a name like package.class.ser) using a class lo 
ader. For more information, see the JavaBeans API Specification. 
The value of beanName is either a package and class name or an Expression th 
at evaluates to a package and class name, and is passed to Beans.instantiate 
. The value of type is a package and class name. The value of type can be  
the same as beanName, a superclass of beanName, or an interface implemented by 
beanName. 
With both beanName and type, the package and class names are case sensitive. 
 
(6)type="package.class" 
If the Bean already exists in the specified scope, gives the Bean the type  
you specify. If you use type without class or beanName, no Bean is  
instantiated. The package and class name are case sensitive. 
 
 
-- 
 
※ 修改:·agents 於 Jan  2 17:21:41 修改本文·[FROM:   166.111.54.22] 
※ 来源:·BBS 水木清华站 smth.org·[FROM: 166.111.54.22] 

BBS水木清华站∶精华区