您的位置:首页 > Web前端 > JavaScript

EL&JSTL

2017-12-30 10:27 267 查看
一、EL技术

1.EL 表达式概述

EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL 出现的目的是要替代jsp页面中脚本(java代码)的编写。

2.EL从域中取出数据(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域中 获取属性,在某个域中获取后将不在向后寻找

3.EL的内置对象11个

pageScope,requestScope,sessionScope,applicationScope

—- 获取JSP中域中的数据

param,paramValues - 接收参数.

相当于 : request.getParameter() rrquest.getParameterValues()

header,headerValues - 获取请求头信息

相当于request.getHeader(name)

initParam - 获取全局初始化参数

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


cookie - WEB开发中cookie

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

pageContext - WEB开发中的pageContext.

pageContext获得其他八大对象

${pageContext.request.contextPath}

相当于

<%=pageContext.getRequest().getContextPath%> 这句代码不能实现

获得WEB应用的名称

二:JSTL技术

1.JSTL概述

JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能。jstl出现的目的同el一样也是要代替jsp页面中的脚本代码。JSTL标准标准标签库有5个子库,但随着发展,目前常使用的是他的核心库Core http://java.sun.com/jsp/jstl/core c

3.JSTL核心库的常用标签

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

其中test是返回boolean的条件

2)< c:forEach>标签

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.*" %>
<%@ page import="com.west.domain.*" %>
<!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> list = new ArrayList<String>();
list.add("itcast");
list.add("west");
request.setAttribute("list",list);

List<User> userList = new ArrayList<User>();
User user1 = new User();
user1.setName("tom");
userList.add(user1);
User user2 = new User();
user2.setPassword("1234");
userList.add(user2);
application.setAttribute("userList", userList);

Map<String,User> usermap = new HashMap<String,User>();
usermap.put("user1",user1);
usermap.put("user2",user2);
request.setAttribute("usermap", usermap);
%>

<h1>list数据</h1>
<c:forEach items="${list }" var="str">
${str }<br>
</c:forEach>
<h1>userlist data</h1>
<c:forEach items="${userList }" var="user">
name:${user.name }-----password:${user.password }<br>
</c:forEach>
<hr>
<c:forEach items="${ usermap}" var="entry">
${entry.key }:${entry.value.name }-----${entry.value.password }<br>
</c:forEach>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  el jstl