8.1 表达式语言简介
JSP用于在网页上显示动态内容,通常需要在JSP页面中嵌入Java脚本以完成复杂功能。但是大量的Java脚本使得JSP页面难以维护。一种类似JavaScript语言—JSP表达式语言可用于在网页上生成动态内容,并代替JSP脚本元素。JSP表达式语言是从JSP2.0规范开始支持的技术。
8.2 禁用EL 表达式计算
8.3 变量
8.4 隐式对象
8.5 文字常量
8.6 运算符
8.7 保留字
8.8 表达式使用案例
8.9 本章小结
EL的主要功能:
精确的访问存储对象
Bean属性的简略记法
空值取代错误消息
之前代码:
<% pageContext.setAttribute("name","darkness"); %> <% if(pageContext.getAttribute("name") != null) { %> <%=pageContext.getAttribute("name")%> <% } %> <!--可以发现,这样,代码较多--> <% pageContext.setAttribute("name","darkness"); %>
${name}
使用表达式时不需要去判断属性是否为空
<!--如果在不同的属性范围中设置了同名的属性,则之后的同名属性无法找到--> <!--按照四中标志位来寻找--> <% pageContext.setAttribute("name","darkness"); request.setAttribute("name","darkness"); %> ${name}
EL的主要功能在于MVC设计模式之中
后台:
package org.sky.darkness.servlet.el ; import java.io.* ; import javax.servlet.* ; import javax.servlet.http.* ; public class ELServletDemo01 extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException { req.setAttribute("name","www.sky.org") ; // 服务器端跳转 req.getRequestDispatcher("eldemo03.jsp").forward(req,resp) ; } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException { this.doGet(req,resp) ; } }; /* <servlet> <servlet-name>el01</servlet-name> <servlet-class> org.sky.darkness.servlet.el.ELServletDemo01</servlet-class> </servlet> <servlet-mapping> <servlet-name>el01</servlet-name> <url-pattern>/eldemo/el_1</url-pattern> </servlet-mapping> */ 后台:${name}
*访问Bean的属性 :
1、点号记法
${beanName.beanProperty}
2、数组记法
${beanName[“beanProperty”]}
package org.sky.darkness.servlet.el ; import java.io.* ; import javax.servlet.* ; import javax.servlet.http.* ; public class ELServletDemo02 extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException { Simple s = new Simple() ; s.setName("darkness") ; req.setAttribute("sim",s) ; // 服务器端跳转 req.getRequestDispatcher("eldemo04.jsp").forward(req,resp) ; } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException { this.doGet(req,resp) ; } }; /* <servlet> <servlet-name>el02</servlet-name> <servlet-class> org.sky.darkness.servlet.el.ELServletDemo02</servlet-class> </servlet> <servlet-mapping> <servlet-name>el02</servlet-name> <url-pattern>/eldemo/el_2</url-pattern> </servlet-mapping> */
package org.sky.darkness.servlet.el ; public class Simple { private String name ; public void setName(String name) { this.name = name ; } public String getName() { return this.name ; } };
如果保存的是一个对象,则在 JSP页面中要写以下代码:
<%@ page import=”org.sky.darkness.servlet.el.*”%> <% If(request.getAttribute(“sim”) != null){ Simple simp = (Simple)request.getAttribute(“sim”); %> <%=simp.getName()%> <% } %> <h1>${sim.name}</h1> 嵌套调用: ${ emp.name.firstName }
package org.sky.darkness.servlet.el ; public class NameBean { private String firstName ; private String lastName ; public void setFirstName(String firstName) { this.firstName = firstName ; } public void setLastName(String lastName) { this.lastName = lastName ; } public String getFirstName() { return this.firstName ; } public String getLastName() { return this.lastName ; } };
package org.sky.darkness.servlet.el ; public class EmployeeBean { private NameBean name ; private CompanyBean company ; public void setName(NameBean name) { this.name = name ; } public void setCompany(CompanyBean company) { this.company = company ; } public NameBean getName() { return this.name ; } public CompanyBean getCompany() { return this.company ; } };
package org.sky.darkness.servlet.el ; public class CompanyBean { private String companyName ; private String business ; public void setCompanyName(String companyName) { this.companyName = companyName ; } public void setBusiness(String business) { this.business = business ; } public String getCompanyName() { return this.companyName ; } public String getBusiness() { return this.business ; } };
package org.sky.darkness.servlet.el ; import java.io.* ; import javax.servlet.* ; import javax.servlet.http.* ; public class ELServletDemo03 extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException { NameBean nb = new NameBean() ; nb.setFirstName("darkness") ; nb.setLastName("depravedAngel") ; CompanyBean cb = new CompanyBean() ; cb.setCompanyName("www.sky.org") ; cb.setBusiness("JAVA EE、JAVA SE、JAVA ME") ; EmployeeBean eb = new EmployeeBean() ; eb.setName(nb) ; eb.setCompany(cb) ; // 保存属性 req.setAttribute("emp",eb) ; // 服务器端跳转 req.getRequestDispatcher("eldemo05.jsp").forward(req,resp) ; } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException { this.doGet(req,resp) ; } }; /* <servlet> <servlet-name>el03</servlet-name> <servlet-class> org.sky.darkness.servlet.el.ELServletDemo03</servlet-class> </servlet> <servlet-mapping> <servlet-name>el03</servlet-name> <url-pattern>/eldemo/el_3</url-pattern> </servlet-mapping> */
<h1>FirstName:${emp.name.firstName}</h1> <h1>LastName:${emp.name.lastName}</h1> <h1>CompanyName:${emp.company.companyName}</h1> <h1>Business:${emp.company.business}</h1>
访问集合操作:
package org.sky.darkness.servlet.el ; import java.io.* ; import java.util.* ; import javax.servlet.* ; import javax.servlet.http.* ; public class ELServletDemo04 extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException { List l = new ArrayList() ; Simple s = null ; s = new Simple() ; s.setName("darkness") ; l.add(s) ; s = new Simple() ; s.setName("wind") ; l.add(s) ; s = new Simple() ; s.setName("cloud") ; l.add(s) ; req.setAttribute("all",l) ; // 服务器端跳转 req.getRequestDispatcher("eldemo06.jsp").forward(req,resp) ; } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException { this.doGet(req,resp) ; } }; /* <servlet> <servlet-name>el02</servlet-name> <servlet-class> org.sky.darkness.servlet.el.ELServletDemo02</servlet-class> </servlet> <servlet-mapping> <servlet-name>el02</servlet-name> <url-pattern>/eldemo/el_2</url-pattern> </servlet-mapping> */
<%@page import="java.util.*"%> <%-- 要使用Iterator进行输出 --%> <% // List要使用Iterator输出 List l = (List)request.getAttribute("all") ; Iterator iter = l.iterator() ; while(iter.hasNext()) { // 将取出的对象放在page范围之中 pageContext.setAttribute("sim",iter.next()) ; %> //调用隐式对象 <h1>${pageScope.sim.name}</h1> <% } %>