struts2中表現層的數據展現(bean,list,iterator,format,etc)

假定在Action中通過get/set了綁定了一個ModelAndView 的bean,用於渲染頁面的數據。在實際的開發中,這個bean可能包含了各種頁面需要的信息,因此會相當複雜。

設定:bean mv


public class CompanyMV {
	private Company company;
	private List<City> cities;
	private List<Currency> currencies;
	private List<Moneystyle> moneystyles;
	private List<Customer> customers;
       //.......

        private File logofile;
	private String logofileContentType;
	private String logofileFileName;
       //..get/set
	
}



1,遍歷(iterator)

1.1) 包含html

    	<select name="mv.company.cityid"> 
	    	<s:if test="mv.cities.size>0">
	    		<s:set name="cities" value="mv.cities" scope="session"/>
	    	</s:if>	
    		<s:iterator value="#session.cities" var="city">
    			<option value="${city.cityid}" 
    				<s:if test="#city.cityid == mv.company.cityid "> SELECTED </s:if>  >
    				<s:property value="name" />   <s:property value="state"/>
    			</option>
    		</s:iterator>	
    	</select> 

Note: 此處通過<s:set>設置cities的值到session, 爲了防止表單驗證之後的list值丟失。如果不通過<s:set>也可使用在form中隱含變量的方式,eg

<s:iterator value="mv.qualitys" status="stat" var="item">
            <input type="hidden" name="mv.qualitys[<s:property value="#stat.index" />].id.qualityindex" value="<s:property value="#item.id.qualityindex" />" />
            <input type="hidden" name="mv.qualitys[<s:property value="#stat.index" />].qualityvalue" value="<s:property value="#item.qualityvalue" />" />
        </s:iterator>

 此處的id是qualitys的複合primary key. (通過hibernate生成)


固定的list

    	<select id="mv.company.payterms" name="mv.company.payterms">
        <option value="On Due Date" <s:if test="\"On Due Date\" == mv.company.payterms"> SELECTED </s:if> ><s:text name="comp.locmain.compform.slct.ondate"/></option>
        <option value="Due on the 1st" <s:if test="\"Due on the 1st\" == mv.company.payterms"> SELECTED </s:if> ><s:text name="comp.locmain.compform.slct.onfir"/></option>
        <option value="Due on the 5th" <s:if test="\"Due on the 5th\" == mv.company.payterms"> SELECTED </s:if> ><s:text name="comp.locmain.compform.slct.onfif"/></option>
        <option value="Net 15"  <s:if test="\"Net 15\" == mv.company.payterms"> SELECTED </s:if> ><s:text name="comp.locmain.compform.slct.netfit"/></option>
        <option value="Net 30" <s:if test="\"Net 30\" == mv.company.payterms"> SELECTED </s:if> ><s:text name="comp.locmain.compform.slct.netthi"/></option>
      </select> 

      

1.2) 利用tag


<s:select 
	name="mv.locid" 
	list="mv.floors" 
	listKey="locid" 
	listValue="shortdescription" />	


1.3) 包含for循環的java代碼


<select name="curi_servicecutoff">
                <%
                    for (int current = 16; current < 29; current++) {
                        request.setAttribute("current", current);
                %>
                    <option value="${current}" <s:if test="#request.current == curi_servicecutoff"> SELECTED </s:if> > <s:property value="#request.current" /> </option>
                <%
                    }
                %>
</select>




2,格式化(format)


2.1) 數字



				<input type="text" size="20" maxlength="5"
					name="mv.qualitys[<s:property value="#stat.index" />].qualityvalue" 
					value="<s:text name="global.format.number.nopositionif"><s:param name="value" value="#item.qualityvalue" /></s:text>"	
					 />	

Note:global.format.number.nopositionif在多語言資源文件中定義,eg

global.format.number.noposition={0,number,##########}
global.format.number.nopositionif={0,number,##########.##}
global.format.number.2position={0,number,##########.00}
global.format.date.normal={0,date,yyyy-MM-dd}


3集合(List,Array,Set)的顯示


直接看代碼,仔細閱讀,不解析

<s:iterator status="stat" value="mv.suites" var="item">
 			<tr>
		<td>
		  <input type="text" size="7" maxlength="7" name="mv.suites[<s:property value="#stat.index" />].officeid" value="<s:property value="#item.officeid" />" >
		  <input type="hidden" name="mv.suites[<s:property value="#stat.index" />].suiteid" value="<s:property value="#item.suiteid" />" />
		</td>
		
		<td>
		  <input type="text" size="8" maxlength="10" style="text-align:right" name="mv.suites[<s:property value="#stat.index" />].size" value="<s:property value="#item.size" />" onkeyup="javascript:changeEstRent()">
		</td>
		
		<td>
		  <input type="text" size="9" maxlength="10" style="text-align:right" name="mv.suites[<s:property value="#stat.index" />].cost" value="<s:property value="#item.cost" />" readonly>
		</td>
		
		<td>
		  <input type="text" size="15" maxlength="20" name="mv.suites[<s:property value="#stat.index" />].shortdescription" value="<s:property value="#item.shortdescription" />">
		</td>
		
		<td>
		  <input type="text" size="20" maxlength="132" name="mv.suites[<s:property value="#stat.index" />].longdescription" value="<s:property value="#item.longdescription" />">
		</td>
		
		<td>
		  <input type="text" size="2" maxlength="2" name="mv.suites[<s:property value="#stat.index" />].workstation" value="<s:property value="#item.workstation" />">
		</td>
		
		<td>
		  <input type="text" size="4" maxlength="4" name="mv.suites[<s:property value="#stat.index" />].link" value="<s:property value="#item.link" />">
		</td>
		
		
		<td>
			<select name="mv.suites[<s:property value="#stat.index" />].rentable" onchange="javascript:changeEstRent()" 
			
			 >
				<option value="Y" <s:if test="\"Y\" == #item.rentable"> SELECTED </s:if> ><s:text name="setup.month"/></option>
				<option value="H" <s:if test="\"H\" == #item.rentable"> SELECTED </s:if> ><s:text name="setup.hour"/></option>
				<option value="N" <s:if test="\"N\" == #item.rentable"> SELECTED </s:if> ><s:text name="setup.na"/></option>
				<option value="R" <s:if test="\"R\" == #item.rentable"> SELECTED </s:if> ><s:text name="setup.registration"/></option>
			</select>
			<input type="hidden" name="mv.suites[<s:property value="#stat.index" />].crate" value="<s:property value="#item.crate" />" />
			<input type="hidden" name="mv.suites[<s:property value="#stat.index" />].ncrate" value="<s:property value="#item.ncrate" />" />
			<input type="hidden" name="mv.suites[<s:property value="#stat.index" />].arate" value="<s:property value="#item.arate" />" />
			<input type="hidden" name="mv.suites[<s:property value="#stat.index" />].roomtype" value="<s:property value="#item.roomtype" />" />
		</td>
		
		<td>
			<select name="mv.suites[<s:property value="#stat.index" />].quality" onchange="javascript:changeEstRent()">
				<option value="0">--</option>
				<s:iterator status="stat" value="mv.qualitys" var="qitem">
					<option value="<s:property value="#qitem.id.qualityindex" />" 
					<s:if test="#qitem.id.qualityindex == #item.quality "> SELECTED </s:if>  >
					<s:property value="id.qualityindex" />(<s:property value="qualityvalue" />)
					</option>
				</s:iterator>
			</select>	
		</td>
		
		<td>
		  <input type="text" size="12" style="text-align:right" name="mv.suites[<s:property value="#stat.index" />].targetrent" value="<s:property value="#item.targetrent" />" readonly>
		</td>
	</tr>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章