在項目中,原本一個下拉框 代碼如下:
<option value="" selected="selected"></option>
<html:optionsCollection name="orgClassList" value="itemId" label="itemName"/>
</html:select>
以上代碼等同於
<logic:present name="orgClassList">
<option value="" selected="selected" >默認值</option>
<logic:iterate id="orgClassList" name="orgClassList">
<option value="<bean:write name="orgClassList" property="itemId"/>">
<bean:write name="orgClassList" property="itemName"/>
</option>
</logic:iterate>
</logic:present>
</html:select>
,現在要在實現在打開頁面的時候默認值不爲空而爲指定的List中的一個值,則需要通過bean:define,在<%%>中得到List中的值進行判斷 然後在option中加上selected
屬性,代碼如下(假設默認值爲‘省公司’)
<!--
<html:optionsCollection name="orgClassList" value="itemId" label="itemName"/>
<option value="" ></option>
-->
<logic:present name="orgClassList">
<option value="" ></option>
<logic:iterate id="orgClassList" name="orgClassList">
<bean:define id="orgClassName" name="orgClassList" property="itemId">
</bean:define>
<%
if(orgClassName.equals("省公司")){
%>
<option selected="selected" value="<bean:write name="orgClassList" property="itemId"/>">
<bean:write name="orgClassList" property="itemName"/>
</option>
<%}else{ %>
<option value="<bean:write name="orgClassList" property="itemId"/>">
<bean:write name="orgClassList" property="itemName"/>
</option>
<%} %>
</logic:iterate>
</logic:present>
</html:select>