📄 indexedprops.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Indexed Properties, Mapped Properties, and Indexed Tags</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="David M. Karr" name="author" />
<meta content="David Graham" name="author" />
<link href="../struts.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="heading">
<a href="http://apache.org/">
<img id="asf_logo_wide" alt="The Apache Project" src="../images/asf_logo_wide.gif" />
</a>
<a href="http://struts.apache.org/">
<img id="struts-logo" alt="Struts Framework" src="../images/struts.gif" />
</a>
</div>
<!--end heading-->
<div id="content">
<div id="menu">
<p>FAQs</p>
<ul>
<li>
<a href="kickstart.html">Kickstart</a>
</li>
<li>
<a href="newbie.html">Newbie</a>
</li>
<li>
<a href="helping.html">How to Help</a>
</li>
</ul>
<p>Howto Guides</p>
<ul>
<li>
<a href="actionForm.html">Action Forms</a>
</li>
<li>
<a href="apps.html">Building Apps</a>
</li>
<li>
<a href="database.html">Database</a>
</li>
<li>
<a href="indexedprops.html">Indexed Properties</a>
</li>
<li>
<a href="ssl.html">SSL</a>
</li>
<li>
<a href="struts-el.html">Struts-EL (JSTL)</a>
</li>
</ul>
<p>IDE Guides</p>
<ul>
<li>
<a href="eclipse.html">Eclipse</a>
</li>
<li>
<a href="netbeans.html">Netbeans</a>
</li>
</ul>
<p>Quick Links</p>
<ul>
<li>
<a href="../index.html">Welcome</a>
</li>
<li>
<a href="../userGuide/index.html">User and Developer Guides</a>
</li>
</ul>
<div class="authors">
<p>
<strong>Contributors</strong>
</p>
<ul>
<li>David M. Karr</li>
<li>David Graham</li>
</ul>
</div>
</div>
<!--end menu-->
<div id="main">
<h1>Indexed Properties, Mapped Properties, and Indexed Tags</h1>
<h2 id="introduction">Introduction</h2>
<div class="indent">
<p>
The JSP specification discusses using "indexed properties" in reference to
the <jsp:setProperty> tag. However, none of the support provided in
the base JSP specification actually deals with the "indexing" part. In
truth, it allows for setting "array properties", but it doesn't do much
for setting or even getting the actual values in each entry of the array,
except with explicit JSP expressions (<%= %>).
</p>
<p>
The Struts framework provides much more powerful features related to
indexed properties, but in truth most of the heavy lifting is not even in
the Struts framework, but in the Jakarta Commons Beanutils package. This
package is used by Struts to provide this functionality. You can see the
javadoc documentation for the Beanutils package at <a href="http://jakarta.apache.org/commons/beanutils/api/index.html">http://jakarta.apache.org/commons/beanutils/api/index.html</a>.
The information particularly related to indexed properties is in the
package description for the org.apache.commons.beanutils package. This
article mirrors that information, but focuses on how this functionality is
mapped to JSP tags using the Struts tag library.
</p>
<p>
The support for indexed properties also includes "mapped properties",
"nested properties" and "indexed tags", which are all related but slightly
different. The latter is exclusive to Struts, but the first two are also
provided by the Beanutils package. This article will cover all three of
these topics.
</p>
</div>
<h2 id="indexedprops">Indexed Properties</h2>
<div class="indent">
<p>
The simplest demonstration of using indexed properties in Struts can be
shown with the following simple bean and JSP page:
</p>
<pre>
package org.apache.struts.webapp.exercise;
import org.apache.struts.action.ActionForm;
public class StringBean extends ActionForm {
private String strAry[] = { "String 0", "String 1", "String 2", "String 3", "String 4" };
public String getStringIndexed(int index) {
return strAry[index];
}
public void setStringIndexed(int index, String value) {
strAry[index] = value;
}
}</pre>
<p>
First note the two methods in the StringBean class, "getStringIndexed()"
and "setStringIndexed()". Note that the "get" method takes an "int" and
the "set" method takes an "int" and "String". The Beanutils package and
Struts recognizes this arrangement of signatures as an "indexed property",
in this case with the property name "stringIndexed".
</p>
<pre>
<!-- indexedtest.jsp -->
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<jsp:useBean id="bean" class="org.apache.struts.webapp.exercise.StringBean"/>
<bean:write name="bean" property="stringIndexed[1]"/></pre>
<p>
Note the property value of "stringIndexed[1]". This is intended to
reference the indexed property "stringIndexed", and the 1st (zero-based)
entry of whatever array or collection which the indexed property
represents.
</p>
<p>
As you might be able to guess, when this page is executed, it will print
just the string "String 1", which is the corresponding array entry at that
index value.
</p>
<p>
This is a simple demonstration of what indexed properties can provide.
</p>
</div>
<h2 id="listbackedprops">List-Backed Indexed Properties</h2>
<div class="indent">
<p>
A variation on indexed properties are properties whose type is
<code>java.util.List</code> or a subclass.
</p>
<p>
For instance, the first example using "StringBean.java" and
"indexedtest.jsp" could use a modified "StringBean.java" class, like this:
</p>
<pre>
package org.apache.struts.webapp.exercise;
import org.apache.struts.action.ActionForm;
public class StringBean2 extends ActionForm {
private String strAry[] = { "String 0", "String 1", "String 2", "String 3", "String 4" };
public java.util.List getStringIndexed(int index) {
return java.util.Arrays.asList(strAry);
}
}</pre>
<p>
Note the different implementation of the "getStringIndexed()" method,
returning a List instead of a String. If this bean class is substituted
with the original "indexedtest.jsp", the result will be identical.
</p>
</div>
<h2 id="mappedprops">Mapped Properties</h2>
<div class="indent">
<p>
The idea of "mapped properties" as opposed to "indexed properties" is that
the property represents a "map" type, as opposed to an array or collection
type. The signature of the "get" and "set" methods for a mapped property
are different from the same methods for an indexed property. In
particular, instead of an "int" for the index, there is a "String" for the
key.
</p>
<p>
The previous example for indexed properties can be changed to the
following to demonstrate mapped properties:
</p>
<pre>
package org.apache.struts.webapp.exercise;
import java.util.HashMap;
import org.apache.struts.action.ActionForm;
public class StringBean3 extends ActionForm {
private String strAry[] = { "String 0", "String 1", "String 2", "String 3", "String 4" };
private HashMap map = new HashMap();
public StringBean() {
map.put("zero", strAry[0]);
map.put("one", strAry[1]);
map.put("two", strAry[2]);
map.put("three", strAry[3]);
map.put("four", strAry[4]);
}
public Object getStringMapped(String key) {
return map.get(key);
}
public void setStringMapped(String key, Object value) {
map.put(key, value);
}
}</pre>
<p>
Note the "get" and "set" methods to represent the mapped property.
</p>
<pre>
<!-- indexedtest3.jsp -->
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<jsp:useBean id="bean" class="org.apache.struts.webapp.exercise.StringBean"/>
<bean:write name="bean" property="stringMapped(two)"/></pre>
<p>
Note the property value of "stringMapped(two)". This will reference the
mapped property "stringMapped", using the key value of "two".
</p>
<p>
When this page is executed, it will print just the string "String 2",
which is the string stored in the HashMap with the key "two".
</p>
</div>
<h2 id="nestedprops">Nested Properties</h2>
<div class="indent">
<p>
Nested properties allows you to combine normal properties, indexed
properties, and mapped properties in a hierarchical fashion. A property
value of a bean does not have to be a primitive like "int" or "String.
The property value can be a bean with its own properties. The following
example demonstrates this.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -