EL JSTL <c:forEach>遍历时同时获取对象和序号,<c:choose>多条件判断,el标签条件
2017-03-27 16:16
465 查看
今天在做jsp页面开发时,使用c标签遇到了些问题:(都是基础加深下印象)
1,分行遍历list的同时既要获得序号,也要获得list中的对象。
2,多条件判断时,写<c:when></when><c:otherwise></c:otherwise>忘了写<c:choose></c:chooose>。
3,el表达式条件逻辑运算,比较字母字符串相等,区分大小写的问题。
参考资料:
jstl el字符串比较
fn:contains 判断字符串是否包含另外一个字符串 <c:if test="${fn:contains(name, searchString)}">
fn:containsIgnoreCase 判断字符串是否包含另外一个字符串(大小写无关) <c:if test="${fn:containsIgnoreCase(name, searchString)}">
fn:endsWith 判断字符串是否以另外字符串结束 <c:if test="${fn:endsWith(filename, ".txt")}">
fn:escapeXml 把一些字符转成XML表示,例如 <字符应该转为< ${fn:escapeXml(param:info)}
fn:indexOf 子字符串在母字符串中出现的位置 ${fn:indexOf(name, "-")}
fn:join 将数组中的数据联合成一个新字符串,并使用指定字符格开 ${fn:join(array, ";")}
fn:length 获取字符串的长度,或者数组的大小 ${fn:length(shoppingCart.products)}
fn:replace 替换字符串中指定的字符 ${fn:replace(text, "-", "?")}
fn:split 把字符串按照指定字符切分 ${fn:split(customerNames, ";")}
fn:startsWith 判断字符串是否以某个子串开始 <c:if test="${fn:startsWith(product.id, "100-")}">
fn:substring 获取子串 ${fn:substring(zip, 6, -1)}
fn:substringAfter 获取从某个字符所在位置开始的子串 ${fn:substringAfter(zip, "-")}
fn:substringBefore 获取从开始到某个字符所在位置的子串 ${fn:substringBefore(zip, "-")}
fn:toLowerCase 转为小写 ${fn.toLowerCase(product.name)}
fn:toUpperCase 转为大写字符 ${fn.UpperCase(product.name)}
fn:trim 去除字符串前后的空格 ${fn.trim(name)}
<c:choose>
<c:when></c:when>
<c:otherwise></c:otherwise>
</c:choose>
下面是代码:
<c:forEach var="x" begin="1" end="${eRecord.row }">
<tr>
<c:forEach var="eResult" items="${eResults }" begin="${(x-1)*10 }" end="${x*10-1<eResults.size()?x*10-1:eResults.size() }"
varStatus="status">
<c:choose>
<c:when test="${empty eResult.userAnswer }">
<td class="_no">${status.index+1 }</td>
</c:when>
<c:when test="${fn:toUpperCase(eResult.standard_answer) ne fn:toUpperCase(eResult.userAnswer) }">
<td class="_wrong">${status.index+1 }</td>
</c:when>
<c:otherwise>
<td>${status.index+1 }</td>
</c:otherwise>
</c:choose>
</c:forEach>
</tr>
</c:forEach>
下面是成功图片:
1,分行遍历list的同时既要获得序号,也要获得list中的对象。
2,多条件判断时,写<c:when></when><c:otherwise></c:otherwise>忘了写<c:choose></c:chooose>。
3,el表达式条件逻辑运算,比较字母字符串相等,区分大小写的问题。
参考资料:
jstl el字符串比较
fn:contains 判断字符串是否包含另外一个字符串 <c:if test="${fn:contains(name, searchString)}">
fn:containsIgnoreCase 判断字符串是否包含另外一个字符串(大小写无关) <c:if test="${fn:containsIgnoreCase(name, searchString)}">
fn:endsWith 判断字符串是否以另外字符串结束 <c:if test="${fn:endsWith(filename, ".txt")}">
fn:escapeXml 把一些字符转成XML表示,例如 <字符应该转为< ${fn:escapeXml(param:info)}
fn:indexOf 子字符串在母字符串中出现的位置 ${fn:indexOf(name, "-")}
fn:join 将数组中的数据联合成一个新字符串,并使用指定字符格开 ${fn:join(array, ";")}
fn:length 获取字符串的长度,或者数组的大小 ${fn:length(shoppingCart.products)}
fn:replace 替换字符串中指定的字符 ${fn:replace(text, "-", "?")}
fn:split 把字符串按照指定字符切分 ${fn:split(customerNames, ";")}
fn:startsWith 判断字符串是否以某个子串开始 <c:if test="${fn:startsWith(product.id, "100-")}">
fn:substring 获取子串 ${fn:substring(zip, 6, -1)}
fn:substringAfter 获取从某个字符所在位置开始的子串 ${fn:substringAfter(zip, "-")}
fn:substringBefore 获取从开始到某个字符所在位置的子串 ${fn:substringBefore(zip, "-")}
fn:toLowerCase 转为小写 ${fn.toLowerCase(product.name)}
fn:toUpperCase 转为大写字符 ${fn.UpperCase(product.name)}
fn:trim 去除字符串前后的空格 ${fn.trim(name)}
<c:choose>
<c:when></c:when>
<c:otherwise></c:otherwise>
</c:choose>
下面是代码:
<c:forEach var="x" begin="1" end="${eRecord.row }">
<tr>
<c:forEach var="eResult" items="${eResults }" begin="${(x-1)*10 }" end="${x*10-1<eResults.size()?x*10-1:eResults.size() }"
varStatus="status">
<c:choose>
<c:when test="${empty eResult.userAnswer }">
<td class="_no">${status.index+1 }</td>
</c:when>
<c:when test="${fn:toUpperCase(eResult.standard_answer) ne fn:toUpperCase(eResult.userAnswer) }">
<td class="_wrong">${status.index+1 }</td>
</c:when>
<c:otherwise>
<td>${status.index+1 }</td>
</c:otherwise>
</c:choose>
</c:forEach>
</tr>
</c:forEach>
下面是成功图片:
相关文章推荐
- JSTL标签用法:<c:choose><c:forEach><c:if>
- 【JSP JSTL】<c:if>多个判断条件 + <c:foreach>满足条件跳出循环
- jstl <c:foreach>标签进行数据遍历
- 如何在JSP页面中使用JSTL标签实现if和if-else判断,判断条件使用<%=value%>
- JSTL Core标签库 c:if标签 条件标签 <c:forEach>:<c:forTokens>:迭代标签
- [JSP]EL表达式和JSTL标签遍历取值对象和数组与原来<%%>的对比
- 【转】JSTL标签用法:<c:choose><c:forEach><c:if><c:when><c:set>
- JSTL标签用法:<c:choose><c:forEach><c:if><c:when><c:set>
- JSTL标签用法:<c:choose><c:forEach><c:if><c:when><c:set>
- <c:forEach>遍历标签自动生成序号…
- JSTL标签用法:<c:choose><c:forEach><c:if><c:when><c:set>
- 在<c:foreach />标签之外访问遍历的单个对象--作用域扩展
- JSTL标签参考手册,JSTL标签用法:<c:choose><c:forEach><c:if><c:when><c:set>
- JSTL标签用法:<c:choose><c:forEach><c:if><c:when><c:set>
- jstl标签<c:forEach>如何获取循环子级元素值
- JSTL标签用法:<c:choose><c:forEach><c:if><c:when><c:set>
- JSTL标签用法:<c:choose><c:forEach><c:if><...
- <s:iterator>使用status获取当前遍历数据的行数、序号等操作
- JSTL <c:forEach>标签的使用
- 通过Struts标签和JSTL标签来遍历List<Map<String,String>>格式的数据