一、EL表达式(形式:${ })
1. 1 EL运算符
- 算术运算符: +、-、*、/、%
示例 结果
${1+1} 2
${1-1} 0
${1*3} 3
${3/2} 1.5
${5%3} 2
关系运算符:大于、小于、等于、不等于、大于等于、小于等于
示例 结果
${2>1} true
${1<0} false
${1==1} true
${3!=2} true
${5>=2} true
${4<=3} false
逻辑运算符: ||、&&、!
示例 结果
${true||true} true
${true||false} true
${false||false} false
${true&&true} true
${true&&false} false
${false&&false} false
${!true} false
${!false} true
条件运算符:${A?B:C}
示例: ${2>1? "true" : "false"} 结果 true
验证运算符:${empty var}
String username = "tom";
${empty username} 结果 true
1.2 EL内置对象
-
域对象
- pageScope
- requestScope
- sessionScope
- applicationScope
-
输入对象
- param // param.username 相当于 request.getParameter("username");
- paramValues // paramValues.hobby相当于 request.getParameterValues("hobby");
-
Cookie对象
- ${cookie.username.value} //获取cookie中username属性的值
-
initParam对象
- ${initParam.username} // 获取context对象中的参数值
-
如:
<context-param>
<param-name>username</param-name>
<param-value>tom</param-value>
</context-param>
-
pageContext对象
- ${pageContext.request.requestURL} //获取请求URL
- ${pageContext.request.queryString} //获取参数字符串
- ${pageContext.request.contextpath} //获取当前应用目录
- ${pageContext.request.method} //获取请求方式
-
1.3. EL存取器
- 获取JavaBean数据
${sessionScope.person.name}
${sessionScope.person.age}
- 获取Map数据
// HashMap userInfo= new HashMap(); userInfo.put("username","tom"); userInfo.put("password","123456");
- 获取JavaBean数据
${userInfo.username}
${userInfo.password}
- 获取数组数据
${names[0]} // String names = {"tom","lisa","jerry"}
${names[0]}
二、JSTL标签库
2.1 核心标签库
- <c:set var="username" value="value"></c:set>
- <c:out value="value"></c:out>
- <c:remove var="number"></c:remove>
- <c:if test=“ifCondition” var="varName" [scope="{page|request|session|application}"]></c:if>
- <c:choose>
······
<c:when test="testCondition">
</c:when>
······
<c:otherwise>
</c:otherwise>
</c:choose>
<c:forEach [var="varName"] items="collection" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]>
</c:forEach>