文章目录
一、简答题
1.简述什么是EL表达式?
EL表达式是为了使JSP写起来更加简单
。借鉴了JavasScript多类型转换无关性的特点,并且简单的标签来表现复杂的逻辑,将用户从类型转换的繁琐工作中脱离出来。
2.简述EL表达式的作用
①获取作用域中数据;②用于计算。
3.简述JSTL的作用
①增加可读性。②将业务封装到JSTL可以方便重用。③数据与显示分离。④简化JSP开发,易于维护。⑤可以对其进行自定义扩展。
二、选择题
(1)下列说法正确的是()
- A. EL表达式查找对象的范围依次是request,pageContext,session,application。
- B. 使用EL表达式输出对象的属性值时,如果属性值为空,则输出空白。
- C. 如果指定了对象的查找范围,那么如果在该范围内没有找到绑定的对象则不会再去其他范围进行查找了。
- D. 使用EL表达式输出Bean属性时,不允许使用下标的形式。
正确答案:C 执行的过程为从pageContext、request、session、application四个作用域中依次查找。
(2)下列说法正确的是()
A. 核心标签中的if标签、choose标签、forEach标签都有test属性。
B. 核心标签中的choose标签内可以包含when和otherwise子标签。
C.无法获取forEach标签迭代时的下标。
D. 自定义标签时可以继承自javax.servlet.jsp.tagext.SimpleTagSupport类。
正确答案:A,B,C,D forEach标签迭代时的下标可以通过varStatus获取。
三、编码题
1.使用EL表达式获取下列List集合的数据
<%
List<String> list = new ArrayList<String>();
list.add("李芙蓉");
list.add("杨芙蓉");
list.add("王凤");
pageContext.setAttribute("list", list);
%>
代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equive="content-type" content="text/html,charset=utf-8"/>
</head>
<%
List<String> list = new ArrayList<String>();
list.add("李芙蓉");
list.add("杨芙蓉");
list.add("王凤");
pageContext.setAttribute("list", list);
%>
<body>
${list[0]},${list[1]},${list[2]}.
</body>
</html>
2.使用JSTL标签完成下列需求
首先给出JavaBean,完成下列三个需求。
<%@ page language="java" import="java.util.*,bean.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equive="content-type" content="text/html;charset=utf-8"/>
<style type="text/css">
.s0{
color:red;
}
.s1{
color:pink;
}
</style>
</head>
<body>
<%
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee("Jim","man"));
employees.add(new Employee("Kitty","woman"));
employees.add(new Employee("KangKang","man"));
employees.add(new Employee("Tom","man"));
session.setAttribute("employees",employees);
%>
<c:forEach var ="emp" items="${sessionScope.employees}" varStatus="s">
<p class="s${s.index%2}">
姓名:${emp.name }
性别:<c:if test="${emp.gender=='man'}" var="rs">男</c:if>
<c:if test="${!rs}">女</c:if>
index:${s.index}
count:${s.count}
</p>
</c:forEach>
</body>
</html>
需求(1):在JSP页面中创建的Employee对象,并为属性赋值,gender属性存储man和woman用来代表男和女。使用if标签判断gender属性的值,值是man输出男,不是man则输出女。
<%@ page language="java" import="java.util.*,bean.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equive="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<%
Employee employee = new Employee("tom","man");
request.setAttribute("employee",employee);
%>
姓名:${employee.name}<br/>
性别:
<c:if test="${employee.gender=='man' }" var="rs" scope="request">男</c:if>
<c:if test="${!rs}">女</c:if>
</body>
</html>
需求(2):使用choose标签实现Employee对象gender属性的值的判断及输出。
<%@ page language="java" import="java.util.*,bean.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equive="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<%
Employee employee = new Employee("tom","man");
request.setAttribute("employee",employee);
%>
姓名:${requestScope.employee.name}<br/>
性别:
<c:choose>
<c:when test="${requestScope.employee.gender=='man'}">男</c:when>
<c:when test="${requestScope.employee.gender=='woman'}">女</c:when>
<c:otherwise>其他</c:otherwise>
</c:choose>
</body>
</html>
需求(3):使用forEach标签输出request中绑定的集合中对象的属性值实现不同对象隔行变色的功能。
<%@ page language="java" import="java.util.*,bean.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equive="content-type" content="text/html;charset=utf-8"/>
<style type="text/css">
.s0{
color:red;
}
.s1{
color:pink;
}
</style>
</head>
<body>
<%
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee("Jim","man"));
employees.add(new Employee("Kitty","woman"));
employees.add(new Employee("KangKang","man"));
employees.add(new Employee("Tom","man"));
session.setAttribute("employees",employees);
%>
<c:forEach var ="emp" items="${sessionScope.employees}" varStatus="s">
<p class="s${s.index%2}">
姓名:${emp.name }
性别:<c:if test="${emp.gender=='man'}" var="rs">男</c:if>
<c:if test="${!rs}">女</c:if>
index:${s.index}
count:${s.count}
</p>
</c:forEach>
</body>
</html>