EL表达式和JSTL标签快速入门

jsp代码:

<%@ page language="java" import="cn.itcast.domain.*,java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>el表达式</title>
  </head>
  
  <body>
    <!-- 数据以普通方式带来 -->
	<%
		String data = "abcd";
		
		request.setAttribute("data",data);
	 %> 
	 ${data }	<%-- pageContext.findAttribute("data") page request session application --%>
	
	<br/>
	<!-- 数据存在bean里面带来 -->
	<%
		Person p = new Person();	
		p.setName("aaaa");
		
		request.setAttribute("person",p);
	 %>
	 ${person.name }
	 
	<br/>
	<!-- 数据存在复杂bean里面带来 -->
	<%
		Person p1 = new Person();	
		Address a = new Address();
		a.setCity("上海");
		p1.setAddress(a);
		
		request.setAttribute("p1",p1);
	 %>
	 ${p1.address.city }
	 
	<br/>
	<!-- 数据存在List集合里面带来 -->
	<%
		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 map = new HashMap();
		map.put("aa",new Person("aaaaa"));
		map.put("bb",new Person("bbbbb"));
		map.put("cc",new Person("ccccc"));
		map.put("dd",new Person("ddddd"));
		map.put("111",new Person("eeeee"));
		
		request.setAttribute("map",map);
	 %>
	 ${map.bb.name }
	 ${map['111'].name }	<%--用el表达式在取数据时,通常用.号,.号取不出来时,用[]--%>
	 
	 
	 <%-- 取得当前web应用的名称: /day09 ,用于web链接不写死--%>
	 ${pageContext.request.contextPath }	
	 <a href="${pageContext.request.contextPath }/index.jsp">点点</a>
	 
  </body>
</html>



JSP代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<%@ page import="cn.itcast.domain.Person"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>使用jstl+el完成集合迭代</title>
  </head>
  
  <body>
	<%
		List list = new ArrayList();
		list.add(new Person("aaa"));
		list.add(new Person("bbb"));
		list.add(new Person("ccc"));
		
		request.setAttribute("list",list);
	 %>
    
    <c:forEach var="person" items="${list}">
        ${person.name }<br/>
    </c:forEach>
    
    
    <br/>
    <%
		Map map = new HashMap();
		map.put("aa",new Person("aaaaa"));
		map.put("bb",new Person("bbbbb"));
		map.put("cc",new Person("ccccc"));
		map.put("dd",new Person("ddddd"));
		map.put("111",new Person("eeeee"));
		
		request.setAttribute("map",map);
	 %>
	 
	 <c:forEach var="entry" items="${map}">
	 	${entry.key } : ${entry.value.name}<br/>
	 </c:forEach>
	 
	 //代表用户登录了
	 <c:if test="${user!=null}">
	 	欢迎您:${user.username }
	 </c:if>
	 
	 //代表用户没登录
	 <c:if test="${user==null">
	 	用户名:<input type="text" name="username"/>
	 	密码:<input type="password" name="password"/>
	 </c:if>
	 
  </body>
</html>


posted @ 2013-05-22 16:55  魅惑之眼  阅读(177)  评论(0编辑  收藏  举报