当前位置:编程学习 > JAVA >>

struts2中表现层的数据展现(bean,list,iterator,format,etc)

假定在Action中通过get/set了绑定了一个ModelAndView 的bean,用于渲染页面的数据。在实际的开发中,这个bean可能包含了各种页面需要的信息,因此会相当复杂。

设定:bean mv

[java] 
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

[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
[html] 
<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

[html]
<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


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

1.3) 包含for循环的java代码


[html] 
<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) 数字


[html] view plaincopy
<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
[html] view plaincopy
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)的显示

直接看代码,仔细阅读,不解析

[html] 
<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.off

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,