EL&JSTL

目录

 

1.EL

1)EL表达式

2)EL从域中取出数据

3)EL的内置对象

4)EL执行表达式

2.JSTL

1)JSTL概述

2)JSTL下载与导入

3)JSTL核心库的常用标签

1.标签<c:if test=””>

2.标签<c:forEach>

 3.javaEE的开发模式

1)javaEE经历的模式

2)javaEE的三层架构


1.EL

1)EL表达式

EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL å‡ºçŽ°çš„ç›®çš„æ˜¯è¦æ›¿ä»£jsp页面中脚本的编写。

2)EL从域中取出数据

jsp脚本:<%=request.getAttribute(name)%>

EL表达式替代上面的脚本:${requestScope.name}

EL最主要的作用是获得四大域中的数据,格式${EL表达式}

EL获得pageContext域中的值:${pageScope.key};

EL获得request域中的值:${requestScope.key};

EL获得session域中的值:${sessionScope.key};

EL获得application域中的值:${applicationScope.key};

EL从四个域中获得某个值${key};

---同样是依次从pageContext域,request域,session域,application域中获取属性,在某个域中获取后将不在向后寻找

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="domain.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<!-- 模拟域中的数据 -->
	<%	
		//存储字符串
		request.setAttribute("company", "程序员");
		pageContext.setAttribute("company", "程序员1");
	
		//存储一个对象
		User user = new User();
		user.setId(1);
		user.setName("zhangsan");
		user.setPassword("123");
		session.setAttribute("user", user);
		
		//存储一个集合
		List<User> list = new ArrayList<User>();
		User user1 = new User();
		user1.setId(2);
		user1.setName("lisi");
		user1.setPassword("123");
		list.add(user1);
		User user2 = new User();
		user2.setId(3);
		user2.setName("wangwu");
		user2.setPassword("123");
		list.add(user2);
		application.setAttribute("list",list);
	%>
	
	<!-- 脚本方式取出域中的值 -->
	<%=request.getAttribute("company") %>
	<%
		User sessionUser = (User)session.getAttribute("user");
		out.write(sessionUser.getName());
	%>
	<hr/>
	<!-- 使用EL表达式获得域中的值 -->
	${requestScope.company }<%--程序员--%>
	${sessionScope.user.name }<%--zhangsan--%>
	${applicationScope.list[1].name }<%--wangwu--%>
	
	<!-- 使用el表达式全域查询 -->
	${company }<%--程序员1--%>
	${user.name }<%--zhangsan--%>
	${list[1].name }<%--wangwu--%>
</body>
</html>

3)EL的内置对象

获取JSP中域中的数据

pageScope,requestScope,sessionScope,applicationScope

接收参数

param,paramValues相当于request.getParameter()  rrquest.getParameterValues()

获取请求头信息

header,headerValues相当于request.getHeader(name)

获取全局初始化参数

initParam相当于this.getServletContext().getInitParameter(name)

WEB开发中cookie

cookie相当于request.getCookies()---cookie.getName()---cookie.getValue()

  <context-param>
  	<param-name>aaa</param-name>
  	<param-value>XXX</param-value>
  </context-param>
<%
    Cookie cookie = new Cookie("name","rose");
    response.addCookie(cookie);
%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<!-- 获得表单的参数 -->
	<%
		request.getParameter("username");
		
	%>
	<!-- 使用el获得表单参数 -->
	${param.username }
	${header.Host }
	${header["User-Agent"] }
	${initParam.aaa }
	${cookie.name.value }
	
	<!-- 通过el表达式获得request对象 -->
	${pageContext.request.contextPath }
</body>
</html>

 WEB开发中的pageContext

pageContext,pageContext获得其他八大对象

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="${pageContext.request.contextPath }/xxx.css">
<script type="text/javascript" src="${pageContext.request.contextPath }/yyy.js"></script>
<title>Insert title here</title>
</head>
<body>
	<form action="${pageContext.request.contextPath }/el/form2.jsp" method="post">
		<input type="text" name="username"><br>
		<input type="password" name="password"><br>
		<input type="checkbox" name="hobby" value="zq">足球
		<input type="checkbox" name="hobby" value="pq">排球
		<input type="checkbox" name="hobby" value="ppq">乒乓球<br>
		<input type="submit" value="提交"><br>
	</form>
	<img alt="" src="${pageContext.request.contextPath }/1.jpg">
	<img alt="" src="${pageContext.request.contextPath }/2.jpg">
	<img alt="" src="1.jpg">
</body>
</html>

${pageContext.request.contextPath } åŠ¨æ€èŽ·å–WEB应用名称

4)EL执行表达式

<!-- el可以执行表达式运算 -->
${1+1 }
${user==null?true:false}
<!-- empty判定某个对象是否为空 -->
${empty user}

2.JSTL

1)JSTL概述

JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能。jstl出现的目的同el一样也是要代替jsp页面中的脚本代码。JSTL标准标准标签库有5个子库,常使用的是他的核心库。

标签库

标签库的URI

前缀

Core

http://java.sun.com/jsp/jstl/core

c

I18N

http://java.sun.com/jsp/jstl/fmt

fmt

SQL

http://java.sun.com/jsp/jstl/sql

sql

XML

http://java.sun.com/jsp/jstl/xml

x

Functions

http://java.sun.com/jsp/jstl/functions

fn

2)JSTL下载与导入

从Apache的网站下载JSTL的JAR包。进入 http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/网址下载 JSTL的安装包。jakarta-taglibs-standard-1.1.2.zip,然后将下载好的JSTL安装包进行解压,此时,在lib目录下可以看到两个JAR文件,分别为jstl.jar和standard.jar。

jstl.jar文件包含JSTL规范中定义的接口和相关类,standard.jar文件包含用于    实现JSTL的.class文件以及JSTL中5个标签库描述符文件(TLD)

将两个jar包导入我们工程的lib中。

使用jsp的taglib指令导入核心标签库。

 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

3)JSTL核心库的常用标签

1.<c:if test=””>标签

其中test是返回boolean的条件

<%
    request.setAttribute("count", 10);
	
%>
<!-- jstl标签经常会和el配合使用 --	>
<!-- test代表的返回boolean的表达式 -->
<c:if test="${count==10 }">
    xxx
</c:if>

2.<c:forEach>标签

使用方式有两种组合形式:模拟for循环和模拟增强for

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<!-- forEach模拟
		for(int i=0;i<=5;i++){
			syso(i)
		}
	 -->
	 <c:forEach begin="0" end="5" var="i">
	 ${i }<br/>
	 </c:forEach>
	 
	 <!-- 模拟增强for    productList---List<Product>
		for(Product product : productList){
			syso(product.getPname());
		}
	 -->
	 <!-- items:一个集合或数组   var:代表集合中的某一个元素-->
	 <c:forEach items="${productList }" var="pro">
	 	${pro.pname }
	 </c:forEach>
</body>
</html>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="domain.User"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
		//模拟List<String> strList
		List<String> strList = new ArrayList<String>();
		strList.add("itcast");
		strList.add("itheima");
		strList.add("boxuegu");
		strList.add("shandingyu");
		request.setAttribute("strList", strList);
		
		//遍历List<User>的值
		List<User> userList = new ArrayList<User>();
		User user1 = new User();
		user1.setId(2);
		user1.setName("lisi");
		user1.setPassword("123");
		userList.add(user1);
		User user2 = new User();
		user2.setId(3);
		user2.setName("wangwu");
		user2.setPassword("123");
		userList.add(user2);
		application.setAttribute("userList", userList);
		
		//遍历Map<String,String>的值
		Map<String,String> strMap = new HashMap<String,String>();
		strMap.put("name", "lucy");
		strMap.put("age", "18");
		strMap.put("addr", "西三旗");
		strMap.put("email", "[email protected]");
		session.setAttribute("strMap", strMap);
		
		//遍历Map<String,User>的值
		Map<String,User> userMap = new HashMap<String,User>();
		userMap.put("user1", user1);
		userMap.put("user2", user2);
		request.setAttribute("userMap", userMap);
	%>
	
	<h1>取出strList的数据</h1>
	<c:forEach items="${strList }" var="str">
		${str }
	</c:forEach>
	
	<h1>取出userList的数据</h1>
	<c:forEach items="${userList }" var="user">
	user的name:${user.name }------user的password:${user.password }<br/>
	</c:forEach>
	
	<h1>取出strMap的数据</h1>
	<c:forEach items="${strMap }" var="entry">
		${entry.key }====${entry.value }<br/>
	</c:forEach>
	
	<h1>取出userMap的数据</h1>
	<c:forEach items="${userMap }" var="entry">
		${entry.key }:${entry.value.name }--${entry.value.password }<br/>
	</c:forEach>
</body>
</html>

 3.javaEE的开发模式

1)javaEE经历的模式

model1模式

    技术组成:jsp+javaBean

    model1的弊端:随着业务复杂性 导致jsp页面比较混乱

model2模式

    技术组成:jsp+servlet+javaBean

    model2的优点:开发中 使用各个技术擅长的方面

    servlet:擅长处理java业务代码

    jsp:擅长页面的现实

MVC:---- web开发的设计模式

M:Model---模型 javaBean:封装数据

V:View-----视图 jsp:单纯进行页面的显示

C:Controller----控制器 Servelt:获取数据--对数据进行封装--传递数据-- 指派显示的jsp页面

EL&JSTL

MVC是三层架构web层的技术。

2)javaEE的三层架构

服务器开发时 分为三层

web层:与客户端交互

service层:复杂业务处理

dao层:与数据库进行交互

开发实践时 三层架构通过包结构体现

EL&JSTL