Preface

Comment

Declaration

Expression

Scriptlet

EL Expression

Directives

Attribute Directive

Include Directive

Page Directive

Tag Directive

Taglib Directive

Variable Directive

Standard Actions

<jsp:attribute>

<jsp:body>

<jsp:element>

<jsp:doBody>

<jsp:forward>

<jsp:getProperty>

<jsp:include>

<jsp:invoke>

<jsp:output>

<jsp:plugin>

<jsp:root>

<jsp:setProperty>

<jsp:text>

<jsp:useBean>

<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" ]|	
   beanName="{package.class | '${' Expression '}' | 	
      <%= expression %>}" type="package.class" | 	
   type="package.class"	
}	
{ /> | > other elements </jsp:useBean> }

XML Syntax

<jsp:useBean id="beanInstanceName"	
   scope="page|request|session|application"	
{	
   class="package.class"  [ type="package.class" ]  |	
   beanName="{package.class | '${' Expression '}' | 	
      %= expression %}" type="package.class" |	
      type="package.class"	
}	
{ /> | > other elements </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> element locates or instantiates a JavaBeans component. <jsp:useBean> first attempts to locate an instance of the bean. If the bean does not exist, <jsp:useBean> instantiates it from a class or serialized template.

To locate or instantiate the bean, <jsp:useBean> takes the following steps, in this order:

  1. Attempts to locate a bean with the scope and name you specify.
  2. Defines an object reference variable with the name you specify.
  3. If it finds the bean, stores a reference to it in the variable. If you specified type, gives the bean that type.
  4. 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.
  5. If jsp:useBean has instantiated (rather than located) the bean, and if it has body tags or elements (between <jsp:useBean> and </jsp:useBean>), executes the body tags.

The body of a jsp:useBean element often contains a jsp:setProperty element that sets property values in the bean. 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.

You can use a jsp:useBean element to locate or instantiate a JavaBeans component, but not an enterprise bean. To create enterprise beans, you can write a jsp:useBean element that calls a bean that in turn calls the enterprise bean, or you can write a custom tag that calls an enterprise bean directly.

Attributes and Usage

See Also



宏飞网络是你学习web开发、测试web程序实例、和培养职业技能的首选网站。我们提供例子也许有些简单,但对理解基本概念有帮助。

我们尽量避免在教程、参考及例子中出现错误,但不能保证所有的内容都是正确的。

你使用本网站时,我们默认你已经阅读并接受了我们的隐私政策。

Copyright 2003-2011宏飞网络 版权所有