EL表达式与JSTL这两种技术的使用,都是为了去掉JSP页面中的Java代码,全部以标签的形式表示,有利于团队的合作
EL表达式:主要作用是从域对象中取出数据
下图中的例子总结了EL表达式的一些用法,分别以pageContext、request、session、application四个域对象为例
<html>
<head>
<title>Title</title>
</head>
<body>
<%--JSP脚本--%>
<%
// HttpServletRequest request = new HttpServletRequest();
// ServletContext application = new ServletContext();
// 内置对象(页面已经创建好了的对象):pageContext、request、session、application
// 1、普通字符串
pageContext.setAttribute("name", "lisi");
request.setAttribute("name", "zhangsan");
// 2、Student对象
Student student = new Student(1, "王五", 23, "男");
session.setAttribute("student", student);
// 3、List集合
Student student1 = new Student(1, "王五1", 23, "男");
Student student2 = new Student(1, "王五2", 23, "男");
Student student3 = new Student(1, "王五3", 23, "男");
List<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
application.setAttribute("stuList", list);
%>
<%--1、普通字符串--%>
<%--JSP表达式方式拿出来--%>
<%=pageContext.getAttribute("name")%><br>
<%=request.getAttribute("name")%><br>
<hr>
<%--EL表达式的方式--%>
${pageScope.name}<br>
${requestScope.name}<br>
<%--直接写name的话,如果有重名的情况,会按域对象作用范围从小到大的
顺序来拿,所以这里显示的是pageContext里的,但是一般不会重名--%>
${name}<br>
<hr>
<%--2、获取对象--%>
<%--使用JSP表达式--%>
<%=session.getAttribute("student")%><br>
<%
Student stu = (Student) session.getAttribute("student");
%>
<%=stu.getId()%>--<%=stu.getName()%><br>
<hr>
<%--EL表达式--%>
${student}<br>
${student.id}<br>
${student.name}<br>
<hr>
<%--List<Student>--%>
<%-- 使用JSP表达式--%>
<%
List<Student> stuList = (List<Student>) application.getAttribute("stuList");
%>
<%=stuList.get(0).getName()%><br>
<hr>
<%--EL表达式--%>
${stuList}<br>
${stuList[0].name}
<hr/>
<%--获取项目路径--%>
<%--JSP表达式--%>
<%=request.getContextPath()%><br>
<hr>
<%--EL表达式--%>
${pageContext.request.contextPath}
<hr>
</body>
</html>
JSTL技术
JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在JSP页面中使用标签的形式完成业务逻辑等功能。同样也是为了替代JSP脚本,让JSP页面中不再出现Java代码。使用前要先把对应jar包放入项目的目录中,同时在JSP页面开始的地方加入如下代码,才可以正常使用。
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
JSTL的一些用法总结:
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("age", 18);
request.setAttribute("gender", "女");
%>
<c:set var="age" value="20" scope="request"></c:set>
${age}
<hr>
<%--1、单条件判断--%>
<c:if test="${age==20}">
今年20岁了
</c:if>
<c:if test="${age!=20}">
今年不是20岁了
</c:if>
<hr>
<c:if test="${gender=='男'}">
男
</c:if>
<c:if test="${gender=='女'}">
女
</c:if>
<hr>
<%--2、多条件判断--%>
<c:set var="score" value="90" scope="request"></c:set>
<c:choose>
<c:when test="${score >= 90 && score <= 100}">
优秀
</c:when>
<c:when test="${score >= 80 && score < 90}">
良好
</c:when>
<c:when test="${score >= 70 && score < 80}">
一般
</c:when>
<c:when test="${score >= 60 && score < 70}">
及格
</c:when>
<c:otherwise>
不及格
</c:otherwise>
</c:choose>
<hr>
<%--遍历集合--%>
<c:forEach begin="0" end="5" var="i">
${i}<br>
</c:forEach>
<hr>
<%
Student student1 = new Student(1, "王五1", 23, "男");
Student student2 = new Student(1, "王五2", 23, "男");
Student student3 = new Student(1, "王五3", 23, "男");
List<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
application.setAttribute("stuList", list);
%>
<c:forEach items="${stuList}" var="student">
${student.id} -- ${student.name} -- ${student.age} <br>
</c:forEach>
<hr>
<%
Map<String, String> map = new HashMap<>();
map.put("1", "11");
map.put("2", "22");
application.setAttribute("map", map);
%>
<c:forEach items="${map}" var="entry">
${entry.key} -- ${entry.value}
</c:forEach>
</body>
</html>