1.初识EL表达式
1.1 简单认识EL
//Stduent.java
package com.imooc.el;
public class Student {
public String name;
public String mobile;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
//StduentServlet.java
package com.imooc.el;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class StduentServlet
*/
@WebServlet("/StduentServlet")
public class StduentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StduentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student stu = new Student();
stu.setName("hachiman");
stu.setMobile(null);
String grade = "A";
request.setAttribute("student", stu);
request.setAttribute("grade", grade);
request.getRequestDispatcher("/el_info.jsp").forward(request, response);
}
}
用普通的jsp表达式
//info.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.imooc.el.Student"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Student stu = (Student)request.getAttribute("student");
String grade = (String)request.getAttribute("grade");
out.println("<h1>姓名:"+stu.getName()+"</h1>");
out.println("<h2>手机:"+stu.getMobile()+"</h2>");
out.println("<h2>教师评级:"+grade+"</h2>");
%>
</body>
</html>
注意需要导入类import="com.imooc.el.Student"
用el表达式,则无需导入类,而且输出也简化很多
//el_info.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>姓名:${requestScope.student.name}</h1>
<h2>手机:${requestScope.student.mobile }</h2>
<h2>评级:${requestScope.grade}</h2>
</body>
</html>
1.2EL作用域对象
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student stu = new Student();
stu.setName("hachiman");
stu.setMobile(null);
String grade = "A";
HttpSession session = request.getSession();
session.setAttribute("student", stu);
session.setAttribute("grade", grade);
/*在不同的作用域设置同名属性*/
request.setAttribute("grade", "A");//request作用域
session.setAttribute("grade", "B");//session作用域
request.getServletContext().setAttribute("grade", "C");//context全局作用域
//request.setAttribute("student", stu);
//request.setAttribute("grade", grade);
request.getRequestDispatcher("/el_info.jsp").forward(request, response);
}
//jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>姓名:${sessionScope.student.name}</h1>
<h2>手机:${sessionScope.student.mobile }</h2>
<h2>评级:${sessionScope.grade}</h2>
<h1>姓名:${student.name}</h1>
<h2>手机:${student.mobile }</h2>
<h2>评级:${grade}</h2>
</body>
</html>
1.3 EL表达式输出
zh
注意:
1.对于直接输出类,本质上是执行类的toString方法,因此重写toString方法可以修改输出内容
2.对于EL输出时,EL人性化将输出为null的对象,输出为空,在页面上就看不到null了
1.4EL输出参数
http://localhost:8080/ServletProj/StduentServlet?teacher=andy
可以用
<h2>讲师:${param.teacher}</h2>
直接获取
2.JSTL标签库
2.1简介
JSTL标签库
*JSTL(JSP Standard Tag Library),JSP标准标签库
*JSTL用于简化JSP开发,提高代码的可读性和可维护性
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核心标签库在taglib-standard-impl.jar由META-INF/c.tld定义
2.2判断标签
注意:单分支<c:if>是没有else的,多分支就不要写这个
//ServletJstl.java
package com.imooc.jstl;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletJstl
*/
@WebServlet("/jstl")
public class ServletJstl extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletJstl() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("score",58);
request.setAttribute("grade", "B");
request.getRequestDispatcher("/core.jsp").forward(request, response);
}
}
//core.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 单分支 -->
<h1>${requestScope.score}</h1>
<c:if test = "${ score >= 60}">
<h1 style = "color:green">恭喜,通过测试</h1>
</c:if>
<c:if test = "${requestScope.score < 60}">
<h1 style="color:red">抱歉,请再接再厉</h1>
</c:if>
<!-- 多分支 -->
<h1>${grade}</h1>
<c:choose>
<c:when test = "${grade == 'A' }">
<h2>你很优秀</h2>
</c:when>
<c:when test = "${grade == 'B' }">
<h2>不错哦</h2>
</c:when>
<c:when test = "${grade == 'C' }">
<h2>水平一般,需要提高</h2>
</c:when>
<c:when test = "${grade == 'D'}">
<h2>需要努力啦,不要气馁</h2>
</c:when>
<c:otherwise>
<h2>一切随缘吧</h2>
</c:otherwise>
</c:choose>
</body>
</html>
2.3 遍历标签
//Company.java
package com.imooc.jstl;
public class Company {
private String cname;
private String url;
public Company(String cname,String url) {
this.cname = cname;
this.url = url;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
//ServletJstl.java
@WebServlet("/jstl")
public class ServletJstl extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletJstl() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Company> list = new ArrayList();
list.add(new Company("腾讯","www.tencent.com"));
list.add(new Company("百度","www.baidu.com"));
list.add(new Company("慕课网","www.imooc.com"));
request.setAttribute("companies", list);
request.getRequestDispatcher("/core.jsp").forward(request, response);
}
}
//core.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 遍历标签 -->
<c:forEach items="${requestScope.companies}" var="c" varStatus="idx">
<h2>${idx.index+1}.${c.cname}-${c.url }</h2>
</c:forEach>
</body>
</html>
2.4 fmt标签
//fmt.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setAttribute("amt", 19876543.567);
request.setAttribute("now",new java.util.Date());
request.setAttribute("html","<a href='index.html'>index</a>");
request.setAttribute("nothing", null);
%>
<h1>${requestScope.now}</h1>
<!-- formatDate pattern
yyyy - 四位年
MM - 两位月
dd - 两位日
HH - 24小时制
hh - 12小时制
mm - 分钟
ss - 秒
SSS - 毫秒
-->
<h1>
<fmt:formatDate value="${requestScope.now }" pattern="yyyy年MM月dd日HH时mm分ss秒SSS毫秒"/>
</h1>
<h1>${requestScope.amt}</h1>
<h1>
<!-- 保留小数点后两位 -->
<fmt:formatNumber value="${requestScope.amt}" pattern="0.00"></fmt:formatNumber>
</h1>
<h1>
<!-- 增加分割逗号 -->
¥<fmt:formatNumber value="${requestScope.amt}" pattern="0,000.00"></fmt:formatNumber>元
</h1>
<!-- c:out 与 `uri="http://java.sun.com/jsp/jstl/core" prefix="c" `标签库有关 -->
<h1>
<!-- 修改null输出为空的情况,该为自定义 -->
null默认值:<c:out value="${nothing}" default="无"></c:out>
</h1>
<h1>
<!-- 输出带有特殊字符,选择是否需要进行转译,保证原样输出 -->
<c:out value="${html}" escapeXml="true"></c:out>
</h1>
</body>
</html>