📄 package-summary.html
字号:
- Extended syntax to refer to JavaBean properties with simple names
(same as the standard JSP tags <code><jsp:getProperty></code> and
<code><jsp:setProperty></code>), nested names (a property named
<code>address.city</code> returns the value retrieved by the Java
expression <code>getAddress().getCity()</code>), and indexed names (a
property named <code>address[3]</code> retrieves the fourth address from
the indexed "address" property of a bean).</li>
<li><a href="#doc.Creation">Bean Creation</a>
- New JSP beans, in any scope, can be created from a variety of objects
and APIs associated with the current request, or with the servlet container
in which this page is running.</li>
<li><a href="#doc.Output">Bean Output</a>
- Supports the rendering of textual output from a bean (or bean property),
which will be included in the response being created by your JSP page.</li>
</ul>
<p>See the <a href="../../../../../../userGuide/struts-bean.html">Bean Tags Reference</a>
for detailed information about the available tags in this tag library, and
the valid attributes for each tag.<br>
<img src="doc-files/beanUML.gif" alt="Bean Tag UML">
<br>
</p>
<a name="doc.Properties"></a>
<h3>Bean Properties</h3>
<h5>Common Tag Attributes</h5>
<p>The tags in the "struts-bean" tag library (and, generally, in all tag libraries
included with the Struts framework) share a common set of tag attributes
that have the same meaning, no matter what tag they are used on. These common
attributes include:</p>
<ul>
<li><em>id</em> - Names the scripting variable that will be created
by this custom tag, as well as the key value used to locate this bean
in the scope defined by the <code>scope</code> attribute.</li>
<li><em>name</em> - Defines the key value by which an existing bean will
be looked up in the scope defined by the <code>scope</code> attribute
(if any), or by searching through the various scopes in the standard
order (page, request, session, application).</li>
<li><em>property</em> - Defines the name of a JavaBeans property, of the
JSP bean identified by the <code>name</code> and (optional) <code>scope</code>
attributes, whose value is to be used by this custom tag. If not
specified, the bean identified by <code>name</code> is itself used
as the value of interest. See below for more discussion about how a
property can be referenced.</li>
<li><em>scope</em> - Identifies the JSP scope ("page", "request", "session",
or "application" within which a particular bean will be searched for
(under the key specified by the <code>name</code> attribute) or created
(under the key specified by the <code>id</code> attribute). If not
specified, beans will generally be searched for in the order listed above,
or created in page scope.</li>
</ul>
<a name="doc.Properties.References"></a>
<h5>Property References</h5>
<p>Struts tags that support the <code>property</code> tag generally also recognize
a rich syntax for getting and setting properties. There are three types
of references supported: simple, nested, and indexed.</p>
<p><em>Simple References</em> - These are equivalent to the syntax you use
with the standard <code><jsp:getProperty></code> and <code><jsp:setProperty></code>
tags. A reference to a property named "foo" is converted into a method
call to <code>getFoo()</code> or <code>setFoo(value)</code> (as appropriate),
using the standard JavaBeans Specification naming conventions for bean properties.
Struts uses the standard Java introspection APIs to identify the names of
the actual property getter and setter methods, so your beans can provided
customized method names through the use of a <code>BeanInfo</code> class.
See the JavaBeans Specification, available at <a href="http://java.sun.com/products/javabeans/">
http://java.sun.com/products/javabeans/</a>
, for more information.</p>
<p><em>Nested References</em> - Nested references are used to access a property
through a hierarchy of property names separated by periods ("."), similar
to the way that nested properties are accessed in JavaScript. For example,
the following property reference in a getter (such as the <code><bean:define></code>
tag discussed below):</p>
<pre> property="foo.bar.baz"<br></pre>
<p>is translated into the equivalent the Java expression:</p>
<pre> getFoo().getBar().getBaz()<br></pre>
<p>If a nested reference is used in a setter (such as when an input form
is processed), the property setter is called on the <strong>last</strong>
property in the chain. For the above property reference, the equivalent
Java expression would be:</p>
<pre> getFoo().getBar().setBaz(value)<br></pre>
<p><em>Indexed References</em> - Subscripts can be used to access individual
elements of properties whose value is actually an array, or whose underlying
JavaBean offers indexed getter and setter methods. For example, the following
property reference in a getter (such as the <code><bean:define></code>
tag discussed below):</p>
<pre> property="foo[2]"<br></pre>
<p>is translated into the equivalent of the Java expression:</p>
<pre> getFoo(2);<br></pre>
<p>while the same property reference in a setter would call the equivalent
of:</p>
<pre> setFoo(2, value)<br></pre>
<p>As you can see from the above translations, the subscripts used in indexed
references are <strong>zero relative</strong> (that is, the first element
in an array is <code>foo[0]</code>), just as is true in the Java language.</p>
<p><em>Combined References</em> - Nesting and indexing can be combined in
arbitrary ways, so that expressions like <code>foo.bar[0].baz[2]</code> are
legal. You must be careful, of course, to ensure that the actual beans being
accessed by these references have properties of the appropriate names and
types. Otherwise, JSP runtime exceptions will be thrown.</p>
<p>See the JavaDocs for <a href="http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/PropertyUtils.html">PropertyUtils</a>
for more detailed information about the mechanisms that Struts uses to access
properties in a general way, through Java reflection APIs.</p>
<hr> <a name="doc.Creation"></a>
<h3>Bean Creation</h3>
<h5>Introduction</h5>
<p>New beans can be created, and introduced into one of the four standard
JSP scopes (page, request, session, and application) through a variety of
techniques. The following subsections describe the use of the following approaches:</p>
<ul>
<li>Java Code in Action Classes</li>
<li>Java Code in Scriptlets</li>
<li>The Standard <code><jsp:useBean></code> Tag</li>
<li>The Struts <code><bean:define></code> Tag</li>
<li>Other Struts Copying Tags</li>
</ul>
<h5>Java Code in Action Classes</h5>
<p>Because the JSP pages are compiled into Servlets, your <code>Action</code>
classes that are invoked by the Struts controller servlet have convenient
access to three of the four standard JSP scopes (request, session, and application).
It is very common practice for the business logic contained in your <code>
Action</code> class to create results that are stored in request or session
scope, which will be used by a JSP page you forward control to in rendering
the next page of the user interface.</p>
<p><em>Request Scope</em> - To store a bean in request scope under name "cust",
your <code>Action</code> class would execute code similar to this:</p>
<pre> Customer customer = ... create or acquire a customer reference ...;<br> request.setAttribute("cust", customer);<br></pre>
<p><em>Session Scope</em> - To store a bean in session scope under name "user"
(perhaps in a logon action), your <code>Action</code> class would execute
code similar to this:</p>
<pre> User user = ... look up valid user in the database ...;<br> HttpSession session = request.getSession();<br> session.setAttribute("user", user);<br></pre>
<p><em>Application Scope</em> - Generally, application scope beans are initialized
in the <code>init()</code> method of a startup servlet. However, it is legal
for an <code>Action</code> class to create such beans, if this is appropriate,
like this:</p>
<pre> Foo foo = ... create a Foo ...;<br> servlet.getServletContext().setAttribute("foo", foo);<br></pre>
<h5>Java Code in Scriptlets</h5>
<p>While it is not a recommended practice in Struts-based applications (because
developers will be tempted to mix business logic and presentation logic in
their JSP pages), it is legal for scriptlet code in a JSP page to create
new JavaBeans dynamically, and add them to any of the four possible scopes,
as demonstrated in the code examples below:</p>
<p><em>Page Scope</em> - To store a bean in page scope under name "foo", your
scriptlet must execute code like this:</p>
<pre><%<br> Foo foo = ... create a foo ...;<br> pageContext.setAttribute("foo", foo, PageContext.PAGE_SCOPE);<br>%><br></pre>
<p><em>Request Scope</em> - To store a bean in request scope under name "cust",
your scriplet must execute code like this:</p>
<pre><%<br> Customer customer = ... create or acquire a customer reference ...;<br> pageContext.setAttribute("cust", customer, PageContext.REQUEST_SCOPE);<br>%><br></pre>
<p><em>Session Scope</em> - To store a bean in session scope under name "user",
(perhaps as a result of a validated login), your scriplet must execute code
like this:</p>
<pre><%<br> User user = ... look up valid user in the database ...;<br> pageContext.setAttribute("user", user, PageContext.SESSION_SCOPE);<br>%><br></pre>
<p><em>Application Scope</em> - Generally, application scope beans are initialized
in the <code>init()</code> method of a startup servlet. However, a scriptlet
can create such beans, if appropriate, like this:</p>
<pre><%<br> Foo foo = ... create a Foo ...;<br> pageContext.setAttribute("foo", foo, PageContext.APPLICATION_SCOPE);<br>%><br></pre>
<p><strong>NOTE</strong> - As mentioned above, using scriptlets in your JSP
pages is strongly discouraged in a Struts based application, unless you are
executing code that is <strong>only</strong> related to presentation of existing
data. In general, your application's processing logic should be encapsulated
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -