EL与JSTL

EL与JSTL

EL表达式

概述

在jsp开发中,为了获取Servlet域对象中存储的数据,经常要写很多java代码,这样的做法会使JSP页面混乱,难以维护,为此,在JSP2.0规范中提供了EL表达式。它是Expression Language的缩写。

语法

${EL 表达式} 例如:${username}

注意

EL严格区分大小写,初学者严格按规范书写,有利于养成好的编码习惯

EL操作符

操作符“.”

获取对象的属性,例如:${news.title}

操作符“[]”

获取对象的属性,例如:${news["title"]} 获取集合中的对象,例如:${newsList[0]}

EL运算符

为了避免JSP混淆运算符和页面关键字,很多运算符都有替代写法

关系运算符范例结果
== eq${5 == 5}**或${5** eq 5}true
!= ne${5 != 5}**或${5 ne 5}**false
< lt${3 < 5}**或${3** lt 5}true
> gt${3 > 5}**或{3** gt 5}false
<= le${3 <= 5}**或${3 le 5}**true
>= ge${3 >= 5}**或${3** ge 5}false
运算符说明
( )改变执行的优先级,例如${3(4+5)}
+,-,*,/,%算术运算符,例如${3+2}
==,!=,>,>=,<,<=关系运算符,例如${a==b}或${a eq b}
&&,||,!逻辑运算符,例如${true&&false}
?:条件运算符,例如${a>b?1:2}
empty用于检测变量名是否为空或者没定义或者为空字符串或者没元素的数组或者集合,是否等于NULL,例如${empty name}

EL内置对象

  1. pageScope:从page范围域属性空间中查找指定的key

  2. requestScope:从request范围域属性空间中查找指定的key

  3. sessionScope:从session范围域属性空间中查找指定的key

  4. applicationScope:从application范围域属性空间中查找指定的key

  5. pageContext:该pageContext与JSP内置对象pageContext是同一个对象。通过该对象,可以获取到request、response、session、servletContext、servletConfig等对象注意:这些对象在EL里不是内置对象,这些对象只能通过pageContext获取

  6. param=request.getParameter()

  7. paramValues=request.getParameterValues()

目的 简单的数据处理和取值,最常用的就是取值

EL获取值

取得JavaBean对象的属性值

<jsp:usebean id="news" class="xxxxxxx">

${news.title}

取得数组、List、Map类型对象的元素

${list[0]}

使用各类运算符对原始数据进行简单处理

${totalRecordCount/pageSize}

屏蔽一些常见的异常

${username}

能实现简单的自动类型转换

${news}相当于(News)request.getAttribute("news")

注意:脚本中声明的对象是不能直接获取的,要放在相对应的域中才能得到

EL访问作用域

作用域Java代码取值EL取值
请求作用域request.getAttribute("news");${ requestScope.news }
会话作用域session.getAttribute("username");${ sessionScope.username }
程序作用域application.getAttribute("count");${ applicationScope.count }
页面作用域pageContext.getAttribute("userNum");${ pageScope.userNum }

直接写${news}就是从小到大去找

JSTL

简介

JSP标准标签库 实现JSP页面中的逻辑控制

使用步骤

下载jstl.jar和standard.jar包 将这两个包复制到WEB-INF\lib目录 在JSP页面中添加指令 <%@ taglib uri="Oracle Java Technologies | Oracle" prefix="c" %>

分类

标签库名称资源标示符(uri)前缀(prefix)
核心标签库Oracle Java Technologies | Oraclec
国际化/格式化标签库Oracle Java Technologies | Oraclefmt
XML标签库Oracle Java Technologies | Oraclex
数据库标签库Oracle Java Technologies | Oraclesql
函数标签库Oracle Java Technologies | Oraclefn

c:out标签

<c:out value="value" default="default" escapeXml="true|false" />
需要输出显示的表达式
默认输出显示的值
是否对输出的内容进行转义
​

c:set 标签

设置变量

<c:set  var="name"  value= "value" scope="scope" />
表示变量名称
表示变量的值
存在的作用域

设置对象属性

<c:set value= "value" target="target" property="propertyName" />
表示属性的值
表示对象
表示对象的属性名称

c:forEach标签(常用)

<c:forEach
    items="xxx"
    begin="xx"
    end="xx"
    step="xx"
    var="xx"
    varStatus="xx">
属性     描述                              是否必要           默认值
items   要被循环的信息                         否               无
begin   开始的元素(0=第一个元素,1=第二个元素)   否                0
end     最后一个元素(0=第一个元素,1=第二个元素) 否           Last element
step    每一次迭代的步长                       否                1
var     代表当前条目的变量名称                  否               无
varStatus   代表循环状态的变量名称              否               无
${varStatus.index} 此项的索引,从0开始 
${varStatus.count} 此项的计数序号,从1开始 
${varStatus.first} 此项是否是第一项,布尔值 
${varStatus.last} 此项是否是最后一项,布尔值 
${varStatus.begin} 此次迭代的起始索引,对应<c:foreach>中begin属性值 
${varStatus.end} 此次迭代的终止索引,对应<c:foreach>中end属性值 
${varStatus.step} 此次迭代的跳跃步伐,对应<c:foreach>中step属性值 

c:if标签(常用)

<c:if test="condition" var="varName" scope="scope">
         ……
</c:if>
判断的结果
判断结果存放的作用域

c:url标签

<c:url  value="url" />

c:param标签

<c:param name="name"  value="value"/>
参数的名称
参数的值

c:import标签

<c:import url="URL" />
导入资源的路径

fmt:formatDate标签

<fmt:formatDate  value="date"  pattern="yyyy-MM-dd HH:mm:ss"/>
时间对象
显示格式

这是我遇到的bug 

 

<%@page import="kgc.pojo.Student"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="studentService" class="kgc.service.StudentService"></jsp:useBean>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
html, body {
	height: 100%;
	overflow: auto;
}

body {
	background: url(ac.jpg) top left;
	background-size: 100%;
}

span {
	color: red;
	font-size: 30px;
}

#a {
	font-size: 20px;
}

table {
	/* 设置了这个属性,其余所有td都是相同的宽度。
这样操作之后,table是宽度固定了,但是里面的文章如果很长,文字会覆盖 */
	table-layout: fixed;
}

td {
	/* 自动换行就好了 */
	/* word-wrap:break-word; */
	width: 10px;
	height: 11px
}

li {
	display: flex;
	list-style-type: none;
}

ul {
	display: flex;
	padding: 0px
}

li>a {
	display: block;
	width: 70px;
	text-align: center;
	cursor: pointer;
	text-decoration: none;
}

.active {
	background-color: yellow;
}
</style>
</head>
<%
	String cpStr = request.getParameter("currentPage");
	String sumStr = request.getParameter("sumPage");
	int currentPage = (cpStr == null ? 1 : Integer.parseInt(cpStr));
	int sumPage = (sumStr == null ? 12 : Integer.parseInt(sumStr));
	List<Student> lists = studentService.queryLimit(currentPage, sumPage);
	int CountPage = studentService.queryAllNumber();
	int lastPage = CountPage % sumPage != 0 ? CountPage / sumPage + 1 : CountPage / sumPage;
	request.setAttribute("listss", lists);
%>
<body>
	<div>

		<!-- 	div  width 100等于 左靠齐 -->
		<div style="margin: 0px auto; width: 100%">

			您的手机号<%=request.getParameter("phone")%>
			您的密码<%=request.getParameter("loginPwd")%>
			<div style="width: 100%; text-align: center">
				<h1 style="color: blue">欢迎进入学生档案</h1>
			</div>
			<!-- 	width 1000等于表格宽度 -->
			<table border="1px" cellspacing="0px" width="1700" height="150">
				<tr>
					<th colspan="5">学生信息</th>
				</tr>
				<tr>
					<th>学生id</th>
					<th>学生学号</th>
					<th>学生姓名</th>
					<th>学生手机</th>
					<th>学生地址</th>
					<th>操作</th>
				</tr>
<%-- 						<%
				for (Student list : lists) {
			%>

			<tr>	
				<!-- style="display: none" -->
				<td ><%=list.getId() %></td>
				<td><%=list.getStudentNo()%>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
				<td><%=list.getStudentName()%>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
				<td><%=list.getPhone()%></td>
				<td><%=list.getAddress()%></td>				
				<td><a href="<%=request.getContextPath()%>/jsp/DeleteServlet?qqq=<%=list.getId() %>">用户删除</a>(慎点 数据会丢失)</td>
			</tr>
			
			<%
				}
			%>   --%>



<%--  		<c:forEach  var="list1" items="${listss}"> 
				<c:if test="${list1.id%2==0}">  
				<tr style="background-color: pink">			
				</c:if>
				<c:if test="${list1.id%2!=0}">
				<tr >				
				</c:if>				
					<td >${list1.id}</td>
					<td>${list1.studentNo}</td>
					<td>${list1.studentName}</td>
					<td>${list1.phone}</td>
					<td>${list1.address}</td>	
										
				</tr>
			</c:forEach>    --%>

 				<c:forEach var="list1" items="${listss}">
					<c:if test="${list1.id%2==0}">
						<tr style="background-color: pink">
					</c:if>
					<c:if test="${list1.id%2!=0}">
					<tr >
					</c:if>
					<td>${list1.id}</td>
					<td>${list1.studentNo}</td>
					<td>${list1.studentName}</td>
					<td>${list1.phone}</td>
					<td>${list1.address}</td>
					<td><a href="${pageContext.request.contextPath}/jsp/DeleteServlet?qqq=${list1.id}">用户删除</a>(慎点 数据会丢失)</td> 
					</tr>
				</c:forEach>  

				
			</table>
	
		
			<%-- 		<a href="Welcome.jsp?currentPage=1">首页</a>
			<!-- 使用三目运算符 比较  -->
			<a href="Welcome.jsp?currentPage=<%=CountPage%sumPage!=0?CountPage/sumPage+1:CountPage/sumPage%>">尾页</a>
			<a href="Welcome.jsp?currentPage=<%=currentPage!=1?currentPage-1:1%>">上一页</a>
			<a href="Welcome.jsp?currentPage=<%=currentPage!=(CountPage%sumPage!=0?CountPage/sumPage+1:CountPage/sumPage)?currentPage+1:currentPage%>">下一页</a> --%>
			<br>
			<br>



			<ul>
				<li><a href="Welcome.jsp?currentPage=1">首页</a></li>
				<li><a
					href="Welcome.jsp?currentPage=<%=currentPage != 1 ? currentPage - 1 : 1%>">上一页</a></li>

				<li class="<%=currentPage % 5 == 1 ? "active" : ""%>"><a
					href="Welcome.jsp?
	currentPage=
	<%=currentPage / 5 == 0 ? 1 : (currentPage - 1) / 5 * 5 + 1%>">
						<%=currentPage / 5 == 0 ? 1 : (currentPage - 1) / 5 * 5 + 1%> <span
						class="sr-only"></span>
				</a></li>
				<li class="<%=currentPage % 5 == 2 ? "active" : ""%>"><a
					href="Welcome.jsp?currentPage=<%=currentPage / 5 == 0 ? 2 : (currentPage - 1) / 5 * 5 + 2%>"><%=currentPage / 5 == 0 ? 2 : (currentPage - 1) / 5 * 5 + 2%><span
						class="sr-only"></span></a></li>
				<li class="<%=currentPage % 5 == 3 ? "active" : ""%>"><a
					href="Welcome.jsp?currentPage=<%=currentPage / 5 == 0 ? 3 : (currentPage - 1) / 5 * 5 + 3%>"><%=currentPage / 5 == 0 ? 3 : (currentPage - 1) / 5 * 5 + 3%><span
						class="sr-only"></span></a></li>
				<li class="<%=currentPage % 5 == 4 ? "active" : ""%>"><a
					href="Welcome.jsp?currentPage=<%=currentPage / 5 == 0 ? 4 : (currentPage - 1) / 5 * 5 + 4%>"><%=currentPage / 5 == 0 ? 4 : (currentPage - 1) / 5 * 5 + 4%><span
						class="sr-only"></span></a></li>
				<li class="<%=currentPage % 5 == 0 ? "active" : ""%>"><a
					href="Welcome.jsp?currentPage=<%=currentPage / 5 == 0 ? 5 : (currentPage - 1) / 5 * 5 + 5%>"><%=currentPage / 5 == 0 ? 5 : (currentPage - 1) / 5 * 5 + 5%><span
						class="sr-only"></span></a></li>
				<li><a
					href="Welcome.jsp?currentPage=<%=currentPage != lastPage ? currentPage + 1 : currentPage%>">下一页</a></li>
				<li><a href="Welcome.jsp?currentPage=<%=lastPage%>">末页</a></li>
				<%-- <li><input style="width: 50px" type="number" max="<%=CountPage%>" min="1"><span>页</span><button id="jump">跳</button></li> --%>
				<!-- 	<li><input  style="width: 50px"type="number" max="30" min="10"><span>条/页</span><button id="change">变</button></li> -->
			</ul>
			<form action="Welcome.jsp" mehtod="get">
				当前页数:<input type="number" max="<%=lastPage%>" min="1"
					name="currentPage" value="1" />请点击 <input type="submit"
					value="跳转页面" style="font-size: 20px" />
			</form>
			<form action="Welcome.jsp" mehtod="get">
				每页显示:<input type="number" max="30" min="1" name="sumPage" value="10" />条记录
				<input type="submit" value="改变记录" style="font-size: 20px" />
			</form>

		</div>

	</div>
	<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>



	<div style="margin: 0px auto; width: 20%">
		<h1 style="color: blue">修改学生信息</h1>
		<form action="${pageContext.request.contextPath}/jsp/ToChange"
			method="get">
			<div>
				<span>请输入学生ID</span><input type="text" name="id" />
			</div>
			<br> <br> <input type="submit" value="点击修改"
				style="font-size: 20px" />

		</form>
		<a href="${pageContext.request.contextPath}/jsp/AddTenServlet">点击增加10个用户</a>
	</div>
	</div>
	</div>

</body>
</html>

 JSTL中 c:forEach  items 后面不能加空格

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kjshuan

点个赞就好啦!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值