JSP的EL表达式详解[转]

    xiaoxiao2021-03-25  139

    EL表达式 1、EL简介 1)语法结构 expression2[].EL.[].?使[] {user.My-Name}应当改为 user[MyName][]. {sessionScope.user[data]}中data 是一个变量 3)变量 EL存取变量数据的方法很简单,例如: usernameusernameusernamePageRequestSessionApplicationusernamenullELPagePageScopeRequestRequestScopeSessionSessionScopeApplicationApplicationScope4)1EL {}表示,可用在所有的HTML和JSP标签中作用是代替JSP页面中复杂的JAVA代码. 2–EL表达式可操作常量 变量 和隐式对象. 最常用的 隐式对象有 param {paramValues}. param. {paramValues}表示返回请求参数的一组 值.pageScope表示页面范围的变量.requestScope表示请求对象的变量. sessionScope表示会话 范围内的变量.applicationScope表示应用范围的变量. 3 –<%@ page isELIgnored=”true”%> 表示是否禁用EL语言,TRUE表示禁止.FALSE表示不禁 止.JSP2.0中默认的启用EL语言. 4– EL语言可显示 逻辑表达式如 trueandfalsefalse {5>6} 结 果是false 算术表达式如 ${5+5} 结果是10 5–EL中的变量搜索范围是:page request session application 点运算符(.)和”[ ]”都是 表示获取变量的值.区别是[ ]可以显示非词类的变量

    2、EL隐含对象 1)与范围有关的隐含对象 与范围有关的EL 隐含对象包含以下四个:pageScope、requestScope、sessionScope 和 applicationScope; 它们基本上就和JSP的pageContext、request、session和application一样; 在EL中,这四个隐含对象只能用来取得范围属性值,即getAttribute(String name),却不能取得 其他相关信息。 例如:我们要取得session中储存一个属性username的值,可以利用下列方法: session.getAttribute(“username”) 取得username的值, 在EL中则使用下列方法 sessionScope.username2)paramparamValuesELrequest.getParameter(Stringname)request.getParameterValues(Stringname)EL使paramparamValues {param.name} ${paramValues.name}

    3.其他隐含对象 1)cookie JSTL并没有提供设定cookie的动作, 例:要取得cookie中有一个设定名称为userCountry的值,可以使用 cookie.userCountry2)headerheaderValuesheader使 {header[“User-Agent”]}。 另外在鲜少机会下,有可能同一标头名称拥有不同的值,此时必须改为使用headerValues 来取得 这些值。 3)initParam initParam取得设定web站点的环境参数(Context) 例:一般的方法String userid = (String)application.getInitParameter(“userid”); 可以使用 initParam.useriduserid4)pageContextpageContext {pageContext.request.queryString} 取得请求的参数字符串 pageContext.request.requestURLURL {pageContext.request.contextPath} 服务的web application 的名称 pageContext.request.methodHTTP(GETPOST) {pageContext.request.protocol} 取得使用的协议(HTTP/1.1、HTTP/1.0) pageContext.request.remoteUser {pageContext.request.remoteAddr } 取得用户的IP 地址 pageContext.session.newsession {pageContext.session.id} 取得session 的ID pageContext.servletContext.serverInfo4)>1.+ 、/或div、%或mod 2.关系运算符有六个:==或eq、!=或ne、<或lt、>或gt、<=或le、>=或ge 3.逻辑运算符有三个:&&或and、||或or、!或not 4.其它运算符有三个:Empty运算符、条件运算符、()运算符 例: emptyparam.name {A?B:C}、${A*(B+C)} 5) EL函数(functions) 语法:ns:function( arg1, arg2, arg3 …. argN) 其中ns为前置名称(prefix),它必须和taglib 指令的前置名称一置 6) 补充: <%@ taglib prefix=”c” http://java.sun.com/jstl/core_rt“>http://java.sun.com/jstl/core_rt” %> FOREACH:

    转载请注明原文地址: https://ju.6miu.com/read-10554.html

    最新回复(0)