syntaxref.fm14.html
来自「这是java源码。」· HTML 代码 · 共 112 行
HTML
112 行
<html><head><title></title></head><body bgcolor=#ffffff> <a href="tags.html">[Top]</a> <a href="syntaxref.fm13.html">[Prev]</a> [Next] <a href="Copyright.fm.html">[Bottom]</a><hr><br> <a name="8865"> </a><h2 align="center"> <jsp:useBean></h2><p><a name="9807"> </a>Locates or instantiates a Bean with a specific name and scope.</p><a name="9348"> </a><h4> JSP Syntax</h4><blockquote><pre><code><jsp:useBean </code> <code>id="</code><em>beanInstanceName</em><code>"</code> <code>scope="</code><strong>page</strong><code>|request|session|application"</code> <code>{ class="</code><em>package.class</em><code>" |</code> <code>type="</code><em>package.class</em><code>" |</code> <code>class="</code><em>package.class</em><code>" type="</code><em>package.class</em><code>" |</code> <code>beanName="{ </code><em>package.class</em><em> </em><code>|</code><em> </em><code><%= </code><em>expression</em> <code>%> }" type="</code><em>package.class</em>" } { <code>/> |</code> <code>> </code><em>other tags</em><code> </jsp:useBean></code> <code> }</code></pre></blockquote><a name="16474"> </a><h4> Examples</h4><blockquote><pre><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></pre></blockquote><a name="9356"> </a><h4> Description</h4><p><a name="11782"> </a>The <code><jsp:useBean></code> 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, <code><jsp:useBean></code> takes the following steps, in this order:</p><ol><a name="9817"> </a><li>Attempts to locate a Bean with the scope and name you specify.<a name="9838"> </a><li>Defines an object reference variable with the name you specify.<a name="9839"> </a><li>If it finds the Bean, stores a reference to it in the variable. If you specified <code>type</code>, gives the Bean that type.<a name="9840"> </a><li>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 <code>java.beans.Beans.instantiate</code>.<a name="9843"> </a><li>If it has <em>instantiated</em> (rather than located) the Bean, and if it has body tags (between <code><jsp:useBean></code> and <code></jsp:useBean></code>), executes the body tags.</ol><p><a name="18117"> </a>The body of a <code><jsp:useBean></code> tag often contains a <code><jsp:setProperty></code> tag that defines property values in the object. As described in <a href="syntaxref.fm14.html#9843">Step  5</a>, the body tags are only processed if <code><jsp:useBean></code> instantiates the Bean. If the Bean already exists and <code><jsp:useBean></code> locates it, the body tags have no effect.</p><p><a name="15594"> </a>In JSP 1.0, <code><jsp:useBean></code> works with JavaBeans components, but not with enterprise 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 the enterprise bean.</p><a name="10963"> </a><h4> Attributes and Usage</h4><ul><a name="10964"> </a><li><code>id="</code><em>beanInstanceName</em><code>"</code><p><a name="10965"> </a>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. </p><p><a name="18001"> </a>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 <em>Java Language Specification</em>). If the Bean has already been created by another <code><jsp:useBean></code> tag, the value of <code>id</code> must match the value of <code>id</code> used in the original <code><jsp:useBean></code> tag. </p><a name="10966"> </a><li><code>scope="</code><strong>page</strong><code>|request|session|application"</code><p><a name="10967"> </a>Defines a scope in which the Bean exists and the variable named in <code>id</code> is available. The default value is <code>page</code>.</p><a name="10968"> </a><li><code>class="</code><em>package.class</em><code>"</code><p><a name="10969"> </a>Instantiates a Bean from a class, using the <code>new</code> keyword and the class constructor. The class must not be abstract and must have a public, no-argument constructor. The package and class name are case sensitive.</p><a name="18019"> </a><li><code>class="</code><em>package.class</em><code>" type="</code><em>package.class</em><code>"</code><p><a name="18081"> </a>Instantiates a Bean from a class, using the <code>new</code> keyword and the class constructor, and gives the Bean the type you specify in <code>type</code>. The class you specify in <code>class</code> must not be abstract and must have a public, no-argument constructor. The package and class names you use with both <code>class</code> and <code>type</code> are case sensitive.</p><p><a name="18098"> </a>The value of <code>type</code> can be the same as <code>class</code>, a superclass of <code>class</code>, or an interface implemented by <code>class</code>. </p><a name="15804"> </a><li><code>beanName="{ </code><em>package.class</em><code> | <%= </code><em>expression</em><code> %> }" type="</code><em>package.class</em><code>"</code><p><a name="15805"> </a>Instantiates a Bean from either a class or a serialized template, using the <code>java.beans.Beans.instantiate</code> method, and gives the Bean the type specified in <code>type</code>. <code>Beans.instantiate</code> checks whether a name represents a class or a serialized template. If the Bean is a serialized template, <code>Beans.instantiate</code> reads the serialized form (with a name like <em>package.class.ser</em>) using a class loader. For more information, see the <em>JavaBeans API Specification</em>.</p><p><a name="18056"> </a>The value of <code>beanName</code> is either a package and class name or an <a href="syntaxref.fm4.html#11258">Expression</a> that evaluates to a package and class name, and is passed to <code>Beans.instantiate</code>. The value of <code>type</code> is a package and class name. The value of <code>type</code> can be the same as <code>beanName</code>, a superclass of <code>beanName</code>, or an interface implemented by <code>beanName</code>.</p><p><a name="18181"> </a>With both <code>beanName</code> and <code>type</code>, the package and class names are case sensitive.</p><a name="18092"> </a><li><code>type="</code><em>package.class</em><code>"</code><p><a name="18093"> </a>If the Bean already exists in the specified scope, gives the Bean the type you specify. If you use <code>type</code> without <code>class</code> or <code>beanName</code>, no Bean is instantiated. The package and class name are case sensitive.</p></ul><a name="9359"> </a><h4> See Also</h4><ul><a name="9360"> </a><li><code><a href="syntaxref.fm13.html#8856"><jsp:setProperty></a></code><a name="9361"> </a><li><code><a href="syntaxref.fm10.html#8820"><jsp:getProperty></a></code><a name="18068"> </a><li>Javadoc API reference for <code>java.beans.Beans</code><a name="18111"> </a><li><em>JavaBeans API Specification</em></ul><p><a name="7941"> </a></p><hr><br> <a href="tags.html">[Top]</a> <a href="syntaxref.fm13.html">[Prev]</a> [Next] <a href="Copyright.fm.html">[Bottom]</a><hr><br><i>Copyright © 1999, Sun Microsystems, Inc. All rightsreserved.</i></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?