Article Directory
First, the short answer questions
1. Description of what is EL expression?
EL表达式是为了使JSP写起来更加简单
. Learn the characteristics of multi-cast JavasScript independence, and simple label to represent complex logic, the user will be spun off from the tedious work of the type conversion.
2. Description of the role of EL expressions
① scope obtain data; ② for calculating.
3. Description of the role JSTL
① increase readability. ② traffic encapsulated JSTL easy reuse. ③ separated from the display data. ④ simplify JSP development, easy to maintain. ⑤ can be custom extensions.
Second, multiple-choice questions
(1) The following statement is correct ()
- A. EL expression finds objects range followed by request, pageContext, session, application.
- B. When using the EL expression output object attribute value if the attribute value is null, the output gap.
- C. If the object is to find the range specified, if the object bound is not found within this range will not do it again to find the range.
- When using an EL expression output D. Bean property, is not allowed under the underlying form.
The correct answer: C process is performed from pageContext, request, session, application in order to find four scopes.
(2) The following statement is correct ()
IF label A. Core tab, choose the label, forEach tag has test property.
B. inner core tab choose when and otherwise tags can contain sub-tab.
C. can not be obtained when the index forEach tag iteration.
D. custom labels can be inherited from javax.servlet.jsp.tagext.SimpleTagSupport class.
Correct answer: A, B, C, D forEach tag when the iteration index can be obtained by varStatus.
Third, the coding problem
1. EL obtain the following expression data set List
<%
List<String> list = new ArrayList<String>();
list.add("李芙蓉");
list.add("杨芙蓉");
list.add("王凤");
pageContext.setAttribute("list", list);
%>
code show as below:
<%@ 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. Complete the following requirements to use JSTL tags
Firstly, JavaBean, complete the following three requirements.
<%@ 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>
Demand (1): Employee object created in the JSP page, and for the property assignment, gender property store man and woman used to represent male and female. Determining if tags using gender attribute value, the output value is M man, man is not output female.
<%@ 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>
Demand (2): Employee object implemented using labels that choose gender attribute value is determined and output.
<%@ 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>
Requirements (3): the values of properties of objects in the collection forEach tag output request in the object bindings that are interlaced color different functions.
<%@ 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>