• 利用EL表达式+JSTL在客户端取得数据 示例


    <%@page import="cn.gbx.domain.Address"%>
    <%@page import="cn.gbx.domain.User"%>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    	<!--
    	<link rel="stylesheet" type="text/css" href="styles.css">
    	-->
      </head>
      
      <body>
          <%--  取普通数据 --%>
          <%
         	String data = "abcd";
         	request.setAttribute("data", data);
          %>
          ${data}  <br/>   <%-- pageCopntext.findAttribute(data)  page request sesson pageContext --%>
          <hr>
          
          <%-- 取JavaBaen中的属性 --%>
          <%
          	User user = new User("aa","123");
          	// user = null;
          	request.setAttribute("user",user);
           %>
           <c:if test="${!empty(user)}">
           	${user.username}
           </c:if>
           
           <hr>
           
            <%-- 取JavaBaen中的属性 --%>
          <%
          	User user1 = new User("aa","123");
          	Address a =  new Address();
          	a.setName("山东");
          	user1.setAddress(a);
          	request.setAttribute("user1",user1);
          	
           %>
           ${user1.address.name}  <%-- 一路.到底 --%>
           <hr>
           
           <%-- list集合的取值, 以及结合JSTL的遍历 --%>
           
           <%
           	List<User> users = new ArrayList<User>();
           	users.add(new User("aa","12"));
           	users.add(new User("bb","23"));
           	users.add(new User("cc","45"));
           	request.setAttribute("users", users);
           %>
           ${ users[0].username }  <br/>
           
           
           <c:forEach var="u" items="${users}">  <%-- 大哥一定要记住 items 里面加$ --%>
           		${u.username} 
           		${u.password}
           		<br/>
           </c:forEach>
           
           <hr>
           
           <%-- 遍历map集合 --%>
           <%
           		Map<String, User> map = new HashMap<String, User>();
           		map.put("a", new User("aa","a1"));		
           		map.put("b", new User("bb","b1"));		
           		map.put("c", new User("cc","c1"));
           		request.setAttribute("map", map);		
           %>
           ${ map.a.username}  ${ map['b'].username }  <br/>
           <c:forEach var="entry" items="${ map }">
           	 ${entry.key } 
           	 ${entry.value.username }
           	 ${entry.value.password }
           	 <br/>
           </c:forEach>
           <hr>
           
           <%-- 拿到当前web应用的路径 --%>
           ${ pageContext.request.contextPath }
      </body>
    </html>
    

     

  • 相关阅读:
    推荐一款天气App 知心天气
    [推荐]Android DoraemonKit 工具
    推荐一款互动式追星神器App爱豆陪陪
    推荐一款语音直播连麦App YAMI
    推荐一款健康App 多喝水,引领全民时尚喝水生活习惯
    推荐一款万年历App 诸葛万年历
    [原创]A/B测试系统调研思维导图
    推荐一款走路赚钱App爱步行
    2条最佳实践App疯狂增长逻辑
    推荐分享AB测试服务商
  • 原文地址:https://www.cnblogs.com/E-star/p/3521505.html
Copyright © 2020-2023  润新知