美文网首页
jsp el jstl表示式

jsp el jstl表示式

作者: 基本密码宋 | 来源:发表于2017-06-04 16:27 被阅读14次

    简单粗暴上代码

    <title>jsp el 表示式</title>
    </head>
    <body>
        
            <%
                request.setAttribute("name","aaa");
             %>
             <!--  相当于 String str=(String)pageContext.findAttribute("name"); -->
             
             ${name}
             
             <!-- jsp中el表示式可以获取bean的属性 -->
             <%
                Person p=new Person();
                p.setAge(26);
                request.setAttribute("person", p);
              %>
              ${person.age}
              
              <!-- jsp中el表达式获取bean中的bean  -->
             <% 
               Person p1=new Person();
               Address address=new Address();
               p1.setAddress(address);
               request.setAttribute("personBean", p1);
              %>
              
              ${personBean.address.address}
                
                <!-- jsp中用el表达式 来获取 集合 -->
              <%
                List<Person> list=new ArrayList();
                Person pList1=new Person();
                pList1.setName("基本密码");
                Person pList2=new Person();
                list.add(pList1);
                list.add(pList2);
                request.setAttribute("list", list);
               %>
               ${list[0].getName()}
               
               <!-- jsp中用el表达式 来获取 map -->
               <%
               HashMap map=new HashMap();
               map.put("aaa", "111");
               map.put("bbb", "222"); 
               map.put("ccc", "333"); 
               map.put("ddd", "444");
               
               map.put("111", "aaa");
               request.setAttribute("map", map);
               %>
               
               ${map.aaa}
               ${map["111"]}  <!-- 当 map中的 key 是 数字的时候   用这种形式-->
               
               <!-- ${pageContext.request.contextPath} 得到当前的 项目名字 这里是   /JspDemo -->
               <a href="${pageContext.request.contextPath}/1.jsp">点击</a>
    </body>
    

    jstl 表达式

    <%
        List<Person> list=new ArrayList();
        Person pList1=new Person();
        pList1.setName("基本密码");
        Person pList2=new Person();
        pList2.setName("街霸密碼");
        list.add(pList1);
        list.add(pList2);
        request.setAttribute("list", list);
     %>
        <c:forEach items="${list}" var="person">
             ${person.name}<br/>
        </c:forEach>
    
    
    <%
               HashMap map=new HashMap();
               map.put("aaa", "111");
               map.put("bbb", "222"); 
               map.put("ccc", "333"); 
               map.put("ddd", "444");
               
               map.put("111", "aaa");
               request.setAttribute("map", map);
               
              
                
        %>
        <!--  Set<Map.Entry> set =map.entrySet(); -->
        <c:forEach items="${map}" var="mapItem">
             ${mapItem}<br/>
             ${mapItem.key} = ${mapItem.value }
        </c:forEach>
        
    

    相关文章

      网友评论

          本文标题:jsp el jstl表示式

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