zoukankan      html  css  js  c++  java
  • EL与JSTL

    El(Expression Language),全称表达式语言,目的是尽量的消除JSP页面中的java代码,其方法如下:

     1 <%
     2         request.setAttribute("name", "张三");
     3         session.setAttribute("name", "田奇");
     4         session.setAttribute("username", "吴昂无");
     5         application.setAttribute("name", "李四");
     6         request.setAttribute("gender", "");
     7     %>
     8 
     9     <h2>El表达式</h2>
    10     3 + 4: ${3 + 4}  <br>
    11     3 * 4: ${3 * 4}  <br>
    12     3 / 5: ${3 / 5}  <br>
    13 
    14     <h2>获取请求的参数</h2>
    15     通过内置对象Request来获取数据:<%=request.getParameter("username") %> <br>
    16     通过内置EL来获取请求的参数:${param.username} <br>
    17 
    18     <h2>获取request.setAttribute的值:</h2>
    19     <!-- requestScope是固定写法, name是其他位置 request.setAttribute() 的key, -->
    20     ${requestScope.name } <br>  <%-- 相当于 <%=request.getAttribute("name")%> --%>
    21 
    22     <h2>获取session.setAttribute的值:</h2>
    23     <!-- sessionScope是固定写法, username是其他位置 session.setAttribute() 的key, -->
    24     ${sessionScope.username}  <%-- 相当于 <%=session.getAttribute("username")%> --%>
    25 
    26     <h2>获取application.setAttribute的值:</h2>
    27     <!-- applicationScope是固定写法, name是其他位置 application.setAttribute() 的key, -->
    28     ${applicationScope.name}  <%-- 相当于 <%=application.getAttribute("name")%> --%>
    29 
    30     <h2>按照范围来找数据</h2>
    31     <%--
    32         如果直接写值,会按照reqeust -> sesssion -> application这个层级来找。
    33         在实际的应用中,强烈建议把requestScope、sessionScope、applicationScope带着,因为方便代码的维护。
    34      --%>
    35     ${applicationScope.name}
    36 
    37     <h2>Empty的用法</h2>
    38     <!-- 判断一个数据是否为null, 或者一个字符串为空或"" -->
    39     ${empty requestScope.gender} <br>
    40     <!-- empty可以用于三元表达式 -->
    41     ${empty requestScope.gender ? "默认值" : requestScope.gender}
    42 
    43     <h2>gt、ge、lt、le、eq</h2>
    44     5 > 3: ${5 > 3} <br>
    45     5 > 3: ${5 gt 3} <br>  <%-- greater than --%>
    46     14 < 3: ${14 < 3} <br>
    47     14 < 3: ${14 lt 3} <br>  <%-- less than --%>
    48     14 >= 3: ${14 ge 3} <br>  <%-- greater or equals --%>
    49     14 <= 3: ${14 le 3} <br>  <%-- less or equals --%>
    50     14 == 3: ${14 eq 3} <br>  <%-- equals --%>

    ​ JSTL(jsp standard tag library)全称为"标准标签库",使用一套标准的标签库,来极大的简化代码的编写,必须要配置EL来使用

    ​     1) 引入javax.servlet.jsp.jstl.jar和jstl-impl.jar两个jar包

    ​    2) 在jsp页面的头顶加上标准标签库指令

     1 --%>
     2 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     3     <!-- 标签库指令
     4     prefix  前缀, 表示jstl的标签的前缀,可以使用任何的字母(一个或多个都可以)
     5     但是大家都写c, 所以我们也写c
     6     suffix  后缀
     7     -->
     8     <%--标签库指令--%>
     9 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    10 <html>
    11 <head>
    12     <title>Title</title>
    13 </head>
    14 <body>
    15     <%
    16         request.setAttribute("age", 20);
    17         request.setAttribute("gender", null);
    18     %>
    19     <h2>多条件判断</h2>
    20     <c:if test="${empty requestScope.gender && requestScope.age gt 18}">
    21         性别为空,并且是成年人.
    22     </c:if>
    23     <hr>
    24     <!--
    25         requestScope.gender为空值的问题??????
    26     -->
    27     <h5>${requestScope.gender == ''}</h5>
    28     <!-- 透过tomcat源码部分,判断requestScope.gender如果是一个null,
    29        就直接返回一个false, equals判断不做了,也就是为什么抛出空指针异常的问题。
    30 
    31       在实际的工作中,我们通过一个字符串是否为 null, 或者 "", 均通过empty来判断。
    32     -->
    33     <c:if test="${!requestScope.gender.equals('') && requestScope.age gt 18}">
    34         性别为空,并且是成年人.
    35     </c:if>
    36 
    37     <h2>if的用法</h2>
    38     <!--
    39         if是否成立,要根据test中的返回结果,但是test必须是EL.
    40         jstl中,只有if,没有else.
    41      -->
    42     <c:if test="${requestScope.age gt 18}">
    43         成年人.
    44     </c:if>
    45 
    46     <h2>choose when otherwise的用法,是if-else的替代语法</h2>
    47     <!--
    48         有点类似于switch() case .
    49     -->
    50     <c:choose>
    51         <c:when test="${requestScope.age > 40}">
    52             中年人
    53         </c:when>
    54         <c:when test="${requestScope.age > 30}">
    55             壮年
    56         </c:when>
    57         <c:when test="${requestScope.age > 18}">
    58             成年人
    59         </c:when>
    60         <c:otherwise>  <!-- 否则 -->
    61             小青年
    62         </c:otherwise>
    63     </c:choose>
    64 
    65     <h2>循环 for</h2>
    66     <%--
    67         for(int i = 50; i <= 100; i+=10) {
    68 
    69         }
    70     --%>
    71     <%--
    72         begin是开始的值;
    73         end是结束;
    74         var是定义的变量名.
    75         step是步进的意思,就是每次加多少
    76     --%>
    77     <c:forEach var="i" step="10" begin="50" end="100">
    78         ${i}
    79     </c:forEach>

  • 相关阅读:
    3.1《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——下载文件
    rem实现手机页面自动缩放
    Git 常用命令
    使用 canvas+JS绘制钟表
    JS 操作数组的方法
    Node.js Request方法
    兼容浏览器的点击事件
    ES6知识点
    上传项目到github上
    JavaScript 编码风格
  • 原文地址:https://www.cnblogs.com/dabu/p/12701246.html
Copyright © 2011-2022 走看看