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属性的简略记法
空值取代错误消息
之前代码:
Java代码
1.<%
2. pageContext.setAttribute("name","darkness");
3.%>
4.<%
5. if(pageContext.getAttribute("name") != null)
6. {
7.%>
8. <%=pageContext.getAttribute("name")%>
9.<%
10. }
11.%>
12.
13.<!--可以发现,这样,代码较多-->
14.<%
15. pageContext.setAttribute("name","darkness");
16.%>
<%
pageContext.setAttribute("name","darkness");
%>
<%
if(pageContext.getAttribute("name") != null)
{
%>
<%=pageContext.getAttribute("name")%>
<%
}
%>
<!--可以发现,这样,代码较多-->
<%
pageContext.setAttribute("name","darkness");
%>
${name}
使用表达式时不需要去判断属性是否为空
Java代码
1.<!--如果在不同的属性范围中设置了同名的属性,则之后的同名属性无法找到-->
2.<!--按照四中标志位来寻找-->
3.<%
4. pageContext.setAttribute("name","darkness");
5. request.setAttribute("name","darkness");
6.%>
7.${name}
<!--如果在不同的属性范围中设置了同名的属性,则之后的同名属性无法找到-->
<!--按照四中标志位来寻找-->
<%
pageContext.setAttribute("name","darkness");
request.setAttribute("name","darkness");
%>
${name}
EL的主要功能在于MVC设计模式之中
后台:
Java代码
1.package org.sky.darkness.servlet.el ;
2.
3.import java.io.* ;
4.import javax.servlet.* ;
5.import javax.servlet.http.* ;
6.
7.public class ELServletDemo01 extends HttpServlet
8.{
9. public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
10. {
11. req.setAttribute("name","www.sky.org") ;
12. // 服务器端跳转
13. req.getRequestDispatcher("eldemo03.jsp").forward(req,resp) ;
14. }
15. public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
16. {
17. this.doGet(req,resp) ;
18. }
19.};
20./*
21. <servlet>
22. <servlet-name>el01</servlet-name>
23. <servlet-class> org.sky.darkness.servlet.el.ELServletDemo01</servlet-class>
24. </servlet>
25. <servlet-mapping>
26. <servlet-name>el01</servlet-name>
27. <url-pattern>/eldemo/el_1</url-pattern>
28. </servlet-mapping>
29.*/
30.后台:${name}
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”]}
Java代码
1.package org.sky.darkness.servlet.el ;
2.
3.import java.io.* ;
4.import javax.servlet.* ;
5.import javax.servlet.http.* ;
6.
7.public class ELServletDemo02 extends HttpServlet
8.{
9. public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
10. {
11. Simple s = new Simple() ;
12. s.setName("darkness") ;
13. req.setAttribute("sim",s) ;
14. // 服务器端跳转
15. req.getRequestDispatcher("eldemo04.jsp").forward(req,resp) ;
16. }
17. public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
18. {
19. this.doGet(req,resp) ;
20. }
21.};
22./*
23. <servlet>
24. <servlet-name>el02</servlet-name>
25. <servlet-class> org.sky.darkness.servlet.el.ELServletDemo02</servlet-class>
26. </servlet>
27. <servlet-mapping>
28. <servlet-name>el02</servlet-name>
29. <url-pattern>/eldemo/el_2</url-pattern>
30. </servlet-mapping>
31.*/
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>
*/Java代码
1.
2.package org.sky.darkness.servlet.el ;
3.
4.public class Simple
5.{
6. private String name ;
7. public void setName(String name)
8. {
9. this.name = name ;
10. }
11. public String getName()
12. {
13. return this.name ;
14. }
15.};
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页面中要写以下代码:
Java代码
1.<%@ page import=”org.sky.darkness.servlet.el.*”%>
2.<%
3.If(request.getAttribute(“sim”) != null){
4. Simple simp = (Simple)request.getAttribute(“sim”);
5.%>
6. <%=simp.getName()%>
7.<%
8. }
9.%>
10.<h1>${sim.name}</h1>
11.嵌套调用: ${ emp.name.firstName }
<%@ 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 }Java代码
1.package org.sky.darkness.servlet.el ;
2.
3.public class NameBean
4.{
5. private String firstName ;
6. private String lastName ;
7.
8. public void setFirstName(String firstName)
9. {
10. this.firstName = firstName ;
11. }
12. public void setLastName(String lastName)
13. {
14. this.lastName = lastName ;
15. }
16. public String getFirstName()
17. {
18. return this.firstName ;
19. }
20. public String getLastName()
21. {
22. return this.lastName ;
23. }
24.};
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 ;
}
};
Java代码
1.package org.sky.darkness.servlet.el ;
2.
3.public class EmployeeBean
4.{
5. private NameBean name ;
6. private CompanyBean company ;
7.
8. public void setName(NameBean name)
9. {
10. this.name = name ;
11. }
12. public void setCompany(CompanyBean company)
13. {
14. this.company = company ;
15. }
16. public NameBean getName()
17. {
18. return this.name ;
19. }
20. public CompanyBean getCompany()
21. {
22. return this.company ;
23. }
24.};
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 ;
}
};Java代码
1.
2.package org.sky.darkness.servlet.el ;
3.
4.public class CompanyBean
5.{
6. private String companyName ;
7. private String business ;
8.
9. public void setCompanyName(String companyName)
10. {
11. this.companyName = companyName ;
12. }
13. public void setBusiness(String business)
14. {
15. this.business = business ;
16. }
17. public String getCompanyName()
18. {
19. return this.companyName ;
20. }
21. public String getBusiness()
22. {
23. return this.business ;
24. }
25.};
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 ;
}
};
Java代码
1.package org.sky.darkness.servlet.el ;
2.
3.import java.io.* ;
4.import javax.servlet.* ;
5.import javax.servlet.http.* ;
6.
7.public class ELServletDemo03 extends HttpServlet
8.{
9. public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
10. {
11. NameBean nb = new NameBean() ;
12. nb.setFirstName("darkness") ;
13. nb.setLastName("depravedAngel") ;
14.
15. CompanyBean cb = new CompanyBean() ;
16. cb.setCompanyName("www.sky.org") ;
17. cb.setBusiness("JAVA EE、JAVA SE、JAVA ME") ;
18.
19. EmployeeBean eb = new EmployeeBean() ;
20. eb.setName(nb) ;
21. eb.setCompany(cb) ;
22.
23. // 保存属性
24. req.setAttribute("emp",eb) ;
25.
26. // 服务器端跳转
27. req.getRequestDispatcher("eldemo05.jsp").forward(req,resp) ;
28. }
29. public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
30. {
31. this.doGet(req,resp) ;
32. }
33.};
34./*
35. <servlet>
36. <servlet-name>el03</servlet-name>
37. <servlet-class> org.sky.darkness.servlet.el.ELServletDemo03</servlet-class>
38. </servlet>
39. <servlet-mapping>
40. <servlet-name>el03</servlet-name>
41. <url-pattern>/eldemo/el_3</url-pattern>
42. </servlet-mapping>
43.*/
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>
*/
Java代码
1.<h1>FirstName:${emp.name.firstName}</h1>
2.<h1>LastName:${emp.name.lastName}</h1>
3.<h1>CompanyName:${emp.company.companyName}</h1>
4.<h1>Business:${emp.company.business}</h1>
<h1>FirstName:${emp.name.firstName}</h1>
<h1>LastName:${emp.name.lastName}</h1>
<h1>CompanyName:${emp.company.companyName}</h1>
<h1>Business:${emp.company.business}</h1>
访问集合操作:
Java代码
1.package org.sky.darkness.servlet.el ;
2.
3.import java.io.* ;
4.import java.util.* ;
5.import javax.servlet.* ;
6.import javax.servlet.http.* ;
7.
8.public class ELServletDemo04 extends HttpServlet
9.{
10. public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
11. {
12. List l = new ArrayList() ;
13. Simple s = null ;
14. s = new Simple() ;
15. s.setName("darkness") ;
16. l.add(s) ;
17.
18. s = new Simple() ;
19. s.setName("wind") ;
20. l.add(s) ;
21.
22. s = new Simple() ;
23. s.setName("cloud") ;
24. l.add(s) ;
25.
26. req.setAttribute("all",l) ;
27. // 服务器端跳转
28. req.getRequestDispatcher("eldemo06.jsp").forward(req,resp) ;
29. }
30. public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException
31. {
32. this.doGet(req,resp) ;
33. }
34.};
35./*
36. <servlet>
37. <servlet-name>el02</servlet-name>
38. <servlet-class> org.sky.darkness.servlet.el.ELServletDemo02</servlet-class>
39. </servlet>
40. <servlet-mapping>
41. <servlet-name>el02</servlet-name>
42. <url-pattern>/eldemo/el_2</url-pattern>
43. </servlet-mapping>
44.*/
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>
*/
Java代码
1.<%@page import="java.util.*"%>
2.<%--
3. 要使用Iterator进行输出
4.--%>
5.<%
6. // List要使用Iterator输出
7. List l = (List)request.getAttribute("all") ;
8. Iterator iter = l.iterator() ;
9. while(iter.hasNext())
10. {
11. // 将取出的对象放在page范围之中
12. pageContext.setAttribute("sim",iter.next()) ;
13.%>
14. //调用隐式对象
15. <h1>${pageScope.sim.name}</h1>
16.<%
17. }
18.%>