曾梦垚

导航

EL简介

一、EL简介

1.语法结构
    ${expression}
2.[]与.运算符
    EL 提供.和[]两种运算符来存取数据。
    当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符号,就一定要使用 []。例如:
        ${user.My-Name}应当改为${user["username"] }---${user.username }
    如果要动态取值时,就可以用[]来做,而.无法做到动态取值。例如:
        ${sessionScope.user[data]}data 是一个变量
3.变量
    EL存取变量数据的方法很简单,例如:${username}。它的意思是取出某一范围中名称为username的变量。
    因为我们并没有指定哪一个范围的username,所以它会依序从Page、Request、Session、Application范围查找。
    假如途中找到username,就直接回传,不再继续找下去,但是假如全部的范围都没有找到时,就回传null。
    属性范围在EL中的名称
        Page         pageScope-------------pageContext.getAtrribute()
        Request         requestScope-------request.gsetAtrribute()
        Session         sessionScope-------session.getAtrribute()
        Application     applicationScope---application.getAtrribute()
       
二、EL隐含对象
1.与范围有关的隐含对象
与范围有关的EL 隐含对象包含以下四个pageScope、requestScope、sessionScope 和applicationScope;
它们基本上就和JSP的pageContext、request、session和application一样;
EL中,这四个隐含对象只能用来取得范围属性值,即getAttribute(String name),却不能取得其他相关信息。

例如:我们要取得session中储存一个属性username的值,可以利用下列方法:
    session.getAttribute("username") 取得username的值,
EL中则使用下列方法
    ${sessionScope.username}

2.与输入有关的隐含对象
与输入有关的隐含对象有两个:param和paramValues,它们是EL中比较特别的隐含对象。

例如我们要取得用户的请求参数时,可以利用下列方法:
    request.getParameter(String name)
    request.getParameterValues(String name)
EL中则可以使用param和paramValues两者来取得数据。
    ${param.name}--->request.getParameter(name);
    ${paramValues.name[0]}---request.getParameterValues(name)[0]

3.其他隐含对象

cookie
JSTL并没有提供设定cookie的动作,
例:要取得cookie中有一个设定名称为userCountry的值,可以使用${cookie.userCountry}来取得它。

cookie.getName(userCountry).getValue()

 

    header和headerValues
header 储存用户浏览器和服务端用来沟通的数据
例:要取得用户浏览器的版本,可以使用${header["User-Agent"]}。
另外在鲜少机会下,有可能同一标头名称拥有不同的值,此时必须改为使用headerValues 来取得这些值。

initParam
initParam取得设定web站点的环境参数(Context)
例:一般的方法String userid = (String)application.getInitParameter("userid");
    可以使用 ${initParam.userid}来取得名称为userid

     --->  config.getInitParameter(userid);

pageContext
pageContext取得其他有关用户要求或页面的详细信息。
    ${pageContext.request.queryString}         取得请求的参数字符串

    ${pageContext.request.requestURL}         取得请求的URL,但不包括请求之参数字符串
    ${pageContext.request.contextPath}         服务的web application 的名称
    ${pageContext.request.method}           取得HTTP 的方法(GET、POST)
    ${pageContext.request.protocol}         取得使用的协议(HTTP/1.1、HTTP/1.0)
    ${pageContext.request.remoteUser}         取得用户名称
    ${pageContext.request.remoteAddr }         取得用户的IP 地址
    ${pageContext.session.new}             判断session 是否为新的
    ${pageContext.session.id}               取得session 的ID
    ${pageContext.servletContext.serverInfo}   取得主机端的服务信息

三、EL运算符
1.算术运算符有五个:+、-、*或$、/或div、%或mod
2.关系运算符有六个:==或eq、!=或ne、<或lt、>或gt、<=或le、>=或ge
3.逻辑运算符有三个:&&或and、||或or、!或not
4.其它运算符有三个:Empty运算符、条件运算符、()运算符
    例:${empty param.name}、${A?B:C}、${A*(B+C)}

 

 

 

 

1.基准路径  < base href="< %=basePath% >" >base基准路径   跳转的时候就是按基准路径来   如果没有base那么就按照相对路径来跳转<br>

   2.jsp中用JAVA程序需要 < % java  % >    注释< %    //  /**/    % >   java程序定义静态属性和方法< %! % ><br>

   3. HTML/XML 注释< !-- info    --><br>

   4.el表达式 $ {    }<br>

      a.el表达式是专门用来取值 <br>

按照由小到大的范围去取值  page< request  <   session  <  application   <br>

attribute===pageContext.getAttribute()< request.getAttribute()<  session.getAtribute()<  application.getAttribute()<br>

param==request.getParameter("")<br>

paramValues=request.getParameterValues("")<br>

$ { message  }=${message }<br>

$ { pageScope.message }  -- PageScope=${pageScope.message }<br>

$ { requestScope.message }--  RequestScope=${requestScope.message }<br>

$ { sessionScope.message }-- SessionScope=${sessionScope.message }<br>

$ { applicationScope.message }--  ApplicationScope=${applicationScope.message }<br>

$ { bean["name"]  }--$  {bean.name }---  bean.name=${bean.name }---${bean["name"] }---arr[]=${arr[0] }<br>

$   {  param.username } --等价--  < %=request.getParameter("username") %><br>

$   {    paramValues.fav[1] } ---等价---  < %=request.getParameterValues("fav")[1] % ><br>

$ {initParam.userid} ---等价this.getInitParameter("encode")---ecode=${initParam.encode}<br>

$  {cookie.username}--- ${cookie.username.value}<br>--

Cookie  cookie=new Cookie("username","godyang");

response.addCookie(cookie);

$ { empty  param.sex ? "no":"yes"  }= ${ empty  param.sex ? "no":"yes"  }<br>

$ {sex  eq "1" ? "":""  }=== ${sex  eq "1" ? "":""  }<br>

   5 中文转码  

     a.  <  a href=second.jsp?username2=平安 > second1< /a>------>< %=new String((request.getParameter("username2")).getBytes("iso-8859-1"),"UTF-8") %><br>

     b.  < a href=second.jsp?username2=< %=URLEncoder.encode(URLEncoder.encode("平安"))%>    > second3< /a>

     ---< %=URLDecoder.decode(request.getParameter("username2")) %><br/>

   

posted on 2016-12-24 23:45  曾梦垚  阅读(671)  评论(0编辑  收藏  举报