美文网首页
集合操作

集合操作

作者: kylelin | 来源:发表于2014-08-08 08:04 被阅读94次

    集合操作在开发中被广泛地应用,在表达式语言中也已经很好地支持了集合的操作,可以方便地使用表达式语言输出Collection(子接口:List、Set)、Map集合中的内容。

    【输出Collection接口集合】

    <%@page import="java.util.ArrayList"%>
    <%@page import="java.util.List"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>标题</title>
    </head>
    <body>
        
        <%
            List<String> all = new ArrayList<String>();
            all.add("百度");
            all.add("http://www.baidu.com");
            all.add("244098979@qq.com");
            request.setAttribute("info", all);
        %>
        <h1>${requestScope.info[0] }</h1>
        <h1>${requestScope.info[1] }</h1>
        <h1>${requestScope.info[2] }</h1>
        
    </body>
    </html>
    

    由于表达式语言只能访问保存在属性范围中的内容,所以此处将集合保存在了request范围中。

    【输出Map集合】

    <%@page import="java.util.HashMap"%>
    <%@page import="java.util.Map"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>标题</title>
    </head>
    <body>
        <%
            Map<String, String> map = new HashMap<String, String>();
            map.put("baidu", "http://www.baidu.com");
            map.put("google", "http://www.google.com");
            map.put("bing", "http://www.bing.com");
            request.setAttribute("info", map);
        %>
        <h2>${info["baidu"] }</h2>
        <h2>${info["google"] }</h2>
        <h2>${info.bing }</h2>
    </body>
    </html>
    

    在表达式语言中,除了可以采用"."的形式访问,也可以采用"[]"的形式访问。

    相关文章

      网友评论

          本文标题:集合操作

          本文链接:https://www.haomeiwen.com/subject/trretttx.html