zoukankan      html  css  js  c++  java
  • JSP表达语言EL

    SP编写无脚本的JSP页面:
    EL:表达式语言:Expression Language

    EL语言只要目标是从JSP页面中消除Java代码。让前端做前端的事,后台专注后台
    EL只用于展示,比如if那些不能用,定义变量无法

    要定义变量:用个JavaBean的属性,JSP提供的隐式对象,自动以标记库和标准标记库


    代码示例:
    JSP:<%= user.getUserName() %>不能对象.对象.属性
    EL:$<user.userName>可以对象.对象.属性,展示比较简单
    $<requestScope.user.userName>
    放在request对象里边的,requestScope可以省略


    隐式对象:
    requestScope对应req
    applicationScope对应ServletContext

    EL表达式在操作对象是,方式:切记:对象.对象的属性名。不用get方法之类的

    得到Request的值
    $<user.userName>

    获取动态的环境值
    applicationScope可以访问JSP的隐式对象application,Request,Response,Session,Out等


    获取servletContext初始化环境值
    ${initParam.money}

    获取session的值
    req.getSession().setAttribute("user2", "u14");
    ${sessionScope.user.userName}

    获取集合的值:Map,list,set集合
    List<UserBean> users = new ArrayList<UserBean>();
    users.add(user);
    req.setAttribute("userList",users);

    Map<String, UserBean> maps = new HashMap<String ,UserBean>();
    maps.put("a1",user);
    req.setAttribute("userMap",maps);

    ${requestScope.user[0].userName}
    ${requestScope.userMap['a1'].userName}

    JSP自定义标记库和标准标记库JSTL
    在需要的界面导进去,一般这两个包:jstl.jar standard.jar

    <c:catch var="exception"/>
    <% int x = 3/0; %>
    <c:set var="unm" value="S{4*2}"/>
    <c:out value="${num}">
    </c:catch>

    <c:if test="${exception != null}">
    <p>这是一个异常:${excption}</p>
    </c:if>

    set
    <c:set var="unm" value="S{4*2}"/>
    <c:set var="unm" value="8"/>
    或者
    <c:set var="num">
    ${4*2}
    </c:set>

    我们可以操作后台返回的对象,对对象进行设值
    <c:set gerget="${user }" property="age" value="38"/>
    <c:out value="$user.age"/>

    通过remove标记,删除某一个指定作用域内的属性
    <c:remove var="userName" scope="request">

    流程控制:
    choose when相当于swith case
    <c:choose>
    <c:when test="${num < 5}">
    不约
    <c:when>

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

    <c:when>

    <c:otherwise>
    gun
    </c:otherwise>

    </c:choose>

    forEach:循环,就是Java的foreach
    <c:forEach items="${user }" var="u1">
    ${u1.userName},
    ${u1.password},.
    ${u1.age},
    </c:forEach>

    可以放在表格里边
    <table border=1>
    <tr>
    <th>姓名</th>
    <th>年龄</th>
    <th></th>
    <th></th>
    <th></th>
    </tr>

    <c:forEach items="${user }" var="u1">
    <tr>
    <td>${u1.userName}</td>
    <td>${u1.password}</td>
    <td>${u1.age}</td>
    <td></td>
    <td></td>
    </tr>
    </c:forEach>


    <table>

  • 相关阅读:
    use paramiko to connect remote server and execute command
    protect golang source code
    adjust jedi vim to python2 and python3
    install vim plugin local file offline
    add swap file if you only have 1G RAM
    datatables hyperlink in td
    django rest framework custom json format
    【JAVA基础】网络编程
    【JAVA基础】多线程
    【JAVA基础】String类的概述和使用
  • 原文地址:https://www.cnblogs.com/wanglei718/p/5495091.html
Copyright © 2011-2022 走看看