EL表达式和JSTL基础
el表达式:
1、语法运算:@{expression}
2、属性范围在EL中的名称
PageContext pageScope
Request requestScope
Session sessionScope
Application applicationScope
3、登陆的时候,当登陆错误的时候,给出提示
<%if(request.getAttribute("error")!=null) {%>
<%=request.getAttribute("error") %>
<%} %>
把<%=request.getAttribute("error") %>
改用el表达式输出 ${requestScope.error}
el表达式不能使用,加上这个:
<%@ page isELIgnored="false" %>
4、JSTL:标签库
1:<c:out value="${exception}" default="默认值"/>
value 需要显示出来的值
default 如果 value 的值为 null,则显示 default 的值
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
使用需导包:<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2:<c:set>主要用来将变量储存至 JSP 范围中或是 JavaBean 的属性中。
<c:set value = "aaa" var = "name"></c:set>
<c:set value="value" var="varName" scope="{ page|request|session|application }"/>
3:用于计算一个表达式并将结果输出。类似于 JSP 中 <%=%> 表达式。
<c:out value = "${name}"></c:out>
4:<c:remove>主要用来移除变量。
语法
<c:remove var="varName" scope="{ page|request|session|application }" />
5:<c:if>的用途就和我们一般在程序中用的 if 一样。
语法 1:没有本体内容(body)
<c:if test="testCondition" var="varName"
[scope="{page|request|session|application}"]/>
语法 2:有本体内容
<c:if test="testCondition" [var="varName"]
[scope="{page|request|session|application}"]>
具体内容
</c:if>
<c:set value="10" var="a"></c:set>
<c:if test="${a>5}" var="flag">
<c:out value="a>5"></c:out>
<c:out value="${flag}"></c:out></c:if>
6:<c:choose>本身只当做 <c:when> 和 <c:otherwise> 的父标签。
语法:
<c:choose>
本体内容( <c:when> 和 <c:otherwise> )
</c:choose>
例如:
<c:choose>
<c:when test="${score>=90}">
<c:out value="良好"></c:out>
</c:choose>
<c:otherwise>
<c:out value="不及格"></c:out>
</c:otherwise>
7:<c:forEach items=“${list}” var=“note”>
//循环体
</c:forEach>
说明: 1)items:是集合,用EL表达式;
2)var:变量名,存放items
3)begin:循环的初始值(整型);
4)end: 循环结束 ;
5)step:步长,循环间隔的数值;
8:表单提交的数据域比较多的时候显得非常烦琐,而JSTL提供一种非常简洁的方式:
${param.name}和$${paramValues.name}
9:<c:redirect>跳转 标签
用来代替response.sendRedirect()
<c:redirect url="login.jsp">
<c:param name="method" value="toIndex"/>
</c:redirect>
10:fn函数:导包:<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
fn:contains 判断字符串是否包含另外一个字符串
<c:if test="${fn:contains(name, searchString)}">
fn:containsIgnoreCase 判断字符串是否包含另外一个字符串(大小写无关)
fn:endsWith 判断字符串是否以另外字符串结束
<c:if test="${fn:endsWith(filename, '.txt')}">
fn:startsWith 判断字符串是否以某个子串开始
fn:indexOf 子字符串在母字符串中出现的位置
${fn:indexOf(name, '-')}
fn:join 将数组中的数据联合成一个新字符串,并使用指定字符格开
fn:length 获取字符串的长度,或者数组的大小
fn:replace 替换字符串中指定的字符
${fn:replace(text, '-','?')}
fn:split 把字符串按照指定字符切分
${fn:split(customerNames, ';')}
fn:substring 获取子串
${fn:substring(zip, 6, -1)}
fn:toLowerCase 转为小写
fn:toUpperCase 转为大写字符
fn:trim 去除字符串前后的空格