jstl

jstl

使用方法

       1导入jstl.jar和standard,jar两个jar包到项目文件下

       2在页面中引入要使用的库

<%@taglib uri="要使用的库的路径" prefix="前缀名" %>

       例如<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

    前缀名可以任意取

       3在代码中使用标签

<前缀名:标签名 属性名=”value”></前缀名:标签名>

两个标签之间写入需要执行的代码

属性值通常通过el表达式操作

<% request.setAttribute("score", 50); %>

<c:if test="${score < 60 }">

<h2>你个渣渣!</h2>

</c:if>

不设置jstl时代码较为混乱

//在不使用jstl情况下的if使用 不停使用<%   %>进行开闭

<%

         request.setAttribute("score", 50);

    %> 

    <%

         // 获取域对象中的值

         Integer score = (Integer)request.getAttribute("score");

         if (score < 60) {

    %>

             <h2>你个渣渣!</h2>

    <%      

         } else if (score >= 60 && score <80) {

    %>

             <h2>哎哟不错哦!</h2>

    <%      

         } else {

    %>

         <h2>你很棒棒哦!</h2>

    <%      

         }

    %>

 

 

if标签

<c:if test="判断条件,返回true或false" var="限域变量名,用户接收判断条件的返回值" scope="限域变量名的域范围 page|request|session|application">

// 当满足条件时,执行的代码

</c:if>

当num域对象的值大于1执行<h2>标签,boolean设置为request域对象的flag属性

<c:if test="${num > 1 }" var="flag" scope="request">

     <h2>Hello JSTL</h2>

</c:if>

1     判断条件一般为el表达式,结果为booelan,可以直接写boolean        

              有主体内容的形式

              <c:if test=”${  内容  }”> //执行代码 </c:if >

              无主体内容的形式 中间没有执行代码

              <c:if test=”${  内容  }”> </c:if >

2    var和scope一般不使用,一般用在调试中,

若需要获取可以通过对应的域对象获取,或直接el表达式获取${requestScope.flag }

3     if不存在else机制 可以通过条件取反实现else效果

    <c:if test="${num >1 }">

         <h2>Hello</h2>

    </c:if>

    <c:if test="${num <=1 }">

         <h2>Hi</h2>

    </c:if>

 

choose、when、otherwise标签

语法

       choose中嵌套when、otherwise标签

<c:choose>

       <c:when test="返回boolean类型的判断条件">

       // 满足条件执行的代码

       </c:when>

       ...

       <c:otherwise>

       // 当所有的when不执行时执行的代码

       </c:otherwise>

</c:choose>

1、choose标签与otherwise标签没有属性,when标签必须设置test属性

2、choose标签中一级子标签只能包含when标签与otherwise标签,

但when标签与otherwise标签可以包含其它标签

3、choose标签中至少包含一个when标签,最多只有一个otherwise标签

       4、所有的when标签必须设置在otherwise标签之前

       5、只有所有的when标签都不执行的情况下才会执行otherwise标签

       例子:

     <%request.setAttribute("num", 90); %>    

     <c:choose>

        <c:when test="${num <60 }">

             <h4>你个渣渣!</h4>

        </c:when>

        <c:when test="${num >= 60 && num < 80 }">

             <h4>哎哟不错哦!</h4>

        </c:when>

        <c:otherwise>

             <h4>你很棒棒哦!</h4>

        </c:otherwise>

     </c:choose>

 

forEach标签

      

1 循环执行代码多次

<c:forEach begin="循环起始数" end="循环结束数" var="当前成员的限域变量名" step="循环的间隔数"> //循环执行的代码</c:forEach>

var所声明变量的值也就是当前的循环数 可以作为域对象属性被调用

与java中for(int i=0;1<=100;i++){ }中的i相同

<c:forEach begin="0" end="10" var="i" step="1">

        ${i }

</c:forEach>

<%    

     request.setAttribute("num", 0);

     request.setAttribute("num2", 10);

     request.setAttribute("n", 2);   

 %>

 <c:forEach begin="${num }" end="${num2 }" var="i" step="${n }">

     ${i }

 </c:forEach>

 

2  迭代集合

list类型

<c:forEach items="${list1 }" var="item">

     ${item } <br>

</c:forEach>

items的值为所迭代的集合

var值为所迭代集合的当前所指向的值

varStatus 值为迭代相关信息

       itemp.index迭代索引         ${itemp.index}

itemp.count迭代索引次数

itemp.first 是否是第一次被循环     

itemp.last是否是最后一次被循环

map类型

<c:forEach items="${map }" var="item">

        ${item.key } -- ${item.value }

 </c:forEach>

       items的值为所迭代的map

       var值为所迭代集合的当前所指向的对象

       item.key表示获取当前对象的key值

       item.value表示获取当前对象的value值

 

格式化动作指令

属于jstl.jar下的内容

formatNumber   将数值转为字符串

无主体格式

<fmt:formatNumber value=""  type=""  var="" />

有主体格式

<fmt:formatNumber  type="" var="" >所转换的数值</fmt:formatNumber>

       注:

value表示所要转换的数字或域对象

type为转换的格式

       number数值型 (若不设置,则为默认值)

       percent百分比类型

       currency货币型

var为转换后的域对象key

若设置了var引用则不打印  若未设置var引用则打印

 

parseNumber      将字符串转为数值

<fmt: parseNumber value=""  type=""  var="" />

<fmt: parseNumber type="" var="" >所转换的数值</fmt:formatNumber>

与formatNumber反向

 

formatDate  日期对象转字符串

       <fmt:formatDate value="" var="" type="" dateStyle="" timeStyle="" pattern=""/>

value:要格式化的数据

type:要格式化成的类型  

日期型(默认)、时间型、日期+时间型

yyyy-MM-dd

dateStyle:日期型的格式

timeStyle:时间型的格式

pattern:具体的格式

      

 

 

 

可以进行区域设置

<fmt:setLocale value="en_US"/>

<fmt:setLocale value="zh_cn"/>会使得时间格式和货币格式产生不同

 

 

 

 

 

      

 

原文地址:https://www.cnblogs.com/javaxiaobu/p/11173897.html