JSTL标签和EL表达式使用笔记

STL标签和EL表达式使用笔记关键字: jstl, c, el

    今天比较闲,平时对JSTL标签的总结的比较少,就成现在这个时间做简单的开头吧!以后慢慢加上去。平时jsp用的最多的就是jstl+EL这两个东西

   一般在jsp页面的头部都会应用一些标签库,一般就用下面这几个:

   <%@ page contentType="text/html; charset=UTF-8" language="java"
    pageEncoding="UTF-8"%>
   <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
   <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
   <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

   为了避免每个页面都写一次,可以把这些写在一个jsp文件里面,然后每个要用到这些标签的页面应用下就可以了。

<%@include file="../common/head.jsp"%>

下面贴几个使用的方法和场景。

   A:forEach遍历后台传过来的数据。一般是个list.

            <c:forEach var="result" items="${requestScope.page.items}" varStatus="varStatus">
                   
                        <tr class="${class}">
                            <td><input type="checkbox" name="rowid" value="${result.rowId} "/></td>
                            <td>${result.ipAddress}</td>
                            <td>${result.subUser.userType}</td>
                            <td>${result.enabled}</td>
                            <td>${result.sensitive}</td>
                          </tr>
            </c:forEach>

B:C:choose标签的使用,用语有选择情况下:

           <td>
                                <c:choose>
                                    <c:when
test="${fn:length (result.failAuditLevel.auditDesc) > 10}">

                                        <a style="TEXT-DECORATION:none"               title="${result.failAuditLevel.auditDesc}">${fn:substring (result.failAuditLevel.auditDesc,"0","3")}...</a>
                                    </c:when>
                                    <c:otherwise>
                                        ${result.failAuditLevel.auditDesc}
                                    </c:otherwise>
                                </c:choose>

                             </td>

C:fn:length标签的使用,主要用来操作处理字符串。

              <c:choose >
                                    <c:when test="${fn:length (result.failAuditLevel.auditDesc) > 10}">                                         <a style="TEXT-DECORATION:none"               title="${result.failAuditLevel.auditDesc}">${fn:substring (result.failAuditLevel.auditDesc,"0","3")}...</a>
                                    </c:when>
                                    <c:otherwise>
                                        ${result.failAuditLevel.auditDesc}
                                    </c:otherwise>
                   </c:choose>

D:在有些情况下URL重新的情况下传参数 ,比如参数里面有%之类的 ,直接传会导致字符丢失 。

           此时应该采用<c:param name="id" value="${result.rowId}"/>这样的形式来传参数。

E:有的时候,修改和添加公用一个form页面,但提交的action是不同的,这个时候可以用c:url标签。

           <form:form action="${submitUrl }" name="form1" method="post" onsubmit="return checkverify();" modelAttribute="ipAddress">

            决绝办法:

          <c:choose>
                <c:when test="${ipAddress.rowId!=null}">
                    <c:url var="submitUrl " value="/verifyfactor/ip_update.do"/>
                   
                </c:when>
                <c:otherwise>
                   <c:url var="submitUrl " value="/verifyfactor/ip_save.do"/>
                   
                </c:otherwise>
            </c:choose>

原文地址:https://www.cnblogs.com/danghuijian/p/4400622.html