rss· 导航· 设为首页· 加入收藏
当前位置: 火魔网 » 程序开发 » JavaWeb开发

jsp中遍历集合类型等

1.JSP中日期数据的格式化:
 问题描述:Oracle数据中timestamp类型的数据读出来后,在jsp中默认显示为yy-mm-dd hh:mm:ss,但是想要的结果是yyyy-mm-dd格式


 问题解决:1)导入fmt标签库:<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
    2)使用:<input type="text" id="startDate" value="<fmt:formatDate value="${modifyList[0].startDate}"
pattern="yyyy-MM-dd"/>" onclick="WdatePicker()" onchange="getDteVal(endDate.value,this.value);">
    3)也可使用fn标签:fn:substring(modifyList[0].startDate,0,10);
 备注:fmt标签中的value值为数据库中对应的日期数据,用EL来读出即${}。
2.JSP中动态数据比较去除空格:
 问题描述:在修改页面,下拉框的默认选项处理过程中,特别是下拉框数据是动态加载的时候。
 问题解决:1)导入fn标签:<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
    2)使用:
   <select name="select2">
    <s:iterator value="customerType">
     <option  <c:if test="${fn:trim(code)==fn:trim(modifyList[0].autoTax.billTyepFlag)}">

selected="selected" </c:if>><%=Unicode2GB.UnicodeToGB((String) request.getAttribute("name"))%></option>
    </s:iterator>
   </select>
3.JSP中类似if....else功能
 问题描述:在页面中常常需要判断一个数据,根据数据的值来确定该显示哪一部分。
 问题解决:方式一:1)导入s标签:<%@ taglib prefix="s" uri="/struts-tags"%>
     2)使用:
    <s:if test=""></s:if>
    <s:else></s:else>
    方式二:1)导入c标签:<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>
     2)使用:
    <c:chose>
     <c:when test="">
     </c:when>
     <c:otherwise>
     </c:otherwise>
    </c:chose>
 方法比较:都可以,据说如果test里面嵌入的el过于复杂的时候,s标签会无能为力,再者c标签看着很安全,该框起来的都框起来了


4.JAVA中HashSet中的数据遍历:
 问题描述:保单对应有子险,关系是one to many,取出各个子险中的保费,放到map中。
 问题解决:
  //取得保单信息
  modifyList=this.queryManager.queryBdByInsureNo(this.bdno);
  //得到子险,为set
  subSets=modifyList.get(0).getSubinsureList();
  //调用set的iterator迭代器。
  Iterator <Subinsure> iter=subSets.iterator();
  while(iter.hasNext()){
   Subinsure item=iter.next();
   //迭代出每个子险的子险号码作为key,子险的保费作为value,放入HashMap中即priceMap
   priceMap.put(item.getLbtcode(), item.getLbtAmount());
  }
5.JSP中Set类型的数据的遍历问题:
 问题描述:如上的subSets,在页面上显示。
 问题解决:
 <s:iterator value="subInsureList" id="sl">
 <tr>
  <td width="3%">
   <input type="checkbox" <s:iterator value="subSets" id="subs"><c:if test="${fn:trim(subs.lbtcode)

==fn:trim(sl.lbtcode)}">checked="checked"</c:if></s:iterator> />
  </td>
  <td width="19%"><%=Unicode2GB.UnicodeToGB((String) request.getAttribute("lbtchnname"))%>
 </tr>
 </s:iterator>
 备注:以上是较复杂情况下的应用,如果只是显示,就没那么复杂。
5.Jsp中数组的遍历:
 <input <c:forEach items="${tbydstr}" var="st"> <c:if test="${fn:trim(st)==fn:trim(rule.SplCode)}"> checked="checked"

</c:if> </c:forEach> type="checkbox" name="checkitems" value="${ruleList[n.index]['SplCode']}" />
 估计用用s标签的iterator与c:if嵌套也能满足要求。

顶一下
(1)
踩一下
(0)