The "struts-bean" tag library contains JSP custom tags useful in defining
new beans (in any desired scope) from a variety of possible sources, as well
as a tag to render a particular bean (or bean property) to the output response.
The "struts-bean" tag library contains JSP custom tags useful in defining
new beans (in any desired scope) from a variety of possible sources, as well
as a tag to render a particular bean (or bean property) to the output response.
Introduction
Much of the power of JavaServer Pages (JSP) technology comes from the
simple and powerful mechanisms by which the servlet that is generated automatically
from your JSP source page can interact with JavaBeans that represent the computational
state of your application. In standard JSP pages, the
<jsp:useBean>
tag is used create a bean (if necessary), as well as a "scripting variable"
that can be used within scriptlets to refer to these beans.
The "struts-bean" tag library provides substantial enhancements to the
basic capability provided by
<jsp:useBean>
, as discussed
in the following sections:
- Bean Properties
- Extended syntax to refer to JavaBean properties with simple names
(same as the standard JSP tags
<jsp:getProperty>
and
<jsp:setProperty>
), nested names (a property named
address.city
returns the value retrieved by the Java
expression getAddress().getCity()
), and indexed names (a
property named address[3]
retrieves the fourth address from
the indexed "address" property of a bean). - Bean Creation
- 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.
- Bean Output
- 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.
See the
Bean Tags Reference
for detailed information about the available tags in this tag library, and
the valid attributes for each tag.

Bean Properties
Common Tag Attributes
- id - 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
scope
attribute. - name - Defines the key value by which an existing bean will
be looked up in the scope defined by the
scope
attribute
(if any), or by searching through the various scopes in the standard
order (page, request, session, application). - property - Defines the name of a JavaBeans property, of the
JSP bean identified by the
name
and (optional) scope
attributes, whose value is to be used by this custom tag. If not
specified, the bean identified by name
is itself used
as the value of interest. See below for more discussion about how a
property can be referenced. - scope - Identifies the JSP scope ("page", "request", "session",
or "application" within which a particular bean will be searched for
(under the key specified by the
name
attribute) or created
(under the key specified by the id
attribute). If not
specified, beans will generally be searched for in the order listed above,
or created in page scope.
Property References
property
Simple References<jsp:getProperty>
<jsp:setProperty>
getFoo()
setFoo(value)
BeanInfo
http://java.sun.com/products/javabeans/Nested References<bean:define>
property="foo.bar.baz"
getFoo().getBar().getBaz()
last getFoo().getBar().setBaz(value)
Indexed References<bean:define>
property="foo[2]"
getFoo(2);
setFoo(2, value)
zero relativefoo[0]
Combined Referencesfoo.bar[0].baz[2]
PropertyUtils
Bean Creation
Introduction
- Java Code in Action Classes
- Java Code in Scriptlets
- The Standard
<jsp:useBean>
Tag - The Struts
<bean:define>
Tag - Other Struts Copying Tags
Java Code in Action Classes
Action
Action
Request ScopeAction
Customer customer = ... create or acquire a customer reference ...;
request.setAttribute("cust", customer);
Session ScopeAction
User user = ... look up valid user in the database ...;
HttpSession session = request.getSession();
session.setAttribute("user", user);
Application Scopeinit()
Action
Foo foo = ... create a Foo ...;
servlet.getServletContext().setAttribute("foo", foo);
Java Code in Scriptlets
Page Scope<%
Foo foo = ... create a foo ...;
pageContext.setAttribute("foo", foo, PageContext.PAGE_SCOPE);
%>
Request Scope<%
Customer customer = ... create or acquire a customer reference ...;
pageContext.setAttribute("cust", customer, PageContext.REQUEST_SCOPE);
%>
Session Scope<%
User user = ... look up valid user in the database ...;
pageContext.setAttribute("user", user, PageContext.SESSION_SCOPE);
%>
Application Scopeinit()
<%
Foo foo = ... create a Foo ...;
pageContext.setAttribute("foo", foo, PageContext.APPLICATION_SCOPE);
%>
NOTEonlyAction
The Standard <jsp:useBean>
Tag
<jsp:useBean>
must<jsp:useBean>
<jsp:getProperty>
<jsp:setProperty>
<jsp:useBean>
<jsp:useBean>
http://java.sun.com/products/jsp/download.htmlThe Struts <bean:define>
Tag
<bean:define>
aboveIntroduce A String Constant <bean:define id="foo" value="This is a new String"/>
<bean:define id="bar" value='<%= "Hello, " + user.getName() %>'/>
<bean:define id="last" scope="session"
value='<%= request.getRequestURI() %>'/>
Copy An Existing Beanjava.lang.Object
<bean:define id="foo" name="bar"/>
<bean:define id="baz" name="bop" type="com.mycompany.MyBopClass"/>
Copy An Existing Bean Property <bean:define id="foo" name="bar" property="baz" scope="request"
toScope="session"/>
<bean:define id="bop" name="user" property="role[3].name"/>
Other Struts Copying Tags
Bean Tags ReferenceCopy A Cookie
javax.servlet.http.Cookie
<logic:present cookie="xxx">
<bean:cookie id="foo" name="cookiename"/>
<bean:cookie id="all" name="JSESSIONID" multiple="true"/>
Copy A Request Header<logic:present
header="xxx">
<bean:header id="agent" name="User-Agent"/>
<bean:header id="languages" name="Accept-Language" multiple="true"/>
Copy A Dynamically Created Response <bean:include id="text" name="/generateXml?param1=a¶m2=b"/>
Copy A JSP Implicitly Defined Object <bean:page id="app" property="application"/>
<bean:page id="sess" property="session"/>
Copy A Request Parameter<logic:present
parameter="xxx">
<bean:parameter id="name" name="name"/>
<bean:header id="options" name="option" multiple="true"/>
Copy a Web Application Resourcejava.io.InputStream
ServletContext.getResource()
ServletContext.getResourceAsStream()
<bean:resource id="deployment" name="/WEB-INF/web.xml"/>
<bean:resource id="stream" name="/WEB-INF/web.xml"
input="true"/>
Copy A Struts Configuration Object <bean:struts id="form" formBean="CustomerForm"/>
<bean:struts id="fwd" forward="success"/>
<bean:struts id="map" mapping="/saveCustomer"/>
Bean Output
Render An Internationalized Message
MessageResources
Bean Tags Reference <bean:message key="label.Cancel"/>
<bean:message key="message.hello" arg0='<%= user.getFullName() %>'/>
Render A Bean or Bean Property above <bean:write name="username"/>
<bean:write name="user" property="fullName"/>
<bean:write name="customer" property="orders[2].partNumber"
scope="session"/>