javaWeb el表达式和jstl快速入门案例

<%@page import="de.bvb.domain.Person"%>
<%@page import="de.bvb.domain.Address"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jstl 和 el表达式</title>
</head>
<body>

    输出应用名 ${pageContext.request.contextPath }

    <br />输出八种基本数据类型:
    <%
        String data = "data";
        request.setAttribute("data", data);
    %>
    ${data }

    <br />输出一个对象的字段:
    <%
        Person person = new Person("腾讯");
        person.setAddress(new Address("深圳"));
        request.setAttribute("person", person);
    %>
    ${person.name } ${person['name'] } ${person.address.city }

    <br />输出list中的一个数据(只能通过下标获取):
    <%
        List<Person> list = new ArrayList<Person>();
        list.add(new Person("aaa"));
        list.add(new Person("bbb"));
        list.add(new Person("ccc"));
        request.setAttribute("list", list);
    %>
    ${list[0].name }

    <br />输出map中的一个数据:
    <%
        Map<String, Person> map = new HashMap<String, Person>();
        map.put("aa", new Person("aaa"));
        map.put("bb", new Person("bbb"));
        map.put("cc", new Person("ccc"));
        request.setAttribute("map", map);
    %>
    ${map.aa.name } ${map['bb'].name }

    <br />
    <br />遍历 list :
    <c:forEach items="${list }" var="p">
        <br />${p.name }
    </c:forEach>

    <br />
    <br /> 遍历 map :
    <c:forEach items="${map }" var="m">
        <br />${m.key } ==> ${m.value.name } 
    </c:forEach>
</body>
</html>

 

posted @ 2016-10-17 13:12  MarcoReus  阅读(312)  评论(0编辑  收藏  举报