jstl

来源:互联网 发布:液压系统计算软件 编辑:程序博客网 时间:2024/05/01 13:50

目标
    回顾昨天的代码
    了解 JSTL 标签库
    掌握 EL 表达式语言
    掌握 核心的 JSTL 标签---core 中的循环判断
   
1、回顾昨天的代码
    页面经常使用的不变的数据,应该放在 Session 中。
   
2、了解 JSTL 标签库
    Jsp Standard tag lib(JSP 标准标签库)
    1.0  1.1  1.2 三个版本
    在 J2EE 1.4 中用 JSTL 1.1 版本
    在 JavaEE 1.5 中用 JSTL 1.2 版本,jar 默认包含在
JavaEE 1.5 中
   
    作用:类似于 JSP 动作标签。简化页面的代码开发
把页面上需要用 <%  %> 代码块实现的功能,用标签<>
实现。
   
3、掌握 EL 表达式语言
    Expression Language (表达式语言)
    作用:从 服务器作用域对象中,读取数据。
   pageContext 、request 、session 、application
   //Servlet 中
   request.setAttribute("message","你好");
   session.setAttribute("message","你不好");

   //jsp
   String msg = "";
   if(request.getAttribute("message")!=null){
        msg = (String)request.getAttribute("message");
   }
  
   out.println(msg);
   
   //EL
    ${message}  
    作用域范围(默认从小范围到大范围查找)
    pageContextScope、requestScope、sessionScope、
    applicationScope、、
   
    ${requestScope.message} 
    存取器    .    []
    Stock st = dao.findById(12);
    List list = dao.findAll();
    Map mp = new HashMap();
    mp.put("aa","一");
    request.setAttribute("stock",st);
    request.setAttribute("list",list);
    session.setAttribute("mp",mp);
   
    ${requestScope.stock.name}
    ${requestScope.stock.price}
    ${requestScope.stock.code}
   
    ${requestScope.list[0]}  //取出集合对象中第一个值
    ${sessionScope.mp["aa"]}
    运算符
     算术运算符 + - * / %
     关系运算符  ==(eq)  !=(ne)  <(lt)  <=(le)  >(gt)  >=(ge)
     逻辑运算符  &&(and)  ||(or)  !(not)
     非空运算符  empty
     三元运算符  ?:
   
    ${aa == bb}      ${aa eq bb}
    ${stu.sex} == '男'    错误
    ${stu.sex == '男' }   正确
   

4、掌握 核心的 JSTL 标签---core 中的循环判断
   (1)在项目中添加 jstl.jar 库
   (2)在每个要使用 jstl 的 jsp 页面头部,声明 taglib
<%@taglib uri="http://java.sun.com/jsp/jstl/core"
          prefix="c" %>

   判断          
   <c:if test="${返回boolean 值的El 表达式}"></c:if>
       
   <liu:choose>
        <liu:when test="1"></liu:when>
        <liu:when test="2"></liu:when>
        <liu:when test="3"></liu:when>
        <liu:when test="4"></liu:when>
        ........
        <liu:otherwise></liu:otherwise>
   </liu:choose>

       
   循环
    仿照 java 中的新的 for 循环  for(String temp:arr){}
   <c:forEach items="${sessionScope.stocklist}"
              var="st"      varStatus="statu">
     ${statu.count}  循环次数
   </c:forEach>
  
  
   循环状态值:
   索引                index
   是否是循环的开始    first
   是否是最后一次循环  last
   循环的次数          count
         
         
   <jsp:xxxx>
   <form >

原创粉丝点击