⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indexedprops.html

📁 struts api,学习使用struts必备的文档
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    </p>
    <pre>
package org.apache.struts.webapp.exercise;
import org.apache.struts.util.LabelValueBean;
import org.apache.struts.action.ActionForm;
public class StringBean4 extends ActionForm {
    private LabelValueBean[] lvbeans;
    
    public StringBean() {
        lvbeans   = new LabelValueBean[5];
        lvbeans[0]   = new LabelValueBean("Zero", 0+"");
        lvbeans[1]   = new LabelValueBean("One", 1+"");
        lvbeans[2]   = new LabelValueBean("Two", 2+"");
        lvbeans[3]   = new LabelValueBean("Three", 3+"");
        lvbeans[4]   = new LabelValueBean("Four", 4+"");
    }

    public  LabelValueBean getLabelValue(int index) { 
        return lvbeans[index];
    }
}</pre>
    <p>
     First note the use of the class "LabelValueBean".  This is a simple class
     provided in the Struts library which represents a pair of two Strings, a
     "label" and a "value".  It itself is a bean, providing these two
     properties with standard getters and setter methods.
    </p>
    <p>
     Then, see the "getLabelValue()" method, representing the indexed property
     "labelValue".  This class doesn't show a "setter" method. If you only ever
     provide read-only access a property, then it is not necessary to provide a
     setter method.
    </p>
    <pre>
&lt;!-- indexedtest4.jsp --&gt;
&lt;%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %&gt;
&lt;jsp:useBean id="bean" class="org.apache.struts.webapp.exercise.StringBean"/&gt;
&lt;bean:write name="bean" property="labelValue[1].label"/&gt;</pre>
    <p>
     Note here the "nested" property reference. It is first using the indexed
     property "labelValue" and then the "normal" property of the LabelValueBean
     to get the final result.  When this page is executed, it will print the
     string "One", representing the "label" property of the 1st entry of the
     array represented by the "labelValue" indexed property.
    </p>
   </div>
<h2 id="dynamicindexes">Dynamic Indexes for Indexed Properties</h2>
<div class="indent">
    <p>
     When people started using indexed properties in Struts tags, I'm
     reasonably certain they started out with a high level of enthusiasm, but
     were somewhat frustrated when they discovered reality.  The reality is
     that the "index" for indexed properties often needs to be a dynamic value,
     usually from the "indexId" counter in the "&lt;logic:iterate&gt;" tag.
    </p>
    <p>
     For instance, the following example JSP page using the same "StringBean"
     bean class uses a dynamic value for the index:
    </p>
    <pre>
&lt;!-- indexedtest5.jsp --&gt;
&lt;%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %&gt;
&lt;%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %&gt;
&lt;html&gt;
 &lt;body&gt;
  &lt;html:form action="indexedtest5.do"&gt;
   &lt;logic:iterate name="stringbean" property="stringArray" id="foo"
                  indexId="ctr"&gt;
    &lt;html:text name="stringbean"
               property='&lt;%= "labelValue[" + ctr + "].label" %&gt;' /&gt;
   &lt;/logic:iterate&gt;
   &lt;html:submit property="submitValue"&gt;Submit Changes&lt;/html:submit&gt;
  &lt;/html:form&gt;
 &lt;/body&gt;
&lt;/html&gt;</pre>
    <p>
     The JSP expression syntax for the <code>property</code> attribute is somewhat 
     messy and easy to get wrong so it's something we want to avoid. One
     way to make this a little cleaner is to use "indexed tags", but there's a
     wrinkle to this approach.  We'll first cover the details of indexed tags,
     then we'll talk about the wrinkle, and then finally an alternative to
     indexed tags.
    </p>
   </div>
<h2 id="indexedtags">Indexed Tags</h2>
<div class="indent">
    <p>
     The "indexed tags" feature is provided by several tags that have an
     optional boolean "indexed" attribute.  This is only legal when inside a
     "&lt;logic:iterate&gt;" tag.  When the "indexed" attribute is true, then
     the tag will incorporate the loop index into the resulting HTML component.
    </p>
    <p>
     The several tags that support the "indexed" attribute can be broken into
     three groups, split by what they do to incorporate the loop index into the
     resulting HTML component.
    </p>
    <table>
     <thead>
     <tr>
      <th>Group 1</th>
<th>Group 2</th>
<th>Group 3</th>
     </tr>
     </thead>
     <tr>
<td>checkbox</td>
<td>button</td>
<td>link</td>
</tr>
     <tr>
<td>file</td>
<td>image</td>
<td>聽</td>
</tr>
     <tr>
<td>hidden</td>
<td>submit</td>
<td>聽</td>
</tr>
     <tr>
<td>password</td>
<td>聽</td>
<td>聽</td>
</tr>
     <tr>
<td>radio</td>
<td>聽</td>
<td>聽</td>
</tr>
     <tr>
<td>select</td>
<td>聽</td>
<td>聽</td>
</tr>
     <tr>
<td>text</td>
<td>聽</td>
<td>聽</td>
</tr>
     <tr>
<td>textarea</td>
<td>聽</td>
<td>聽</td>
</tr>
    </table>
    <p>
     In Group 1, all of these tags will generate an HTML "name" attribute of
     "name[nn].property".  The value of each tag will also be initialized by
     the getter method corresponding to that property specification.
    </p>
    <p>
     In Group 2, these tags will generate an HTML "name" attribute of
     "property[nn]".  These three tags don't have "name" attributes, so since
     it wouldn't make sense to use "[nn].property" (no name value), the array
     indexes are attached to the property instead.
    </p>
    <p>
     The "link" tag in Group 3 isn't anything like any of the others.  The base
     description of the "link" tag doesn't even mention how this works, but the
     description of the "indexed" tag describes how it works somewhat (although
     it doesn't specifically say that it uses the name "index" if "indexId"
     isn't set).  In short, the "indexed" behavior of this tag is to add a URL
     query parameter, where the parameter name is "index" or the value of the
     "indexId" attribute, and the parameter value is the current index value.
     Outside of this, the "indexed" behavior of the "link" tag needs no more
     explanation, in contrast to the tags in the first two groups.
    </p>
   </div>
<h2 id="wrinkle">The Wrinkle with Indexed Tags</h2>
<div class="indent">
    <p>
     The problem with using the "indexed" attribute to automatically attach the
     loop index is that it gives you less control over how the loop index is
     used.  For instance, in our earlier examples using the "&lt;html:text&gt;"
     tag, we attached the loop index to the end of the "property" attribute
     value, which was "labelValue" in our example.  This results in a "name"
     attribute value in the HTML component of "labelValue[nn]".  This maps to a
     "labelValue" indexed property on the "stringbean" bean instance.
    </p>
    <p>
     However, if we instead add the "indexed" attribute, and remove the manual
     attachment of the loop index, then the resulting "name" attribute in the
     HTML component ends up as "stringbean[nn].labelValue".  This will not work
     as is.  It can be changed to work, however.  From the previous example
     (indexedtest5.jsp), the "&lt;html:text&gt;" component would change the
     "name" attribute from "stringbean" to "labelValue", and the "property"
     attribute to "label".  With the "indexed" attribute, that ends up with a
     "name" attribute value of "labelValue[nn].label".
    </p>
    <p>
     So, it's very likely you could use indexed tags to support your needs for
     indexed properties, but you have to understand the resulting structure of
     your "name" attribute in order to understand what other attribute values
     you need.
    </p>
   </div>
<h2 id="strutsel">Using Struts-EL To Avoid Some Of The Pain</h2>
<div class="indent">
    <p>
     The "indexed tags" feature was created partially because of the awkward
     process for encoding the loop index into the property attribute, using a
     JSP expression.  The creation of the <a href="http://java.sun.com/products/jsp/jstl/">JSTL</a> 
     eventually resulted in a solution that makes that process less painful.
    </p>
    <p>
     The JSTL uses a string-based expression language to evaluate attribute
     values, instead of using a run-time JSP expression.  The engine that
     performs these evaluations is often called just "EL" (expression language)
     for short.  After the JSTL was created, a derivative of the Struts tag
     library called Struts-EL was created.  In short, this does everything that
     the Struts tag library does, but it uses the JSTL EL engine to evaluate
     attribute values.
    </p>
    <p>
     If you use Struts-EL, you can get back some of the flexibility of encoding
     your loop indices manually, but the resulting expressions will be a little
     more readable, and thus maybe a little easier to maintain.
    </p>
    <p>
     For instance, following this is a version of "indexedtest5.jsp" using
     Struts-EL:
    </p>
    <pre>
&lt;!-- indexedtest6.jsp --&gt;
&lt;%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %&gt;
&lt;%@ taglib uri="/WEB-INF/struts-logic-el.tld" prefix="logic-el" %&gt;
&lt;html&gt;
 &lt;body&gt;
  &lt;html-el:form action="indexedtest6.do"&gt;
   &lt;logic-el:iterate name="stringbean" property="stringArray" id="foo"
                     indexId="ctr"&gt;
    &lt;html-el:text name="stringbean"
                  property="labelValue[${ctr}].label" /&gt;
   &lt;/logic-el:iterate&gt;
   &lt;html-el:submit property="submitValue"&gt;Submit Changes&lt;/html:submit&gt;
  &lt;/html-el:form&gt;
 &lt;/body&gt;
&lt;/html&gt;</pre>
    <p>
     The Struts-EL library is part of the Struts distribution, in the "contrib"
     directory.  The one drawback to using Struts-EL is that it requires a web
     container supporting the Servlet 2.3 specification.
    </p>
   </div>
 </div>
<!--end main-->
</div>
<!--end content-->
<div id="footer">
<img id="powered-logo" alt="Powered by Struts" src="../images/struts-power.gif" />
        Copyright (c) 2000-2005, The Apache Software Foundation <span class="noprint">- 
        <a href="http://wiki.apache.org/struts/StrutsDocComments">Comments?</a>
</span>
</div>
<!--end footer-->
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -