Java Web 第10天 EL和JSTL 2

四、JSTL的用法

在平常的使用中我们一般利用 EL 控制变量以及变量的输出,利用 JSTL 控制算术逻辑。 JSTL 是一种标记。

1.       JSTL 的导入

                         i.              手动导入架包,填写真实的 uri=http://java.sun.com/products/jsp/jst1

                       ii.              利用 Myeclipse 导入架包,填写的 uri 也变成相对路径

2.       JSTL 中的流程控制

a)       条件处理

例如:(就算是 String 类型的也是利用 = = 判断是否相同)

<c:if test=”${x==9}”>

${x}

</c:if>

例如:

<c:choose>

<c:when text=” ${x==9}”>

This is when term

</c:when>

<c:otherwise>

This is otherwise term

</c:otherwise>

</c:choose>

b)      循环控制

普通循环:

       <c:forEach var=”x” begin=” 1” end=”9”>

              ${x}

</c:forEach>

数组等遍历:

       <h3> 遍历数组 </h3>

       <%int[] sss = {22,34,21};

           pageContext.setAttribute("ss", sss, PageContext.PAGE_SCOPE);

          for(int s:sss){

            out.print(s);

          }

         %>

       <fw:forEach items="${ss}" var="s">

          ${s }&nbsp;

       </fw:forEach>

      

       <h3> 遍历简单类型的集合 </h3>

       <%List<String> list = new ArrayList<String>();

              list.add("fw"); list.add("jr");

           pageContext.setAttribute("ll", list, PageContext.PAGE_SCOPE);

          for(String l:list){

            out.print(l);

          }

         %>

       <fw:forEach items="${ll}" var="l">

         ${l }&nbsp;

       </fw:forEach>

      

       <h3> 遍历复杂类型的集合 </h3>

       <%List<User> userList = new ArrayList<User>();

              userList.add(new User(1,"mm"));

           userList.add(new User(2,"mjj"));

           pageContext.setAttribute("ul", userList, PageContext.PAGE_SCOPE);

           for(User u:userList){

             out.print(u.getId()+"&nbsp;"+u.getName());

           }

         %>

         <fw:forEach items="${ul}" var="u">

            id:${u.id } &nbsp; name:${u.name }

         </fw:forEach>

        

         <h3>MAP 的遍历 </h3>

         <%

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

           map.put(1,"jjj");

           map.put(2,"rrr");

           pageContext.setAttribute("map", map, PageContext.PAGE_SCOPE);

          %>

          <fw:forEach items="${map}" var="m">

            :${m.key } &nbsp; :${m.value }<br>

          </fw:forEach>

          <jr:formatNumber maxFractionDigits="2">

            45.7891234

          </jr:formatNumber>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值