1.初識(shí)EL表達(dá)式

1.1 簡(jiǎn)單認(rèn)識(shí)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表達(dá)式
//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>手機(jī):"+stu.getMobile()+"</h2>");
out.println("<h2>教師評(píng)級(jí):"+grade+"</h2>");
%>
</body>
</html>
注意需要導(dǎo)入類(lèi)import="com.imooc.el.Student"
用el表達(dá)式,則無(wú)需導(dǎo)入類(lèi),而且輸出也簡(jiǎn)化很多
//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>手機(jī):${requestScope.student.mobile }</h2>
<h2>評(píng)級(jí):${requestScope.grade}</h2>
</body>
</html>
1.2EL作用域?qū)ο?/h2>
image.png
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);
/*在不同的作用域設(shè)置同名屬性*/
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>手機(jī):${sessionScope.student.mobile }</h2>
<h2>評(píng)級(jí):${sessionScope.grade}</h2>
<h1>姓名:${student.name}</h1>
<h2>手機(jī):${student.mobile }</h2>
<h2>評(píng)級(jí):${grade}</h2>
</body>
</html>
1.3 EL表達(dá)式輸出
image.png
image.png

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);
/*在不同的作用域設(shè)置同名屬性*/
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>手機(jī):${sessionScope.student.mobile }</h2>
<h2>評(píng)級(jí):${sessionScope.grade}</h2>
<h1>姓名:${student.name}</h1>
<h2>手機(jī):${student.mobile }</h2>
<h2>評(píng)級(jí):${grade}</h2>
</body>
</html>


zh
注意:
1.對(duì)于直接輸出類(lèi),本質(zhì)上是執(zhí)行類(lèi)的toString方法,因此重寫(xiě)toString方法可以修改輸出內(nèi)容
2.對(duì)于EL輸出時(shí),EL人性化將輸出為null的對(duì)象,輸出為空,在頁(yè)面上就看不到null了
1.4EL輸出參數(shù)

http://localhost:8080/ServletProj/StduentServlet?teacher=andy可以用
<h2>講師:${param.teacher}</h2>直接獲取
2.JSTL標(biāo)簽庫(kù)
2.1簡(jiǎn)介
JSTL標(biāo)簽庫(kù)
*JSTL(JSP Standard Tag Library),JSP標(biāo)準(zhǔn)標(biāo)簽庫(kù)
*JSTL用于簡(jiǎn)化JSP開(kāi)發(fā),提高代碼的可讀性和可維護(hù)性
JSTL的標(biāo)簽庫(kù)種類(lèi)
JSTL按功能劃分為五類(lèi)標(biāo)簽庫(kù)
核心標(biāo)簽庫(kù) -core
格式化輸出標(biāo)簽庫(kù) -fmt
SQL操作標(biāo)簽庫(kù) -sql
XML操作標(biāo)簽庫(kù) -xml
函數(shù)標(biāo)簽庫(kù) -functions
引用JSTL核心庫(kù)
核心標(biāo)簽庫(kù)(Core)是JSTL最重要的標(biāo)簽庫(kù) 提供了JSTL的基礎(chǔ)功能
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
JSTL核心標(biāo)簽庫(kù)在taglib-standard-impl.jar由META-INF/c.tld定義

2.2判斷標(biāo)簽

注意:?jiǎn)畏种?lt;c:if>是沒(méi)有else的,多分支就不要寫(xiě)這個(gè)
//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">恭喜,通過(guò)測(cè)試</h1>
</c:if>
<c:if test = "${requestScope.score < 60}">
<h1 style="color:red">抱歉,請(qǐng)?jiān)俳釉賲?lt;/h1>
</c:if>
<!-- 多分支 -->
<h1>${grade}</h1>
<c:choose>
<c:when test = "${grade == 'A' }">
<h2>你很優(yōu)秀</h2>
</c:when>
<c:when test = "${grade == 'B' }">
<h2>不錯(cuò)哦</h2>
</c:when>
<c:when test = "${grade == 'C' }">
<h2>水平一般,需要提高</h2>
</c:when>
<c:when test = "${grade == 'D'}">
<h2>需要努力啦,不要?dú)怵H</h2>
</c:when>
<c:otherwise>
<h2>一切隨緣吧</h2>
</c:otherwise>
</c:choose>
</body>
</html>
2.3 遍歷標(biāo)簽

//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("慕課網(wǎng)","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>
<!-- 遍歷標(biāo)簽 -->
<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標(biāo)簽

//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小時(shí)制
hh - 12小時(shí)制
mm - 分鐘
ss - 秒
SSS - 毫秒
-->
<h1>
<fmt:formatDate value="${requestScope.now }" pattern="yyyy年MM月dd日HH時(shí)mm分ss秒SSS毫秒"/>
</h1>
<h1>${requestScope.amt}</h1>
<h1>
<!-- 保留小數(shù)點(diǎn)后兩位 -->
<fmt:formatNumber value="${requestScope.amt}" pattern="0.00"></fmt:formatNumber>
</h1>
<h1>
<!-- 增加分割逗號(hào) -->
¥<fmt:formatNumber value="${requestScope.amt}" pattern="0,000.00"></fmt:formatNumber>元
</h1>
<!-- c:out 與 `uri="http://java.sun.com/jsp/jstl/core" prefix="c" `標(biāo)簽庫(kù)有關(guān) -->
<h1>
<!-- 修改null輸出為空的情況,該為自定義 -->
null默認(rèn)值:<c:out value="${nothing}" default="無(wú)"></c:out>
</h1>
<h1>
<!-- 輸出帶有特殊字符,選擇是否需要進(jìn)行轉(zhuǎn)譯,保證原樣輸出 -->
<c:out value="${html}" escapeXml="true"></c:out>
</h1>
</body>
</html>
