struts,動態顯示頁面的textbox

    當頁面上顯示的textbox 是不能確定個數的時候,要想顯示和更新數據就似乎有些不可能,但是通過HashMap,卻可以實現這一點
    在ActionForm中定義一個類型爲HashMap的類變量
 private HashMap investimatesubMap = new HashMap();

    
public Object getInvestimatesubMap(String key) ...{
        
return investimatesubMap.get(key);
    }

    
public void setInvestimatesubMap(String key, Object value) ...{
        
this.investimatesubMap.put(key, value);
    }


    
private Collection listInvestimatesub;
   
    
public void resetListInvestimatesub() {
        Collection myInvestimatesub 
= new ArrayList();
       
        Collection myKeys    
= investimatesubMap.keySet();
        
for (Iterator it = myKeys.iterator(); it.hasNext();) {
            String         key         
= (String) it.next();
            String         getKey        
= this.investimatesubMap.get(key).toString();
            String[]    properties    
= key.split("_");
            String         property    
= properties[0];
            String         vol            
= properties[1];
            String        id            
= properties[2];

                InvestimatesubForm investimatesubForm 
= new InvestimatesubForm();
                investimatesubForm.setEttprojid(
this.ettprojid);
                investimatesubForm.setVoltagegrade(vol);
                investimatesubForm.setInvestestimateid(id);

               
                investimatesubForm.setTranssubstninvest(getKey);
               
                myInvestimatesub.add(investimatesubForm);

        }

       
        
this.listInvestimatesub = myInvestimatesub;
    }



    
public Collection getListInvestimatesub() {
        
if (this.listInvestimatesub == null{
            resetListInvestimatesub();
        }

       
        
return listInvestimatesub;
    }


    
public void setListInvestimatesub(Collection listInvestimatesub) {
        
this.listInvestimatesub = listInvestimatesub;
    }

注意:變量一定要實例化,get方法的返回值一定是Object,set方法的value類型也一定是Object。

    在進入jsp之前的Action中
       InvestimatemainForm investimatemainForm = new InvestimatemainForm();
          String key =
"lineinvest_" + vol + "_" + id
       investimatemainForm.setInvestimatesubMap("key""testValue");

    jsp中
<html:text property="investimatesubMap(lineinvest_220_111)" maxlength="12"  style="width: 100px;"/>
map中的值一定不能加引號,錯誤示例:
<html:text property="investimatesubMap(‘lineinvest_220_111’)" maxlength="12"  style="width: 100px;"/>


可以用key值傳遞一些跟當前所填寫的字段相關的信息,再在後臺解析
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章