問題

你想要在表單中創(chuàng)建對應于一個Bean中的索引屬性的一套輸入字段。

動作要領

在Struts html標簽庫的標簽中使用indexed屬性來產生屬性值:

 

<html:form action="TestOneAction"><p>
  
<logic:iterate name="MyForm" property="stringArray" 
                   id
="stringValue" indexId="ctr">

    
<br/>
    
<html:text property="stringArray" indexed="true"/>
</logic:iterate>
</html:form>

 

動作變化

如第3.3時所示,訪問索引值以供顯示是很容易的。但是,在表單中使用索引屬性則有些麻煩。如果所產生的輸入字段的名稱沒有正確的格式化,在HTML表單被提交后, Apache Struts Web Framework 便不能正確的裝配ActionForm。Struts 要使用Jakarta Commons BeanUtils 包來從HTTP請求中的值組裝ActionForm。特別地,BeanUtils.populate( )方法是從表單提交時從HTTP請求數(shù)據(jù)發(fā)送的數(shù)據(jù)中裝載ActionForm的。

對于索引值,BeanUtils.populate( )使用請求參數(shù)來決定適當?shù)膕etter 方法以調用ActionForm。表3-3 列出了不同的表單輸入字段是如何處理的。表中列出了HTML 標簽,對應的HTTP 請求響應對,以及在請求被處理時在ActionForm之上調用的方法。

表3-3. ActionForm 組裝示例

HTML form 輸入標記

產生的請求對

導致的方法調用

<input type="text" name="bar">

bar=someValue

Form.setBar("someValue")

<input type="text" name="sna.fug">

sna.fug=blah

Form.getSna( ).setFug("blah");

<input type="text" name="baz[0]">

baz[0]=someValue

Form.setBaz(0,"firstVal");

<input type="text" name="glub[1].waf">

glub[1].waf=halb

Form.getGlub(1).setWaf("halb");

<input type="text" name="dog.leg[2]">

dog.leg[2]=lame

Form.getDog( ).setLeg(2, "lame");

 

考慮一個允許用戶輸入最喜愛列表,比如顏色和網站,的表單。持有這些數(shù)據(jù)的ActionForm包含一個表示用戶名稱的String屬性,表示用戶喜好顏色的String數(shù)組,以及一個表示用戶喜好網站的WebLink對象的List。WebLink類如Example 3-5所示,它定義了一個簡單的JavaBean,具有表示站點名稱和URL的屬性。

Example 3-5. WebLink JavaBean

 

package com.oreilly.strutsckbk;

public class WebLink 
{
    
public String getName( ) 
{
        
return
 name;
    }

    
public void setName(String name) {
        
this.name =
 name;
    }

    
public String getUrl( ) {
        
return
 url;
    }

    
public void setUrl(String url) {
        
this.url =
 url;
    }

    
private String url;
    
private
 String name;
}

 

form bean, FavoritesForm, 則包含了用戶名稱,喜愛顏色,以及喜好網站。如Example 3-6所示:

Example 3-6. FavoritesForm form bean

 

package com.oreilly.strutsckbk;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts.action.ActionForm;

public final class FavoritesForm extends ActionForm  
{
    
public FavoritesForm( ) 
{
        webLinks 
= new
 ArrayList( );
        
for (int i=0; i<5; i++) webLinks.add(new
 WebLink( )); 
        colors 
= new String[3
];
    }
    
    
public String getName( ) 
{
        
return
 name;
    }

    
public void setName(String name) {
        
this.name =
 name;
    }

    
public String getColor(int index) {
        
return
 colors[index];
    }

    
public void setColor(int index, String color) {
        colors[index] 
=
 color;
    }

    
public String[] getColor( ) {
        
return
 colors;
    }

    
public List getWebLinks( ) {
        
return
 webLinks;
    }

    
public WebLink getWebLink(int index) {
        
return (WebLink)webLinks.get
(index);
    }

    
public void setWebLink(int index, WebLink webLink) {
        webLinks.
set
(index, webLink);
    }

    
    
public void reset( ) {
        webLinks.clear( );
        colors 
= new String[3
];
    }

    
    
private List webLinks;
    
private
 String name;        
    
private
 String[] colors;    
}

 

現(xiàn)在你可以創(chuàng)建一個JSP 頁面 (favorites.jsp) ,通過它用戶可以輸入表單中的相應數(shù)據(jù),其代碼如Example 3-7所示.

Example 3-7. FavoritesForm JSP

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<html:html locale="true">
<head>
<title><bean:message key="index.title"/></title>
<html:base/>
</head>
<body bgcolor="white">
<h2>Favorites Poll</h2>
<html:form action="/admin/ViewFavorites">
    
<p>
        What is your name:
        
<br/><html:text property="name"/>
    
</p>
    
<p>
        What are your three favorite colors:
        
<br/><html:text property="color[0]"/>
        
<br/><html:text property="color[1]"/>
        
<br/><html:text property="color[2]"/>
    
</p>
   
<p>
      What are your favorite links?
      
<table>
         
<tr>
            
<th>Name</th>
            
<th>URL</th>
         
</tr>
         
<tr>
            
<td><html:text property="webLink[0].name"/></td>
            
<td><html:text property="webLink[0].url"/></td>
         
</tr>
      
</table>
   
</p>
    
<html:submit/>
    
<html:reset/>
</html:form>
</body>
</html:html>

 

因為Example 3-7 中的索引值是硬編碼,不是動態(tài)的,html:text標簽的property值便很容易構造,所以產生的HTML 標簽便具有適當?shù)膎ame屬性值。然而,假設你想要使用logic:iterate標簽來產生重復的輸入字段。比如,為了對color屬性這樣做,你可能想要嘗試以下的JSP 代碼:

 

What are your three favorite colors:
  
<logic:iterate name="FavoritesForm" id="theColor">

    
<br/><html:text property="color" indexed="true"/>
  
</logic:iterate>

 

但是這段代碼不會產生需要的HTML 標記。indexed屬性對為特定的Struts html標簽(這里是html:text)的name屬性所指定的值應用一個索引(即, [n]) 。如果你使用上面的片斷部署了一個JSP ,所產生的HTML可能是下面的樣子:

 

What are your three favorite colors:
<br/><input type="text" name
="org.apache.struts.taglib.html.
BEAN[0].color"
 value="[Ljava.lang.String;@5f1ba8">

<br/><input type="text" name="org.apache.struts.taglib.html.
BEAN[1].color"
 value="[Ljava.lang.String;@5f1ba8">

<br/><input type="text" name="org.apache.struts.taglib.html.
BEAN[2].color"
 value="[Ljava.lang.String;@5f1ba8">

 

索引并沒有應用到屬性的值。相反,卻被應用到了Form Bean的內部internal Apache Struts Web Framework 名稱。另外,value包含了對數(shù)組調用toString( )方法的結果,而不是數(shù)組中特定的元素。.

說到底,意思就是當你需要設置一個本身是復雜對象,比如一個JavaBean 的索引屬性的嵌套的簡單屬性時,indexed屬性時很有用的。你可以在logic:iterate標簽中為一個非嵌套屬性產生輸入字段,但是你卻必須借助scriptlet 來產生數(shù)組索引:

 

What are your three favorite colors:
  
<logic:iterate name="FavoritesForm" id="theColor" indexId="ctr">

    
<br/><html:text property='<%="color["+ctr+"]"%>'/>
  
</logic:iterate>

 

假設你想要使用logic:iterate標簽來為喜好鏈接(WebLink對象)產生輸入字段。這種情況下,indexed的行為則正如你愿:

 

What are your favorite links?
<table>

  
<tr>
     
<th>Name</th>
     
<th>URL</th>
  
</tr>
  
<logic:iterate id="webLink" name="FavoritesForm" property="webLinks">
    
<tr>
      
<td><html:text name="webLink" property="name" indexed="true"/></td>
      
<td><html:text name="webLink" property="url" indexed="true"/></td>
    
</tr>
  
</logic:iterate>
<table>

 

在html標簽中使用indexed屬性多少有點使人混淆。混淆之處主要來自于name屬性的不同意義。大多數(shù)情況下,在使用html標簽時,name都可以忽略因為值將基于action mapping 中聲明的form-bean。然而,在使用indexed屬性時,name卻引用到對應的ActionForm的嵌套的索引屬性。

我們回去看color屬性的問題,你可以選擇使用scriptlet之外的方式。可以使用Struts html-el標簽或者JSTL。它們都可以做一些和scriptlet相同的基本事情,但是它們是通過EL 表達式來進行的。最簡潔的方法是使用html-el標簽:

 

What are your three favorite colors:
  
<logic:iterate name="FavoritesForm" id="theColor" indexId="ctr">

    
<br/><html-el:text property='color[${ctr}]>'/>
  
</logic:iterate>

 

如你喜歡JSTL,你也可以直接產生需要的輸入標簽而不是使用Struts html標簽:

 

What are your three favorite colors:
<logic:iterate id="color" name="FavoritesForm" property="color" indexId="ctr">
 
  
<br/><input type="text" name="color[<c:out value='${ctr}'/>]"

             
value
="<c:out value='${FavoritesForm.color[ctr]}'/>"/>

</logic:iterate>

 

這個JSTL 版本幾乎和原來使用scriptlet的版本一樣丑陋。另外,因為Struts html:text和html-el:text標簽都沒使用, HTML input標簽的value屬性必須是顯式編碼的。如果你使用Struts html:text標簽,這個值就是自動設置的。