J2EE (十) 简洁的JSTL、EL

时间:2023-03-08 19:56:57
  1. 简介
    1. JSTL(JSP Standard Tag Library ,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库。
    1. 由四个定制标记库(core、format、xml 和 sql)和一对通用标记库验证器(ScriptFreeTLV 和 PermittedTaglibsTLV)组成。
    1. JSTL是为了简化JSP页面,让它更加简洁尽量不包含Java代码,只进行数据显示而开发出来的一个标签库,弥补了JSP的不足。
  2. 特点
    1. 在应用程序服务器之间提供了一致的接口,最大程度地提高了WEB应用在各应用服务器之间的移植。
    1. 简化了JSP和WEB应用程序的开发。
      1. 自从有了JSTL我们不再需要写大量的Java代码,只需要小小的几个标签即可完成大的功能,例如查询数据库、显示数据、逻辑判断等等。
  3. 使用
    1. JSTL要与EL(expression language)表达式语言结合使用,标签库作用是流程控制,如果想实现某个功能要与表达式联合起来,比如是函数与控制语句一样。
    2. 使用JSTL需要引入jstl.jar和standard.jar两个包。并在JSP文件中声明标签,如下
      1. 标签代码()
    3. 核心库Core,主要通过举例来说明
      1. Servlet代码
        1. 	@Override
          protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
          //将字符串赋值给request对象
          request.setAttribute("hello", "hello world");
          request.setAttribute("welcome", "<font color='red'>世界欢迎你!</font>"); request.setAttribute("v1", 9);
          request.setAttribute("v2", 10); //类结构
          Group group=new Group();
          group.setName("09生科一班");
          //数组对象
          List users=new ArrayList();
          for(int i=0;i<10;i++)
          {
          User user=new User();
          user.setAge(10);
          user.setName("李龙生"+i);
          user.setGroup(group); users.add(user);
          }
          //设置到request中
          request.setAttribute("users", users); //Map
          Map map=new HashMap();
          map.put("k1", "k1");
          map.put("k2", "k2");
          map.put("k3", "k3");
          map.put("k4", "k4");
          request.setAttribute("map", map); request.setAttribute("strTokens", "1,2,3,4,5");
          request.getRequestDispatcher("/Jstl_Core.jsp").forward(request, response);
          }
      1. JSP代码
        1. <body>
          <h1>测试JSTL核心库</h1>
          <br>
          <li>采用c:out标签</li><br>
          hello使用:<c:out value="123"></c:out><br>
          hello使用:<c:out value ="hello"></c:out><br>
          hello使用:<c:out value="${hello }"></c:out><br>
          hello使用:<c:out value="${hello123 }" default="没有值"></c:out><br>
          hello使用:<c:out value="${hello123 }" >没有值</c:out><br>
          hello使用:<c:out value="${welcome}" ></c:out><br>
          hello使用(escapeXml):<c:out value="${welcome}" escapeXml="true"></c:out><br>
          hello使用:${welcome} //测试大于、小于符号
          <li>condition action sample</li>
          <c:if test="${v1 lt v2 }">
          v1小于v2
          </c:if> <p>
          <li>c:when,c:choose,c:otherwise标签</li>
          <c:choose>
          <c:when test="${v1 gt v2 }">
          v1大于v2 </c:when>
          <c:otherwise>
          v1小于v2
          </c:otherwise>
          </c:choose> <p>
          <li>演示循环控制标签forEach</li>
          <table border="1">
          <tr>
          <td>用户名:</td>
          <td>年龄:</td>
          <td>班级:</td>
          </tr>
          //选择标签
          <c:choose>
          //条件标签
          <c:when test="${empty users }">
          <tr>
          <td colspan="3">没有符合条件的数据!</td>
          </tr>
          </c:when>
          //否则执行这里面内容
          <c:otherwise>
          <c:forEach items="${users}" var="user">
          <tr>
          <td>${user.name }</td>
          <td>${user.age }</td>
          <td>${user.group.name }</td>
          </tr>
          </c:forEach>
          </c:otherwise>
          </c:choose>
          </table> //循环map里面的键值对
          <li>Map循环</li>
          <c:forEach items="${map }" var="entry">
          ${entry.key },${entry.value }<br>
          </c:forEach>
          //将某个字符串,用指定的符号分割
          <h1>c:Tokens标签</h1>
          <c:forTokens items="${strTokens }" delims="," var="a">
          ${a }<br>
          </c:forTokens>
          //将某个页面导入到当前页
          <h2>c:import标签</h2>
          <c:import url="http://localhost:8080/drp4.5/login.jsp"></c:import> <h2>c:url,c:param标签</h2>
          <c:url value="http://localhost:8080/drp4.5/login.jsp" var="u">
          <c:param name="userId" value="long"></c:param>
          </c:url>
          ${u }
          </body>
    4. 格式化库Format
      1. Servlet代码
        1. @Override
          protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
          request.setAttribute("today", new Date());
          request.setAttribute("n", 1234567.234);
          request.setAttribute("n", 0.01234); request.getRequestDispatcher("/jstl_fmt.jsp").forward(request, response);
          }
      1. JSP代码
        1. <body>
          <h1>test format date tag</h1>
          today(default)<fmt:formatDate value="${today }"/><br>
          today(type=date)<fmt:formatDate value="${today }" type="date"/><br>
          today(type=time)<fmt:formatDate value="${today }" type="time"/><br>
          today(type=both)<fmt:formatDate value="${today }" type="both"/><br>
          today(dateStype=short)<fmt:formatDate value="${today }" dateStyle="short"/><br>
          today(dateStype=full)<fmt:formatDate value="${today }" dateStyle="full"/><br>
          today(dateStype=long)<fmt:formatDate value="${today }" dateStyle="long"/><br>
          today(pattern="yyyy/mm/dd hh:mm:ss")<fmt:formatDate value="${today }" pattern="yyyy/mm/dd hh:mm:ss" var="u"/><br>
          ${u } <h2>digital format tag</h2>
          n(default)<fmt:formatNumber value="${n}"></fmt:formatNumber><br>
          n(pattern="###,###,###.####")<fmt:formatNumber value="${n}" pattern="###,###,###.####"></fmt:formatNumber><br>
          n(pattern="###,###,###.0000")<fmt:formatNumber value="${n}" pattern="###,###,###.0000"></fmt:formatNumber><br>
          n(groupingUsed="false")<fmt:formatNumber value="${n}" groupingUsed="false"></fmt:formatNumber><br>
          n(groupingUsed="false")<fmt:formatNumber value="${n}" maxIntegerDigits="12" minIntegerDigits="10"></fmt:formatNumber><br>
          n(maxFractionDigits="8" minFractionDigits="5")<fmt:formatNumber value="${n}" maxFractionDigits="8" minFractionDigits="5"></fmt:formatNumber><br> <h3>currency tag</h3>
          n(type="currency")<fmt:formatNumber value="${n }" type="currency"></fmt:formatNumber>
          n(type="currency" currencySymbol="$")<fmt:formatNumber value="${n }" type="currency" currencySymbol="$"></fmt:formatNumber>
          n(type="percent")<fmt:formatNumber value="${n }" type="percent"></fmt:formatNumber> </body>
  1. 目前应用
    1. JSTL可以跨多种服务器运行,方便了开发和程序的移植性,现在用这种标签在Web层开发的人也越来越多,需要灵活掌握和运用。