EL/JSTL
目的就是去掉JSP中的java代码,都是以标签的形式表示,以标签的形式美工或者前端都可以修改,利于团队的合作。
这套标签和java语言无关。
JSP核心语法:JSP表达式<%=%> JSP脚本<%%>
JSP开发的原则:尽量在JSP页面少写甚至不写java代码,Java代码放在Servlet。
使用EL表达式来代替JSP表达式
EL表达式的作用:向浏览器输出域对象(只能输出域对象)的变量或者表达式计算的结果。
语法:${变量或表达式} ${list}
JSP表达式<%=%>
PageContxt(当前这个JSP页面)、Request、Session、ServletContext
EL 表达式概述
EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp表达式的编写,EL出现的目的是要替代jsp脚本编写。
主要作用:
1、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};
从四个域对象中取某个值${}
依次从page域、request域、session域、application域获取值,在某个域中找到不再往后找。
就是现在小的域范围找,找不到再去大的范围找.
1、获得普通字符串
2、获得Student对象的值
3、获得List的值
<%=request.getContextPath()%>
用EL表达式得到当前项目的路径
${pageContext.request.contextPath}
下面放上一段代码来举例子:
<%--EL作用从域对象中取出数据--%>
<%
//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,"wangwu","男",18);
session.setAttribute("student",student);
// 3、List集合
Student student1 = new Student(1, "王五1", "男", 10);
Student student2 = new Student(2, "王五2", "男", 18);
Student student3 = new Student(3, "王五3", "女", 12);
List<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
application.setAttribute("stuList",list);
%>
<%-- 1.普通字符串--%>
<%=pageContext.getAttribute("name")%><br>
<%=request.getAttribute("name")%><br>
<%--、EL表达式--%>
${pageScope.name}<br>
${requestScope.name}<br>
${name}<br>
<hr/>
<%--2、获取对象--%>
<%--使用JSP表达式--%>
<%=session.getAttribute("student")%><br>
<%
Student stu = (Student) session.getAttribute("student");
%>
<%=stu.getId()%>---<%=stu.getName()%><br>
<%--EL表达式--%>
${student}<br>
${student.id}---${student.name}<br>
<hr/>
<%--3.List<Student>--%>
<%--使用JSP表达式--%>
<%
List<Student> stuList = (List<Student>) application.getAttribute("stuList");
%>
<%=stuList.get(0).getName()%><br>
${stuList}<br>
${stuList[0].name}
<hr/>
<%=request.getContextPath()%>
${pageContext.request.contextPath}
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("name","zhangsan");
request.setAttribute("age",18);
%>
<c:set var="age" value="20" scope="request"></c:set>
${age}
<hr/>
<%--1、单条件判断--%>
<c:if test="${age == 20}">
20岁了
</c:if><br>
<c:if test="${age != 10}">
不是十岁
</c:if><br>
<c:set var="gender" value="男" scope="request"></c:set>
<c:if test="${gender == '男'}">
男
</c:if><br>
<c:if test="${gender == '女'}">
女
</c:if><br>
<%--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><br>
<!--
3、集合遍历
3.1、遍历List<String>
3.2、遍历List<Student>
-->
<c:forEach begin="0" end="5" var="i">
${i}<br>
</c:forEach>
<!-- 遍历List<Student> -->
<%
List<Student> list = new ArrayList();
Student student1 = new Student(1, "张三1", "男", 23);
Student student2 = new Student(2, "张三2", "男", 23);
Student student3 = new Student(3, "张三3", "男",23);
list.add(student1);
list.add(student2);
list.add(student3);
application.setAttribute("list", list);
%>
<c:forEach items="${list}" var="student">
${student.id} -- ${student.name} -- ${student.age}<br>
</c:forEach>
<!--
3.3、遍历Map<String,String>
Map<String,Student> -->
<%
Map<String,String> map = new HashMap<>();
map.put("cn","中国");
map.put("us","美国");
request.setAttribute("map",map);
%>
<c:forEach items="${map}" var="entry">
${entry.key} -- ${entry.value}<br>
</c:forEach>
</body>
</html>
记得导入jstl的jar包,并导入路径,jar包自行下载