利用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>

 

原文地址:https://www.cnblogs.com/E-star/p/3521505.html