EL表达式应用
初始EL表达式
EL表达式
- EL(Expression Language)表达式语言用于简化JSP的输出
- EL表达式的基本语法:
${表达式}
示例程序:
Student
public class Student {
private String name;
private String tel;
public Student(){
}
public Student(String name,String tel){
this.name = name;
this.tel = tel;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
StudentServlet
@WebServlet("/info")
public class StudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student stu = new Student("张三","123456");
request.setAttribute("stu",stu);
request.getRequestDispatcher("/info.jsp").forward(request,response);
}
}
info.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>学生姓名:${requestScope.stu.getName()}</h1>
<h1>学生手机:${requestScope.stu.getTel()}</h1>
</body>
</html>
在浏览器地址栏中输入:http://localhost:8080/el/info
页面显示结果:
EL的作用域对象
EL表达式内置四种作用域对象
- pageScope:从当前页面取值
- requestScope:从当前请求中获取属性值
- sessionScope:从当前会话中获取属性值
- applicationScope:从当前应用获取全局属性值
如果忽略书写作用域对象时,el则会按照作用域从小到大依次尝试获取
示例程序:
StudentServlet
@WebServlet("/info")
public class StudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student stu = new Student("张三", "123456");
HttpSession session = request.getSession();
session.setAttribute("stu", stu);
ServletContext context = request.getServletContext();
request.setAttribute("grade", "A");
session.setAttribute("grade", "B");
context.setAttribute("grade", "C");
request.getRequestDispatcher("/info.jsp").forward(request, response);
}
}
info.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>学生姓名:${sessionScope.stu.getName()}</h1>
<h1>学生手机:${sessionScope.stu.getTel()}</h1>
<!-- 会按照从小到大的顺序进行匹配,所以最先匹配到的作用域为 request 结果输出为 A -->
<h1>成绩评级:${grade}</h1>
</body>
</html>
页面显示结果为:
EL表达式输出
- 语法:
${[作用域.]属性名[.子属性]}
- EL表达式支持将运算结果进行输出
- EL支持绝大多数对象输出,本质是执行toString()方法
EL输出参数值
- EL表达式内置param对象来简化参数的输出
- 语法:
${param.参数名}
示例:
info.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>学生姓名:${sessionScope.stu.getName()}</h1>
<h1>学生手机:${sessionScope.stu.getTel()}</h1>
<!-- 会按照从小到大的顺序进行匹配,所以最先匹配到的作用域为 request 结果输出为 A -->
<h1>成绩评级:${grade}</h1>
<h1>指导教师:${param.teacher}</h1>
</body>
</html>
页面显示结果:
JSTL应用
JSTL介绍
- JSTL(JSP Standard Tag Library),JSP标准标签库
- JSTL用于简化JSP开发,提高代码的可读性与可维护性
- JSTL由SUN(Oracle)定义规范,由Apache Tomcat实现
JSTL v1.2.5 组件介绍
JSTL有两种安装方式
- 将下载的spec,impl Jar包复制到项目的/WEB-INF/lib目录
- 将Jar文件复制到Tomcat安装目录的lib目录
JSTL的标签库种类
JSTL按照功能可以划分为五类标签库
- core:核心标签库
- fmt:格式化输出标签库
- sql:SQL操作标签库
- xml:XML操作标签库
- functions:函数标签库
引用JSTL核心库
- 核心标签库(Core)是JSTL最重要的标签库,提供了JSTL的基础功能
- 核心库引入方式:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- JSTL核心标签库在taglibs-standard-impl.jar由META-INF/c.tld定义
JSTL判断标签
- JSTL核心库提供了两组判断标签
-
<c:if>
单分支判断 -
<c:choose>
,<c:when>
,<c:otherwise>
多分支判断
示例程序:
JSTL_if_Servlet
@WebServlet("/if_test")
public class JSTL_if_Servlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("score",87);
request.setAttribute("grade","B");
request.getRequestDispatcher("/core.jsp").forward(request,response);
}
}
core.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>${requestScope.score}</h1>
<c:if test="${requestScope.score >= 60}">
<h1 style="color: green;">pass</h1>
</c:if>
<c:if test="${requestScope.score < 60}">
<h1 style="color: red;">fail</h1>
</c:if>
<h1>${requestScope.grade}</h1>
<c:choose>
<c:when test="${requestScope.grade == 'A'}">
<h1>优秀</h1>
</c:when>
<c:when test="${requestScope.grade == 'B'}">
<h1>良好</h1>
</c:when>
<c:when test="${requestScope.grade == 'C'}">
<h1>及格</h1>
</c:when>
<c:otherwise>
<h1>不及格</h1>
</c:otherwise>
</c:choose>
</body>
</html>
页面显示结果为:
JSTL遍历集合
-
<c:forEach>
标签用于遍历集合(Collection)中的每一个对象
示例程序如下:
Student
public class Student {
private String name;
private int age;
public Student(String name,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
JSTL_forEach_Servlet
public class JSTL_forEach_Servlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Student> list = new ArrayList<>();
list.add(new Student("张三",18));
list.add(new Student("阿珍",17));
list.add(new Student("阿强",17));
request.setAttribute("students",list);
request.getRequestDispatcher("/core.jsp").forward(request,response);
}
}
core.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:forEach items="${requestScope.students}" var="student" varStatus="i">
<h1>${i.index + 1}. ${student.getName()}-${student.getAge()} </h1>
</c:forEach>
</body>
</html>
页面显示如下:
fmt格式化标签库-日期
- fmt格式化标签库的URI:
http://java.sun.com/jsp/jstl/fmt
- 格式化日期标签:
<fmt:formatDate value="" pattern="">
pattern中的格式:
yyyy - 表示四位年
MM - 表示月
dd - 表示日
HH - 表示小时
mm - 表示分
ss - 表示秒
SSS - 表示毫秒
示例程序如下:
fmt.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("now", new java.util.Date());
%>
<h2>${requestScope.now}</h2>
<h2>
<fmt:formatDate value="${requestScope.now}" pattern="yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒 SSS 毫秒" />
</h2>
</body>
</html>
页面显示:
fmt格式化标签库-数字
- fmt格式化标签库的URI:
http://java.sun.com/jsp/jstl/fmt
- 格式化数字标签:
<fmt:formatNumber value="" pattern="">
示例程序如下:
fmt.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("num", 1234567.456);
%>
<h2>${requestScope.num}</h2>
<h2>
<fmt:formatNumber value="${requestScope.num}" pattern="0.00" />
</h2>
<h2>
¥<fmt:formatNumber value="${requestScope.num}" pattern="0,000.00" />元
</h2>
</body>
</html>
页面显示如下: