zoukankan      html  css  js  c++  java
  • jsp之el表达式jstl标签

    不管是el表达式还是jstl标签最终的目的都是要消除jsp中的java代码,当然是消除显式的java代码

    el表达式的出现是为了简化jsp中读取数据并写入页面的操作.

    el表达式的功能不多,也很好记

    读取域对象中的值,并写入到页面.

     

    <%
        pageContext.setAttribute("pk", "1");
        request.setAttribute("rk", "2");
        session.setAttribute("sk", "3");
        application.setAttribute("ak", "4");
    %>
    
    ${pageScope.pk}
    ${requestScope.rk}
    ${sessionScope.sk}
    ${applicationScope.ak}

    我们使用 作用域.key的方式可以直接获取域对象中的值

    我们也可以省略作用域

    <%
        pageContext.setAttribute("key", "1");
        request.setAttribute("key", "2");
        session.setAttribute("key", "3");
        application.setAttribute("key", "4");
    %>
    
    ${key}

    注意了,如果我们省略作用域,那么表达式会按照 pageScope-->requestScope-->sessionScope-->applicationScope的顺序去查询匹配的key值.如果有一个域中查询到.则不会在查询其他域.

    如果获取不到不会抛出异常

    下面我们使用el表达式来操作object类型的数据

    public class User {
        private String name;
        private int age;
    
        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    <%
        request.setAttribute("user", new User("张三", 22));
    %>
    ${requestScope.user.name}
    ${requestScope.user.age}

    注意啦!如果读取对象的属性.那么该对象必须实现属性相对应get()方法.否则抛异常!!!

    el表达式也支持数据的运算

    <%--数学运算--%>
    ${(1+2*3+1)/2%3}
    <%--关系运算--%>
    ${1>4}
    <%-->  >=  <  <=  ==  !=
    gt ge  lt le  eq  !=--%>
    //注意啦!我们一般不会用符号运算,而是用字符代替.防止跟html标签冲突
    <%--逻辑运算--%>
    ${true||false}
    <%--三元表达式--%>
    ${3>1?"大于":"小于"}

    el表达式的其他使用

    读取请求参数

    <%--获取请求参数中的值--%>
    ${param.age}

    如果参数是数组类型

    <%--获取值是列表的参数--%>
    ${paramValues.age[0]}
    ${paramValues.age[1]}

     jstl(核心类库)

    需要引入两个jar

    jstl.jar   standard.jar

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

    导入标签约束指定标签库命名空间

    首先是设置域属性

    <c:set scope="session" var="cskey" value="c标签设置session"/>//设置attribute
    <c:remove scope="session" var="cskey"/>//删除attribute

    scope是域范围:page,request,session,application. var是属性名.value则是属性值

    if语句,注意 test中只能用el表达式

    <c:if test="${3>2}">
        3>2
    </c:if>

    if else 语句

    <c:choose>
        <c:when test="${crage==1}">
            when1
        </c:when>
        <c:when test="${crage==2}">
            when2
        </c:when>
        <c:otherwise>
            when3
        </c:otherwise>
    </c:choose>

    循环语句

    <c:forEach var="i" begin="1" end="5" step="1">
        当前循环:${i}
    </c:forEach>

    var 当前变量名,begin开始位置,end结束位置,注意这里的end="5"其实是 <=5 以上的会输出5次.另外值得一说的是,在var中声明的变量会存到pageContext域中.所以el表达式才可以取值

    循环操作 集合

    <%
        List<User> users = new ArrayList<User>();
        users.add(new User("张三",1));
        users.add(new User("李四",2));
        users.add(new User("王五",3));
        request.setAttribute("users",users);
    %>
    <c:forEach  items="${users}" var="user">
        循环用户:${user.name}${user.age}
    </c:forEach>

    循环操作 map

    <%
        Map<Integer,User> maps = new HashMap<>();
        maps.put(1,new User("张三",1));
        maps.put(2,new User("李四",2));
        maps.put(3,new User("王五",3));
        session.setAttribute("maps",maps);
    %>

    这个略微有些说法,var 中存储的其实是一个键值对,在el中 .key就是取map的key  .value就是取map的value

    <c:forEach items="${sessionScope.maps}" var="entry">
        循环用户Map:${entry.key}${entry.value.name}${entry.value.age}
    </c:forEach>

    以上就是el表达式和jstl标签的常用操作.

  • 相关阅读:
    Python学习-第三天代码作业
    网络原理-1
    整理操作系统
    操作系统--开发语言--重点
    操作系统
    C#数组的声明方式
    string.Format对C#字符串格式化(转)
    C#中两个Form窗口之间的传值(父->子)(子->父) (转)
    opencv MatchTemplate()模板匹配寻找最匹配部分
    摄像头,图片筛选颜色
  • 原文地址:https://www.cnblogs.com/zumengjie/p/11342597.html
Copyright © 2011-2022 走看看