el and jstl

1. What is el and what are its characteristics?

     1) el: is the expression language.

   2) Format: ${ ...}

  3) Function: It is used to obtain the attribute value in the four major scope objects of jsp and display [assignment] on the node of jsp. It's essentially the <%= expression%> succinct form. In addition, it can fetch physical content. At the same time, the calculation and judgment of expressions can be realized.

  The reflection mechanism used by the bottom layer:

  A) You can directly access attributes in the following ways,

  Format: object.property . For example there is a user object. Take the age attribute inside.

  It can be expressed as ${user.age}; it can be judged automatically according to the true and false values: ${dog.sex? 'male':'female'}

  Note: age, etc. are written in a certain way that the first letter of the first word is lowercase and then follows the camel case format.

  B) Get the value in the container Use container.key to get the value in it.

  C) Get the value of the property from the four scope objects of JSP.

   pageContext ----> pageScope ${pageScope.property name} or ${property name}

 

      request ----> requestScope ${requestScope.property name} or ${property name}

 

      session ----> sessionScope ${sessionScope.property name} or ${property name}

 

      application ----> applicationScope ${applicationScope.property name} or ${property name}

 

    Note: When you don't write the XXXScope prefix, but directly use ${attribute name} to get the values ​​in the four major scopes, you will find them from small to large, and if you find them, you will end. That is, the smaller the scope, the higher the priority;

  D) Get the value of the parameter from the request entity of the JSP:

 

                            param ----> ${param.parameter name}

        paramValues ​​---> ${paramValues.parameter name[index number] } or ${paramValues.parameter name['index number'] 

  E) Operations can be performed in EL expressions.

       Arithmetic operations, relational operations, logical operations:

       The relational operation ${ "5" > 3 } results in true;

       Note: Before the operation, if the data types are inconsistent, automatic type conversion will occur. Such as: ${'123' + 100};

       Note: String concatenation cannot be performed in EL expressions. ${"Beijing" + 2008} //Error

  4) Unique operators.

  A) . and []; //here. is a member operator; [] is an offset operator;

  EL in order to get the entity content. For this purpose, the special keyword param or paramValues ​​is provided

             For example: early, get the entity content of the request: <%= request.getParameter("uname") %>

                   Now, get the entity content when requested: ${param.uname}                         

                   In the early days, in the entity content of the same name and different values ​​when getting the request:

                        <%

                              String[] favs = request.getParameterValues("fav");

                               for(int i = 0; i < favs.length; i++ ){

                                        out.print( favs[i] + "  ");

                               }

                        %>

                        

                   现在, 获取请求时的同名不同值的实体内容中

          [其中第一个参数是]: ${paramValues.fav[0] }

                   [第二个参数是: ]  ${paramValues.fav['1'] }

     b) empty  判断运算符

   当属性值为空值,EL中如何判断?

     案例如下:    

             <%

                       //String name = null;

                       String name = "";

                       request.setAttribute("name", name);

                      

                       Map<String,Integer> dogs = new HashMap<String,Integer>();

                       session.setAttribute("dogs", dogs);

                      

                       Set<String> cats = new TreeSet<String>();

                       List<String> cars = new LinkedList<String>();

                       pageContext.setAttribute("cats", cats);

                       pageContext.setAttribute("cars", cars);

             %>

             在EL表达式中,对属性值进行空值判断

             例如: 对request对象的name属性值进行判断,其结果为:   ${ empty requestScope.name}

             例如: 对session对象的dogs属性值进行判断,其结果为:   ${ empty sessionScope.dogs } ----- ${empty dogs }        

             例如: 对pageContext对象的cats属性值进行判断,其结果为:   ${ empty cats } 

             例如: 对pageContext对象的cars属性值进行判断,其结果为:   ${ empty cars } 

             特别注意:  EL表达式中的变量必须是 四大作用域的属性或请求实体中的参数。

2 、 什么是JSTL? ----jsp standard  tag lib;

    它是 JSP 的标准标签库。

      它主要是将 java的流程控制语句简化或优化成一个 JSP页面上可直接使用的标签。

 

        

 

3、JSTL的基本格式?

 

         格式:

 

          <c:标签>

 

                输出的信息

 

          </c:标签>

注意: 在使用 JSTL标签之前,必须在JSP页面使用 taglib 指令将用到的标签库导入过来。

 

 

4、导入标签库的taglib指令,其使用方式如下:

 

         <%@ taglib prefix="前缀" uri="标签库的地址及库名" %> 。。。//一般前缀使用字母c。

 

        

 

5、常用的标签库及其标签?

 

         a) http://java.sun.com/jstl/core_rt 核心标签库  

 

---> [ <c:if> | <c:choose><c:when><c:otherwise> | <c:forEach> ]

 

     另外了解: <c:url> | <c:param> | <c:import> | <c:redirect> 

 

     而 请求转发:<jsp:forward page=” xxx.jsp”> </jsp:forward> 形式称之为JSP的页面元素或页面标签,它是JSP内置的。

 

     重定向<c:redirect url=”xxx.jsp ”></c:redirect>

 

     b) http://java.sun.com/jstl/fmt_rt  格式化标签 --->[ <fmt:formatDate> | <fmt:formatNumber> ] 

 

 

 

6、<c:if> 标签的应用

 

       a) 形式一

 

                   <c:if test="${条件表达式}" var="存放结果的变量">

 

                       结果表现体

 

                  </c:if>      

 

            

 

    b) 形式二

 

             <c:if test="${条件表达式 }" var="存放结果的变量" scope="作用域对象">

 

                       结果表现体

 

             </c:if>

 

            

 

 注意: 在JSP的 jstl标签库中 <c:if>只是单向的,没有双向的。 那问: 如何实现双向的if语句功能?

 

例如:

 

<c:if test=”${dog.sex==true}”> 公</c:if>

 

<c:if test=”${dog.sex==false}”> 母</c:if>

 

7、<c:choose><c:when><c:otherwise> 标签的应用

 

        

 

         <c:choose>

 

             <c:when test="${条件表达式}"> 结果表现体  </c:when>

 

             <c:when test="${条件表达式}"> 结果表现体  </c:when>

 

             ......

 

             <c:otherwise> 结果表现体  </c:otherwise>

 

    </c:choose>

 

   

 

8、<c:forEach>标签的应用形式

 

         a) 形式一 

 

             <c:forEach begin="起始值" end="终止值" var="存放每次循环的结果变量">

 

                       ${表示式 }

 

             </c:forEach>

 

            

 

            注意: 它相当于java中的标准for循环语句  for(int i=1; i<=10; i++) { ... }

 

           

 

         b) 形式二

 

                    <c:forEach begin="起始值" end="终止值" var="存放每次循环的结果变量" step="增量" >

 

                       ${表示式 }

 

             </c:forEach>            

 

            

 

            注意: 它相当于java中的标准for循环语句  for(int i=1; i<=10; i+=增量) { ... }

 

           

 

         c) 形式三

 

                   <c:forEach items="${容器}" var="存放每次循环的值" varStatus="状态变量">

 

                        结果表现体

 

               </c:forEach> 

 

              

 

         注意: 状态变量中有一个  index 属性,表示每种状态的索引号。

 

    

 

        注意: 这种形式最常用。

 

        例如: 若有 List<Person>  ps = new ArrayList<Person>();

 

                        则对 List 的 ps 容器进行遍历如下:<br/>

 

                        <c:forEach items="${ps}" var="person" varStatus="id">

 

                                  ${id.index + 1} ---- ${person.name} --- ${person.sex ? "男":"女" } --- ${person.age }  <br/>

 

                        </c:forEach> 

 

                       

 

9、<fmt:formatDate> 和 <fmt:formatNumber>格式化标签的应用

 

         一、日期的格式化:

 

            1) 固有格式:

 

                 <fmt:formatDate value="${d1 }" type="date" dateStyle="short"/> <br/>

 

                 其中 type="date" 时, 有: dateStyle="short"/"long"/"full"

 

            2) 自定义格式

 

                        <fmt:formatDate value="${d1 }" pattern="yyyy-MM-dd"/> <br/>

 

                        <fmt:formatDate value="${d1 }" pattern="||^_^yyyy年MM月dd日^_^||"/> <br/>

 

         

 

     二、时间的格式化:

 

            1) 固有格式:

 

                  <fmt:formatDate value="${d1 }" type="time" dateStyle="short"/> <br/>

 

                 其中 type="time" 时, 有: dateStyle="short"/"long"/"full"

 

            2) 自定义格式:

 

                   <fmt:formatDate value="${d1 }" pattern="HH:mm:ss"/> <br/>

 

                   <fmt:formatDate value="${d1 }" pattern="北京时间>>>HH时mm分ss秒"/> <br/>

 

         

 

     三、格式化数值型数据

 

             <fmt:formatNumber value="${data }" pattern="########.##" /> <br/>

 

             <fmt:formatNumber value="${data }" pattern="##,###,###.##" /> <br/>

 

             <fmt:formatNumber value="${data }" pattern="##,###,###" /> <br/>

 

             <fmt:formatNumber value="${data }" pattern="##,###,###.0" /> <br/>

 

             <fmt:formatNumber value="${data }" pattern="00,000,000.0" /> <br/>

 

             <fmt:formatNumber value="${data }" pattern="$00,000,000.0" /> <br/>

 

             <fmt:formatNumber value="${data }" pattern="¥00,000,000.0" />

 

10.Jsp的EL与JSTL对两种容器进行操作:

 

         1)存放对象(javaBean)的容器

 

                   在<c:forEach>中可以直接通过EL访问属性

 

                   Obj.getXXX()--->${obj.属性}

 

   2)存放Map容器的List容器

 

                   在<c:forEach>中可以直接通过EL去访问属性;

 

                   Map.get(key)------>${map.key}

 

 

                                    

 

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324769086&siteId=291194637