JSP EL expression

 

EL namely Expression Language (expression language) is a simple data access language.

EL and output expression <% =%> similar function, the output value of the expression is, but they use different subjects in both.

 

 

EL expression syntax:

 } // {$ expression after an expression not semicolon.

 

 

Access constants:

${1}    //int 

${"ok"}   //String

${false}   //boolean

$ {Null} // null

 

 

Access variables:

$ {Name = 1} // 1. EL variable in expressions without first statement | defined, can be used directly. For example, here is not declared int name.

$ {Name} // access variable value

Java to get the value if the variable does not exist or value is null ,, will be reported null pointer exception. El expression is not, EL expression outputs an empty string.

 

 

Arithmetic operators:

$ {A + b} // pressurized

$ {Ab} // Save

$ {A * b} // multiply

$ {A / b} or $ {a div b} // addition

$ {A% b} or $ {a mod b} // modulo

 

 

Comparison operators:

Returns a boolean value, to avoid the html tag <> conflict, the English abbreviation may be used.

${a==b}    或  ${a eq b}      equals

${a!=b}  或  ${a nq b}      not equals

${a<b}  或  ${a lt b}     less  than

${a<=b}  或  ${a  le  b}     less equals

${a>b}  或  ${a gt  b}     greater  than

${a>=b}  或  ${a ge b}     greater  equals

 

 

Logical Operators:

${a && b}  或   ${a and b}

${a || b}   或  ${a or b}

${!a}   或   ${not  a}

 

 

Ternary operator:

${a?b:c}

 

 

the empty operator:

$ {Empty a} // determines whether a variable is empty, returns a boolean value

If a variable is not present, it returns true;

If a variable is present, but the value is null, return true;

If the variable is a collection type (List, Set, Map), the value is not null, but the collection is empty, returns true.

 

 

When the priority uncertain operators, plus ().

 

 

 

EL attribute takes object (member variables)

  • Object name $ {.} // attribute name attribute name without quotes. Properties are generally private, can not be accessed directly, in real terms, or call the getter () method.
  • {$ Object name .getXxx ()} // use getters () method
  • {Object name $ [ "attribute name"]} // attribute names in quotes

 

 

EL array elements take

  • {$ Array name [index]}

 

 

 

 

EL expressions output expression <% =%> of the similarities and differences

  • Use the same position. Html tag placement and the same, in addition also on the properties of html tags, such as <form action = "$ {}"> </ form>.
  • JSP output expression can access the built-in objects, you can visit <!%%> Variable <%%> defined, you can call <%!> Defined in the method, you can access the <jsp: useBean /> object definition .
  • JSP EL expression can not access the built-in objects, can not access the <!%%> Variable <%> definition, can not call <%!> Defined in the method, access <jsp: useBean /> defined object .

EL expressions can access so much data, why is the data access language?

EL expression has its own built-in objects, in general, an EL expression using its own built-in objects to access data.

 

 

 

 

EL expressions of the 11 built-in objects | implicit object

  • pageScope    
  • requestScope
  • sessionScope
  • applicationScope
  • pageContext // equivalent to JSP built pageContext, commonly used in the acquisition of other objects
  • param // all request parameters (form data, url in? later sections)
  • paramValues
  • All fields header // HTTP request header
  • headerValues
  • cookie
  • initParam // initialization parameters entire web application

These objects are built using Map to store data.

 

 

Although not access EL expression variable <%> defined, such as  <% = Student student new new Student ();%>  , student can access objects in an EL expression.

However Servlet, JSP may be used setAttribute () data (variables) into four domains, EL expression data extracted from the domain.

 1  <%
 2       pageContext.setAttribute("id","01");   //page域
 3       request.setAttribute("name","chy");  //request域
 4       session.setAttribute("age",20);  //session域
 5       application.setAttribute("score",100);  //application域
 6   %>
 7 
 8 <%--  2种写法等效--%>
 9   ${pageScope.get("id")}
10   ${pageContext.getAttribute("id")}
11 
12   ${requestScope.get("name")}
13   ${pageContext.request.getAttribute("name")}
14   
15   ${sessionScope.get("age")}
16   ${pageContext.session.getAttribute("age")}
17   
18   ${applicationScope.get("score")}
19   ${pageContext.servletContext.getAttribute("score")}

 

 

(1) pageScope, requestScope, sessionScope, applicationScope four common method domain objects

  • get (key) // Gets the property value
  • getOrDefault (key, defaultValue) // If no attribute domain name, the default value is returned
  • clear () // clear the entire domain
  • remove (key) // remove a property
  • remove(key,value)
  • replace (key, oldValue, newValue) // replace the value of a property
  • containsKey (key) // contains the specified key, returns a boolean
  • keySet () // Gets a collection of key domains, return Set
  • values ​​() // set values ​​acquired return Collection
  • entrySet () // get entry (a key-value pairs) is set to return Set

The method takes only four data fields, the data is not provided setXxx () method.

 

 

 

Conventional method (2) pageContext of

  • .servletContext
  • .servletConfig
  • .out
  • .session
  • .request
  • .response
  • .exception

These objects get other ways, ostensibly to get through member variables, actually still call getXxx () to get.

pageContext the equivalent of a large Map, which also installed some small Map.

 

EL built 4 domain data objects setXxx not set () method. Data can be set with a few member variables to achieve the above pageContext:

${pageContext.request|session|servletContext.set|remove|getAttribute()   }

With only three fields, page fields it?

pageContext page domain itself:  pageContext.set | GET | removeAttribute () 

 

In addition there are two common methods pageContext:

  • forward (String url) // forward the request
  • include (String url) // request comprising

Are internal server act, only the browser sends an HTTP request, you can see the address bar is not changed.

 

 

 

Methods EL expression summary data from the field

  • ${ pageScope.get("age") }     //xxxScope.get("xxx")
  • $ {PageScope.age} // get directly to attribute names, attribute names are not quoted. Very convenient, it is recommended.
  • $ {Age} // get directly to attribute names. To this domain is not specified, the default from the page, request, session, applicaiotn in order to find, find a return, no longer continue to look down.
  • $ {PageContext.request.getAttribute ( "age")} // Get first domain specified by pageContext, data reacquisition
  • $ {PageContext.getAttribute ( "age")} // Special: pageContext page itself represents domain

 

 

 

(3) the ultimate, paramValues

param, it is stored paramValues ​​request parameters (form data, url in? later sections).

Request parameters are stored as 2 parts:

  • param. key is a String (field name), value is a String. Because using Map storage, key can not be repeated, so have the same field names, for example? Hobby = "running" & hobby = "swimming", only the first store, behind the same is not stored.
  • paramValues. key is a String (field name), value is a String []. With or without the same field names, values ​​are stored as a String [] in the form of, for example? Hobby = "running", is stored as { "running"}.
<form method="post" action="handle.jsp">
     姓名:<input type="text" name="name" /><br />
     爱好:<input type="checkbox" name="hobby" value="running" >跑步
     <input type="checkbox" name="hobby" value="swimming" >游泳<br />
     <button type="submit">提交</button>
 </form>

 

<%
    request.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
%>

<p>姓名:${param.name}</p>
<p>爱好:${paramValues.hobby[0]}、${paramValues.hobby[1]}</p>

 

param is Map <Stirng, String> type, is paramValues ​​Map <Stirng, String []> type, these two objects can be built using the EL Map method.

 

 

 

(4) cookie objects

Map storage request cookie object represents a collection of all the Cookie header.

$ {Cookie.JSESSIONID} // Gets the Cookie JSESSIONID

$ {Cookie.JSESSIONID.name} // get JSESSIONID this Cookie's name

$ {Cookie.JSESSIONID.value} // get the value JSESSIONID this Cookie

 

Guess you like

Origin www.cnblogs.com/chy18883701161/p/11407251.html