📄 package-summary.html
字号:
in <code>Action</code> classes (or in beans or EJBs called by those classes),
rather than being intermixed in your JSP pages.</p>
<h5>The Standard <code><jsp:useBean></code> Tag</h5>
<p>JavaServer Pages (JSP) offers a standard tag, <code><jsp:useBean></code>
that can be used to create a new bean, or introduce a reference to an existing
bean, into a JSP page. Beans (or bean references) introduced through this
mechanism are completely interoperable with beans created by any of the Struts
creation techniques described in this section.</p>
<p>You <strong>must</strong> use <code><jsp:useBean></code> to introduce
a reference to an existing bean, if you wish to reference that bean with
other standard JSP tags (such as <code><jsp:getProperty></code> or <code>
<jsp:setProperty></code>). If you only wish to reference such beans
with other Struts tags, use of <code><jsp:useBean></code> is not required.</p>
<p>For more information about the <code><jsp:useBean></code> tag, see
the JavaServer Pages Specification, available at <a href="http://java.sun.com/products/jsp/download.html">
http://java.sun.com/products/jsp/download.html</a>
.</p>
<h5>The Struts <code><bean:define></code> Tag</h5>
<p>Struts provides a powerful, general purpose, tag (<code><bean:define></code>
) that can be used to create a new bean, in any scope, by copying another
bean (or the value of the property of another bean). This tag supports the
"property" attribute, and therefore all the power of property references,
as discused <a href="#doc.Properties.References">above</a>
. It can be used in a variety of different ways, described further below.
Unless you specify the "toScope" attribute, all defined beans will be created
in page scope.</p>
<p><em>Introduce A String Constant</em> - You can create a new bean that
has a constant String value (or the result of calculating a runtime expression):
</p>
<pre> <bean:define id="foo" value="This is a new String"/><br> <bean:define id="bar" value='<%= "Hello, " + user.getName() %>'/><br> <bean:define id="last" scope="session"<br> value='<%= request.getRequestURI() %>'/><br></pre>
<p><em>Copy An Existing Bean</em> - You can create a new reference to an existing
bean object. You can specify the Java class or interface the new bean is
expected to conform to with the "type" attribute, or accept the default type
of <code>java.lang.Object</code> (this only affects the scripting variable
that is exposed to scriptlets, so it is not generally meaningful in Struts-based
applications).</p>
<pre> <bean:define id="foo" name="bar"/><br> <bean:define id="baz" name="bop" type="com.mycompany.MyBopClass"/><br></pre>
<p><em>Copy An Existing Bean Property</em> - You can create a new bean that
is initialized to the value returned by a property getter. The value of
the "property" attribute can be any simple, nested, or indexed property reference
that follows the rules described earlier. In the first example below, we
also illustrate accessing the property of a request scope bean, and creating
the new bean in session scope (rather than the default page scope).</p>
<pre> <bean:define id="foo" name="bar" property="baz" scope="request"<br> toScope="session"/><br> <bean:define id="bop" name="user" property="role[3].name"/><br></pre>
<h5>Other Struts Copying Tags</h5>
<p>Struts offers a variety of bean creation tags that copy existing beans
(or bean properties) from the environment within which this page is running,
and the request that is currently being processed. Not all of the attributes
for each tag are illustrated in the examples below - see the <a href="../../../../../../userGuide/struts-bean.html">
Bean Tags Reference</a>
for more information. Any bean created by these tags exists only in page
scope, for the remainder of the current page.</p>
<p><em>Copy A Cookie</em> - You can create a new bean containing a <code>
javax.servlet.http.Cookie</code> that was included in the current request.
If no cookie of the specified name was included, a request time expression
will be thrown - therefore, it is common to nest the use of this tag inside
a <code><logic:present cookie="xxx"></code> tag to ensure that the
cookie was really included. If there is the possibility that more than one
cookie of the same name was included, specify the "multiple" attribute (and
the resulting bean will be an array of Cookies, instead of a single Cookie).</p>
<pre> <bean:cookie id="foo" name="cookiename"/><br> <bean:cookie id="all" name="JSESSIONID" multiple="true"/><br></pre>
<p><em>Copy A Request Header</em> - You can create a new bean containing
the value of an HTTP header included in this request. If no header of the
specified name was included, a request time exception will be thrown - therefore,
it is common to nest the use of this tag inside a <code><logic:present
header="xxx"></code> tag to ensure that the header was really included.
If there is the possibility that more than one header of the same name was
included, specify the "multiple" attribute (and the resulting value bean
will be an array of String values, instead of a single String).</p>
<pre> <bean:header id="agent" name="User-Agent"/><br> <bean:header id="languages" name="Accept-Language" multiple="true"/><br></pre>
<p><em>Copy A Dynamically Created Response</em> - You can generate an internal
request to the application you are running, and turn the response data that
is returned from that request into a bean (of type String). One possible
use for this technique is to acquire dynamically created XML formatted data
that will be stored in a bean and later manipulated (such as by applying
an XSLT stylesheet). If the current request is part of a session, the generated
request for the include will also include the session identifier (and thus
be considered part of the same session).</p>
<pre> <bean:include id="text" name="/generateXml?param1=a&param2=b"/><br></pre>
<p><em>Copy A JSP Implicitly Defined Object</em> - You can create a bean
that is one of the JSP implicitly defined objects (see the JSP spec for more
details). This is useful if you wish to perform property getter actions against
the implicit object with a custom tag instead of a scriptlet.</p>
<pre> <bean:page id="app" property="application"/><br> <bean:page id="sess" property="session"/><br></pre>
<p><em>Copy A Request Parameter</em> - You can create a new bean containing
the value of a parameter included in this request. If no parameter of the
specified name was included, a request time exception will be thrown - therefore,
it is common to nest the use of this tag inside a <code><logic:present
parameter="xxx"></code> tag to ensure that the parameter was really included.
If there is the possibility that more than one parameter of the same name
was included, specify the "multiple" attribute (and the resulting value bean
will be an array of String values, instead of a single String).</p>
<pre> <bean:parameter id="name" name="name"/><br> <bean:header id="options" name="option" multiple="true"/><br></pre>
<p><em>Copy a Web Application Resource</em> - You can create a new bean containing
either the value of a web application resource as a String, or a <code>java.io.InputStream</code>
for reading the content of that resource. The resource is accessed with
a context-relative path (beginning with "/"), using the <code>ServletContext.getResource()</code>
or <code>ServletContext.getResourceAsStream()</code> methods on the underlying
application object.</p>
<pre> <bean:resource id="deployment" name="/WEB-INF/web.xml"/><br> <bean:resource id="stream" name="/WEB-INF/web.xml"<br> input="true"/><br></pre>
<p><em>Copy A Struts Configuration Object</em> - You can create a new bean
containing one of the standard Struts framework configuration objects. Doing
this gives you access to the properties of the configuration object, if needed.
</p>
<pre> <bean:struts id="form" formBean="CustomerForm"/><br> <bean:struts id="fwd" forward="success"/><br> <bean:struts id="map" mapping="/saveCustomer"/><br></pre>
<hr> <a name="doc.Output"></a>
<h3>Bean Output</h3>
<p>None of the Struts Bean tags discussed so far render any output to the
response page that is being generated from this JSP page. They are executed
in order to make relevant Java objects visible as beans for further manipulation.
The following tags cause output to be written to the response, and therefore
made visible to the ultimate requester.</p>
<p><em>Render An Internationalized Message</em> - You can specify a message
key (with optional parameter replacement objects) that are passed to a <a href="../../util/MessageResources.html">
MessageResources</a>
object that returns the corresponding message text. The message text will
be copied to the response currently being created. By default, messages
are looked up in the application resources bundle that is initialized for
you (as an application scope bean) by the Struts controller servlet, using
the Locale must recently stored in the user's session. These defaults can
be overridden by setting values for the "bundle" and "locale" attributes,
as described in the <a href="../../../../../../userGuide/struts-bean.html#message">
Bean Tags Reference</a>
. </p>
<pre> <bean:message key="label.Cancel"/><br> <bean:message key="message.hello" arg0='<%= user.getFullName() %>'/><br></pre>
<p><em>Render A Bean or Bean Property</em> - The contents of a bean, or bean
property, are converted to a String and then copied to the response currently
being created. This tag understands the syntax for simple, nested, and indexed
property references described <a href="#doc.Properties.References"> above</a>
. Beans from any scope can be requested - by default, the scopes are searched
in expanding visibility order (page, request, session, and application) to
locate the requested bean.</p>
<pre> <bean:write name="username"/><br> <bean:write name="user" property="fullName"/><br> <bean:write name="customer" property="orders[2].partNumber"<br> scope="session"/><br></pre>
<P>
<HR>
<!-- ========== START OF NAVBAR ========== -->
<A NAME="navbar_bottom"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0">
<TR>
<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_bottom_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3">
<TR ALIGN="center" VALIGN="top">
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD>
<TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Package</B></FONT> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <FONT CLASS="NavBarFont1">Class</FONT> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-use.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
</EM>
</TD>
</TR>
<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../../../../org/apache/struts/taglib/package-summary.html"><B>PREV PACKAGE</B></A>
<A HREF="../../../../../org/apache/struts/taglib/html/package-summary.html"><B>NEXT PACKAGE</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../../../../index.html" TARGET="_top"><B>FRAMES</B></A>
<A HREF="package-summary.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD>
</TR>
</TABLE>
<!-- =========== END OF NAVBAR =========== -->
<HR>
Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -