最近在寫一個(gè)系統(tǒng)中,有一個(gè)有下拉列表的修改資料功能,對(duì)Struts的html:select進(jìn)行了運(yùn)用。
其中怎么樣使進(jìn)入修改頁面之后,該下拉列表里的數(shù)據(jù)是該需要修改的數(shù)據(jù)列的數(shù)據(jù)字段,也就是使html:select中顯示的值默認(rèn)對(duì)應(yīng)數(shù)據(jù)庫中的值。測(cè)試修改了一個(gè)下午,總得不出結(jié)果,最后還是看了Struts的examples才算是弄出來了,如:

<
html:select property="id">  //這里一定不能再用vlue屬性了,只用一個(gè)property就可以了   
    <c:forEach var="row" item="${rs.rows}">       
          <html:option value="row.id">   
              <c:out value="row.name"/>    
         </html:option>       
   </c:forEach>         
 </html:select>         

總結(jié):在使用Struts進(jìn)行修改功能時(shí),要在修改頁面上取的數(shù)據(jù)庫先前的值。只要在form中reset里取得數(shù)據(jù)庫里的值,然后在前臺(tái)頁面里html:form里對(duì)應(yīng)的項(xiàng)設(shè)置其property為form中對(duì)應(yīng)的屬性值就好了,而不需要再設(shè)置value屬性了。

XXAction{   

execute()   {   

 request.setAttribute("ids", getNameID());   

}   

private List getNameID()   {   

 //load from db    ...   

 //make a LabelValueBean List   

 ArrayList ls = new ArrayList();   

 for()    {   

   ls.add(new LabelValueBean(name,id);   

 }              

 }         

jsp

<html:select property="id">   

    <html:optionsCollection name="ids" />   

</html:select>  

如果Select的選項(xiàng)固定,可以直接用Selected標(biāo)簽實(shí)現(xiàn)默認(rèn)值(JAVA代碼1)

<html:select property="id">        
    <html:option value="0" selecded>html:option>         
       <html:optionsCollection name="ids" />         
</html:select>    

如果是動(dòng)態(tài)的(JAVA代碼2),可以在相對(duì)應(yīng)Action的From中設(shè)置默認(rèn)值(JAVA代碼3)
public List getOptions() {    
    List ls = new ArrayList();      
        for(;;)  {       
             ls.add(new LabelValueBean(key,value);       
        }                      
}

public ActionForward update(ActionMapping mapping, ActionForm form,    
          HttpServletRequest request, HttpServletResponse response) {    
    //do some thing         
    xxxForm f = (xxxForm)form;    
    f.setOption(value);   //這里設(shè)置下拉選項(xiàng)的默認(rèn)值    
    return mapping.findForward("page");    


如果上面的還不能滿足的話,可以試試用JS控制
function Selected(index,valued){    
    if(index!=""){    
        var lengths = $("select").options.length;//下拉項(xiàng)的長度    
        for(var i=0;i 
            if(valued == index){    
                $("select").options[i].selected=true;    
            }    
       }    
    }   
}  

<html:select property="id">  //這里一定不能再用vlue屬性了,只用一個(gè)property就可以了        
   <c:forEach var="row" item="${rs.rows}">           
          <html:option value="row.id">       
             <c:out value="row.name"/>       
          </html:option>           
        </c:forEach>             
</html:select>

<c:forEach var="row" item="${rs.rows}"> ……c:forEach>
<logic:equal name="by" value="desc" scope="session">