EL+JSTL入门

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="com.lan.domain.Person"%> 
<%
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>EL+JSTL</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 }
    <%--pageContext.findAttribute("data") page request session application--%>
    <br/>
    <%
    	Person p =  new Person("");
        	p.setName("aaaa");
        	request.setAttribute("person", p);
    %>
   ${person.name }
   <br/>
   
   <%
   		List list = new ArrayList();
   		list.add(new Person("aaa"));
   	 	list.add(new Person("bbb"));
   	 	list.add(new Person("ccc"));
   	 	
   	 	request.setAttribute("list",list );
    %>
   ${list[1].name }
   
   
   <br/>
   <%
   		Map map = new LinkedHashMap();
   		map.put("h1",new Person("hash1"));
   		map.put("h2",new Person("hash2"));
   		map.put("h3",new Person("hash3"));
   		map.put("h4",new Person("hash4"));
   		map.put("111",new Person("11111"));
   		request.setAttribute("map", map);
    %>
	${map.h1.name }   
	${map['111'].name } <%--用el表达式在取数据时,通常用.号 , .号取不出来时用['']--%>
	
	${pageContext.request.contextPath }<%--获取当前web应用的名称 --%>
	
	<a href="${pageContext.request.contextPath }/index.jsp">首页</a>
	
	<br/>
	-------------------------------------------------------------------jstl<br/>
    <c:forEach var="persons" items="${list }">
    	${persons.name }<br/>
    </c:forEach>
	
	<c:forEach var="entry" items="${map }">
    	${entry.key }:${ entry.value.name}<br/>
    </c:forEach>
	
	<c:if test="${user!=null }">
		欢迎您:${user.name }
	</c:if>
	<c:if test="${user==null }">
		用户名:<input type="text">
		密码:<input type="text">
	</c:if>
  </body>
</html>

你可能感兴趣的:(EL+JSTL入门)