当前位置: 移动技术网 > IT编程>开发语言>Java > EL_JSTL

EL_JSTL

2019年05月15日  | 移动技术网IT编程  | 我要评论

赖上霸王龙,诛仙青云志迅雷下载,奔跑吧兄弟花絮

el(expression language)el表达式
作用:在jsp中消灭java代码
语法: "${ }"
用法:参考jsp页面
1 //servlet中,:
2      user user = new user(1,"admin","admin","hahahaha");
3          httpsession session =request.getsession();
4          session.setattribute("user",user);
5          response.sendredirect("index.jsp");

 

//index.jsp中,加判断,避免空指针异常
    user user =(user)session.getattribute("user");
          if(user!=null){
              out.write(,欢迎"+user.getrealname()+"光临本站");
          }else{
              out.write(",欢迎光临本站!");    
          }

 

<%-- 可能会报500异常:
 ,欢迎<%= ((user)session.getattribute("user")).getrealname() %>光临本站! --%>

<%-- 更为简单,稳妥的方式! --%>
欢迎${user.realname}光临本站!

 

<body>
    <%-- 
加判断,避免空指针异常
<%
    user user = (user)session.getattribute("user");
    if(user != null){
        out.write("欢迎"+user.getrealname()+"光临本站!");
    }else{
        out.write("欢迎光临本站!");
    }
%> --%>

    <%-- 可能会报500异常:
 欢迎<%= ((user)session.getattribute("user")).getrealname() %>光临本站! --%>

    <%-- 更为简单,稳妥的方式! --%>
    <%
        int a = 10;

        //数据在四大作用域对象中
        pagecontext.setattribute("a", a);
        request.setattribute("aaa", true);
        session.setattribute("age", 18);
        application.setattribute("name", "laowang");

        // 简单对象
        user user = new user(1, "admin", "admin", "老王");
        request.setattribute("uuu", user);
        // 复合对象
        user u2 = new user(2, "admin-laowang", "123123", "老王", new address(
                "广东省", "广州市", "天河区"));
        pagecontext.setattribute("u2", u2);
        //list中的数据
        list list = new arraylist();
        list.add("apple");
        list.add("orange");
        list.add("watermelon");
        list.add("pineapple");
        pagecontext.setattribute("fruits", list);
        //map中的数据
        map map = new hashmap();
        map.put("name", "zhangsan");
        map.put("age", 18);
        map.put("gender", "女");
        map.put("address", new address("广东省", "广州市", "天河区"));
        pagecontext.setattribute("person1", map);

        //作用域中有相同的key
        pagecontext.setattribute("xxx", "pagecontext");
        request.setattribute("xxx", "request");
        session.setattribute("xxx", "session");
        application.setattribute("xxx", "application");
        
        user uu1 = new user(5,"admin","admin","张三");
        user uu2 = new user(5,"admin","admin","张三");
        
        request.setattribute("uu1", uu1);
        request.setattribute("uu2", uu2);
        
        request.setattribute("score", 99);
        
        pagecontext.setattribute("a1", null);
        pagecontext.setattribute("a2", new arraylist());
        pagecontext.setattribute("a3", new hashmap());
        pagecontext.setattribute("a4", "");
    %>

    欢迎${user.realname}光临本站!
 <h3>el表达式从哪里取值?一般情况下,从作用域对象中取值</h3>
    ${a} --- ${aaa} --- ${age} --- ${name}
    
 <h3>el表达式可以取什么类型的值?基本数据类型、引用数据类型(对象、复合对象、list、map)</h3>
    ${uuu.realname} --- ${u2.addr.town} --- ${fruits[2]} ---
    ${person1.address.city}
    
 <h3>el表达式从哪个作用域中取值?默认从最小作用域中开始寻找,找不到往大的作用域中找,都找不到显示为空</h3>
    ${xxx}
    
 <h3>el从指定作用域中取值?
        pagescope、requestscope、sessionscope、applicationscope</h3>
    ${pagescope.xxx }--- ${requestscope.xxx} --- ${sessionscope.xxx } ---
    ${applicationscope.xxx }
    
 <h3>el表达式还可以取请求参数中的值:param、paramvalues</h3>
    ${param.username} --- ${paramvalues.fav[2] }
    
 <h3>el表达式除了“.”操作符之外,还有“[ ]”操作符:“[]”可以解析子el表达式</h3>
    ${uuu["realname"]} --- ${param.type} --- ${uuu[param.type]}
    
 <h3>el表达式中的为空判断:empty判断为空的有:空字符串、null、空集合、空map</h3>
    ${empty a1} --- ${empty a2} --- ${empty a3} --- ${empty a4}
    
 <h3>el表达式可以进行一些基本的数学运算</h3>
    ${5+4} --- ${5-4} --- ${5*4} --- ${5/4} --- ${5%4} --- ${5+"4"}
    
 <h3>el表达式可以进行一些逻辑判断</h3>
    ${5>4} --- ${ 5 == 4} --- ${ 5 eq 4 } --- ${uu1 == uu2} --- ${uu1 eq uu2} --- ${score == 99 }    
</body>

 

 
jstl(jsp standard tag library) jsp标准标签库
 
作用:在jsp中消灭java代码
 
使用:1.下载jar包,并导入
2.使用<%@ taglib %>引入标签库,设置uri和prefix的属性值(属性的值可以在jstl-impl-1.2.2.jar/meta-inf/c.tld找到)
3.使用标签。
 
标签用法:参考jsp页面
 
常用的子标签库:
– 核心标签库:http://java.sun.com/jsp/jstl/core 包含 web 应用的常 见工作,比如:循环、表达式赋值、基本输入输出等。
– 格式化标签库:http://java.sun.com/jsp/jstl/fmt 用来格式化显示数 据的工作,比如:对不同区域的日期格式化等。
– xml 标签库:http://java.sun.com/jsp/jstl/xml 用来访问 xml 文件 的工作,这是 jstl 标签库的一个特点。
– 函数标签库:http://java.sun.com/jsp/jstl/functions 用来读取已经定 义的某个函数。
– 数据库标签库:http://java.sun.com/jsp/jstl/sql 可以做访问数据库的 工作
 
 
多用途核心标签:
– <c:out>用于在 jsp 中显示数据
• <c:out value="${anyvalue}" default="value" escapexml="false"/>
– <c:set>将值存储到作用域或为 javabean 中的变量属 性赋值
• <c:set var=“name“ value=“zhangsan“ scope=“page”/> – 将值zhangsan存储到变量name, name为作用域pagecontext 中的一个属性。
– <c:remove>删除存在于 scope 中的变量
• <c:remove var="samplevalue" scope="session"/>
 
 
条件控制标签: – <c:if>
名称
描述
类型
默认值
test
若返回true,则执行本体内 容;否则,不执行。
boolean
var
用来存储test执行后的结果
string
scope
var 指定变量的存储范围
string
page
 
– <c:choose> <c:when> <c:otherwise>
– 使用要点:
• <c:when>必须在<c:otherwise>之前
• <c:choose>中除了<c:when>/<c:otherwise>不能有其他 元素。
 
• 循环控制标签:
– <c:foreach>
 
名称
说明
必须
默认值
items
循环体
no
none
begin
开始条件
no
0
end
结束条件
no
last element
steo
步长
no
1
var
当前循环的变量名
no
none
varstatus
循环状态
no
none
 
url 相关标签:
– <c:import>包含另一个 jsp 页面到本页面
• 作用类似<jsp:include>,但是比<jsp:include>作用范围广, 不仅可以包含本项目下资源,也可以包含外部的资源!
 
– <c:redirect>用于页面重定向
• <c:redirect url=“fortoken.jsp“ />
 
• 格式化日期:
– <fmt:formatdate value=“${ date }” pattern=“yyyy年mm月dd日”/>
 
获取字符串长度:
– ${fn:length(‘abc’)} ---3
– ${fn:length( abc )} ---去scope中找,找不到为0
 
<body>
    <%
        request.setattribute("weather", "今天天气很热!");

        pagecontext.setattribute("aaa", "aaa");
        request.setattribute("aaa", "bbb");
        session.setattribute("aaa", "ccc");
        application.setattribute("aaa", "ddd");

        request.setattribute("score", 78);
        
        
        list list = new arraylist();
        user u1 = new user(1, "admin", "admin", "老王");
        user u2 = new user(2, "cai10", "admin", "蔡依林");
        user u3 = new user(3, "cai20", "admin", "蔡依林");
        user u4 = new user(4, "cai30", "admin", "蔡依林");
        user u5 = new user(5, "cai40", "admin", "蔡依林");
        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);
        list.add(u5);
        request.setattribute("persons", list);
        
        
        date date = new date();
        request.setattribute("d1", date);
        
        string datestr = "1999/12/12 03:04:05";
        request.setattribute("ds", datestr);
        
        string str2 = "18810950652";
        request.setattribute("phone", str2);
        
        
    %>
    <h3>c:out标签:用于将作用域中的对象输出,default属性会在value属性为空时显示</h3>
    <c:out value="${weather}" default="汗流浃背!"></c:out>

    <h3>c:set标签:用于将一个数据保存在作用域中(默认最小作用中),通过scope设定存放的作用域</h3>
    <c:set var="name" value="laowang" scope="request"></c:set>
    ${requestscope.name}

    <c:set var="table">
        <table border="1" style="border-collapse: collapse;">
            <tr>
                <td>1*1</td>
                <td>1*2</td>
            </tr>
            <tr>
                <td>2*1</td>
                <td>2*2</td>
            </tr>
        </table>
    </c:set>
    ${table}-${table}-${table}

    <h3>c:remove标签:将作用域中的指定key的数据删除(默认删除所有作用域中的指定key的数据)</h3>
    <c:remove var="aaa" scope="page" />
    ${aaa}

    <h3>c:if标签:条件判断(单条件)</h3>
    <c:if test="${score >= 80}" var="flag">
        优秀!
    </c:if>
    <c:if test="${!flag}">
        良好!
    </c:if>
    
    <h3>c:choose 、 c:when、 c:otherwise标签:条件判断(多条件)</h3>
    <h4>注意以下几点:1.c:when必须在c:otherwise之前出现</h4>
    <h4>2.c:otherwise只能出现一次</h4>
    <h4>3.c:when和c:when和c:otherwise之前不允许出现其他任何字符</h4>
    <c:choose>
        <c:when test="${score >=80 }">
            优秀!
        </c:when>
        <c:when test="${score >=70 }">
            良好
        </c:when>
        <c:when test="${score >=60 }">
            及格
        </c:when>
        <c:otherwise>
            不及格        
        </c:otherwise>
    </c:choose>
    
    <h3>c:foreach标签:循环</h3>
    <h4>items:循环体,通过使用el表达式获取循环的内容</h4>
    <h4>begin & end:只能为数字,当没有items属性时,从begin数字循环到end数字;当有items属性时,begin & end就表示下标</h4>
    <h4>step:步长</h4>
    <h4>var:表示每次的循环体</h4>
    <h4>varstatus:当前循环体的状态:有几个属性--first(判断当前循环体是否为第一个)、last(判断当前循环体是否为最后一个)、count(当前循环次数)、index(当前循环体的下标)</h4>
    <c:foreach begin="3" end="9" var="each">
        ${each}
    </c:foreach>
    <br>
    <c:foreach items="4123,2512,222,111,555,677,999" begin="1" end="3" var="each">
        ${each}
    </c:foreach>
    <br>
    <c:foreach items="${persons}" step="2" var="person">
        ${person.username}<br>
    </c:foreach>
    <br>
    <c:foreach items="${persons}" var="person" step="2" varstatus="status">
        ${person.username} --- ${status.first} --- ${status.last} --- ${status.count} --- ${status.index}<br>
    </c:foreach>
    
    <br>
    <table border="1" style="border-collapse: collapse;">
        <tr><td>id</td><td>用户名</td><td>真实姓名</td></tr>
        <c:foreach items="${persons}" var="person">
            <tr><td>${person.id}</td><td>${person.username }</td><td>${person.realname }</td></tr>
        </c:foreach>
    </table>
    
    <h3>c:redirect标签:重定向</h3>
    <%-- <c:redirect url="http://www.baidu.com"></c:redirect> --%>
    
    <h3>c:import标签:指定一个url的页面在本页面中</h3>
    <%--<c:import url="http://www.baidu.com"></c:import> --%>
    
    <fmt:formatdate value="${d1}" pattern="yyyy年mm月dd日 hh时mm分ss秒"/>
    <fmt:parsedate value="${ds}" var="d2" pattern="yyyy/mm/dd hh:mm:ss"></fmt:parsedate>
    
    //计算长度
    ${fn:length(phone) }
</body>

 

 
 
 
 
 
 
 
 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网