日期:2014-05-16  浏览次数:20289 次

EL表达式语言和JSTL标签小结(转)

(转自:http://blog.fjlylc.com/post/24.html)

EL表达式语言和JSTL标签小结

EL(Expression language) jsp表达式语言
${"hello"}//输出字符串常量
${str}? //输出str变量值
${user.name}//输出user对象的name属性
${list[1]}//访问list对象的第二项
${map["key"]}//访问map指定键值
默认web.xml文档头是支持EL表达式的,禁用EL表达式 <%@ page isELIgnored="true"%>
EL隐含对象::pageScope、requestScope、sessionScope 和applicationScope;
${sessionScope.username}相当于 session.getAttribute("username")?
与输入有关的隐含对象有两个:param和paramValues
${param.name}相当于??? request.getParameter(String name)
${paramValues.name}相当于?? request.getParameterValues(String name)
要取得cookie中有一个设定名称为userCountry的值,可以使用${cookie.userCountry}

JSTL(JSP Standard Tag Library ,JSP标准标签库)
在JSP页面引入核心标签库的代码为:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:out value=”EL表达式”>、<c:out value=”字符串”>显示数据对象(字符串、表达式)的内容
<c:set value="coo" var="name1" scope="session"></c:set>//把一个值coo放入session中。
${sessionScope.name1 }//从session中得到coo值 。

<jsp:useBean id="person" class="org.olive.Person" /> //html头部,JSP的指令元素指定要使用的
JavaBean,负责实例化Bean。
<c:set target="${person}" property="age">25</c:set>? //设置person的age=25
<c:catch var=”name1”>容易产生异常的代码</c:catch>? //name1存储异常信息
<c:if test="${param.uname=='admin' }" var=”adminchock”> // 条件控制if,true执行</c:if>包含的代
码,原型<c:if test=”条件1” var=”name” [scope=”page|request|session|application”]>

<c:forEach var=”name” items=”Collection” varStatus=”StatusName” begin=”begin” end=”end” step=”
step”>本体内容</c:forEach>
URL操作标签:<c:import>、<c:redirect>和<c:url>标签。它们的作用为:显示其他文件的内容、网页导
向、产生URL。