EL表达式与JSTL

 

EL(Express Language)表达式

EL 隐含对象包含以下四个:pageScope、requestScope、sessionScope 和applicationScope

输入有关的隐含对象有两个:param和paramValues,

其他隐含对象:(1)cookie(2)header和headerValues(3)initParam(4)pageContext

1、算符运算 ,逻辑运算  

$('a'<'b'?1:2)   //输出1

2、获取表单数据,URL重写内容

//获取单个数据

${param.usernmae}

//获取数组对象

${paramValues.langue}

//取出数组指定位置的值

${paramValues.langue[0]

3、对象操作(不常用)

//单对象

<body>

     <%

         Person p = new Person();

          p.setNmae("xiaogou"); 

          request.setAttribute("person",p);

     %>

</body>



${person.name}

//输出xioagou

//组合对象

<jsp:useBean id="group" calss="xx" scope="page" ></jsp:userBean>

<jsp:setProperty property="gname" name="group" value="火箭队">

<jsp:userBean id="myUser" calss="yy">

<jsp:setProperty property="group" value="${group}" name="myUser"/>



${myUser.group.gname}

//输出火箭队

4、集合获取

<body>

     <%

          Map map = new HashMap();

          map.put("human1",new Person("gou"));

          map.put("human2"new Person("cut"));

          request.setAttribute("aaaaaaaa",map)

     %>

</body>



${aaaaaa.human1.name}

//输出gou

5、获取当前工程名称

<a href="${pageContext.request.contextPath}/login.html">跳转登录页面</a

JSTL(JSP  Standrad Tag Lib)

标准标签库

1、JSTL介绍

     JSTL可以实现页面中的简单逻辑,从而替换页面中的脚本代码。实现流程①、导入jstl.jar和standerd.jar两个JSTl的jar文件;②、在JSP页面使用<%@taglib uri=""  prefix=""%>元素导入标签库;③、JSTL标签库常用标签<c:foreach var="" items=""> <c:if tets="">

c     核心标签(core)

fmt     格式化标签(日期,格式化)

x     xml标签

fn     函数标签

<%--导入标签库,在JSTL包->META-INF->c.tld->uri标签->http:java.sun.com/jsp/jstl/core--%>

<%--prefix任意,前缀--%>

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

 

2、核心标签库    

功能分类

标签名称

表达式操作

out、set、remove、catch

流程控制

if、choose、when、otherwise

迭代操作

forEach、forTokens

URL操作

import、param、url、redirect

    c:set,向指定范围存储数据,为对象赋值,c:out输出

//向存储器赋值

<c:set var="msg" value="这是一条数据" scope="session"></c:set>

<c:out value="${msg}"></c:out>//可以很好防止XSS

//对象赋值

<c:set property="name" target="${user} value="张三"></c:set>//给user对象的name赋值为张三

<c:out values="${user.name}"></c:out>

    c:if

<c:if test="${user!=null"}>

     如果为true,就会执行这里的语句     

</c:test>

    c:chose,c:oherwise

<c:choose>

    <c:when test="${user.name=="admin"}"

        管理员

    </c:when>

    <c:otherwise>

        普通用户

    </c:otherwise>

</c:choose>

    c:forEach

<body>

     <%

          List list = new ArrayList();

          list.add(new Person("aa"));

          list.add(new Person("bb"));

          request.setAttribute("list",list);          

     %>



     <c:forEach var = "person" items="${list}">

         ${person.name}<br> 

     </c:forEach>

</body>



 //=========

<c:forEach begin="1" end="10" var="i" step="2" varStatus="stat">

        ${i}---->${stat.index}---->${stat.count}---->${stat.first}

        <br>

</c:froEach>

//结果:

结果-->索引-->位置-->是否为第一位数字

1--->1---->1---->true

3--->3---->2---->false

5--->5----->3--->false

7--->7----->4---->false

9--->9----->5---->false

    c:forTokens

<%

String aa="张三#李四#王五";

request.setAttribute("names",aa);

%>

<c:forTokens items="${names}" delims="#" var="n">

${n}<br>

</c:forTokens>

//输出

张三

李四

王五

    c:import    类似于动态包含

c:import uer="b.jsp"></c:import>

    c:url    c:param    c:redirect  

<c:url var="tagetPath" value="main.jsp" scope="page">

    <c:param name="name" value="admin">adsdasda</c:param>

    <c:param name="password" value="123456">sssssssss</c:param>

</c:url>

<c:redirect url="${tagetPath}"></c:redirect>





<%--相当于main.jsp?name=admin?password=123456--%>

${param.name}----->${param.password}

3、formt标签 format...与pase..

<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

    时间

<%

Date now = new Date();

request.setAttribute("now",now);

%>



<fmt:formatDate value="${now}" var="this_time" scope="page" pattern="yyyy/MM/dd HH:mm:SS"></fmt:formatDate>



${this_time};

    货币,百分比默认整数位

数字处理类型,percent百分比,currency货币

<fmt:setLocale value="en-US"></fmt:setLocale>

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

    百分比

<fmt:formatNumber value="" pattern="##.##%"></fmt>

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值