您的位置:首页 > 其它

08_表达式语言

2012-12-13 23:43 218 查看
8.1 EL(表达式语言)基础

JSTL,即JSP标准标记库,包括四个库:core,format,xml,sql。

EL的主要功能有:

a.访问储存对象:

b.对JavaBean的简化访问;

c.对集合的简化访问;

d.简单运算符;

e.条件输出;

f.EL的语法:${expression}

8.2 访问作用域属性

<%= request.getAttribute("varName")%>等价于${varName}

示例8-1:EL的作用域

源文件:ScopedVarsSetting.java

package uikoo9.el.demo;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@SuppressWarnings("serial")
public class D01_ScopedVarsSetting extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setAttribute("firstVar", "Tom");

HttpSession session = req.getSession();
session.setAttribute("secondVar", "Jack");

ServletContext application = this.getServletContext();
application.setAttribute("thirdVar", "Jerry");

RequestDispatcher dis = req.getRequestDispatcher("D02_getScopedVars.jsp");
dis.forward(req, resp);
}
}


示例8-2:通过EL从指定的作用域读取数据
源文件:getScopedVars.jsp

<%@ page language="java" contentType="text/html; charset=GBK"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>EL表达式</title>
</head>

<body>
<h1>使用EL表达式获得作用域属性</h1>
<table border="1" width="80%">
<tr>
<td width="50%">使用getAttribute方法</td>
<td width="50%">使用EL表达式</td>
</tr>
<tr>
<td width="50%"><%= request.getAttribute("firstVar") %></td>
<td width="50%">${firstVar}</td>
</tr>
<tr>
<td width="50%"><%= request.getAttribute("secondVar") %></td>
<td width="50%">${secondVar}</td>
</tr>
<tr>
<td width="50%"><%= request.getAttribute("thirdVar") %></td>
<td width="50%">${thirdVar}</td>
</tr>
</table>
</body>
</html>


8.3 使用EL访问JavaBean

示例8-3:表示书籍的JavaBean

源文件:Book.java

package uikoo9.el.demo;

public class D03_Book {
private String bookName;
private D04_Publisher publisher;

public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public D04_Publisher getPublisher() {
return publisher;
}
public void setPublisher(D04_Publisher publisher) {
this.publisher = publisher;
}
}


示例8-4:表示出版社的JavaBean
源文件:Publisher.java

package uikoo9.el.demo;

public class D04_Publisher {
private String publisherName;
private String address;

public D04_Publisher(String name, String addr){
this.publisherName = name;
this.address = addr;
}

public String getPublisherName() {
return publisherName;
}
public void setPublisherName(String publisherName) {
this.publisherName = publisherName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}


示例8-5:在Servlet中设置JavaBean

源文件:BeanSettingServlet.java

package uikoo9.el.demo;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class D05_BeanSettingServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
D04_Publisher publisher = new D04_Publisher(
"the People's Post and Telecommunication Press",
"Xizhaoshi Street");

D03_Book book = new D03_Book();
book.setBookName("JSP Programming");
book.setPublisher(publisher);

req.setAttribute("bookBean", book);

RequestDispatcher dis = req.getRequestDispatcher("D06_getBean.jsp");
dis.forward(req, resp);
}
}


示例8-6:在EL中使用param隐含对象接收数据
源文件:getBean.jsp

<%@ page language="java" contentType="text/html; charset=GBK"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>EL示例-使用param隐含对象接收数据</title>
</head>

<body>
<h1>${bookBean.bookName}的出版者是${bookBean.publisher.publisherName}</h1>
</body>
</html>


8.4 用EL访问集合对象

可以使用:${students[0].name}

8.5 EL隐含对象

示例8-7:接收数据的表单

源文件:ELForm.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>EL示例-数据输入</title>
</head>

<body>
<form action="D08_ELParam.sjp" method="post">
姓名:
<input type="text" name="userName" />
<input type="submit" name="submit" value="提交" />
</form>
</body>
</html>


示例8-8:使用EL读取表单数据
源文件:ELParam.jsp

<%@ page language="java" contentType="text/html; charset=GBK"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>EL示例-使用param隐含对象接收数据</title>
</head>

<body>
<h1>欢迎您,${param.userName}!</h1>
</body>
</html>


8.6 EL中的.与[]运算符
8.7 EL运算符

8.7.1 EL运算中的算术运算符

示例8-9:EL中的算术运算符示例

源文件:arith.jsp

<%@ page language="java" contentType="text/html; charset=GBK"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>EL算术运算</title></head>
<body>
<h1>算术运算符示例</h1>
<ul>
<li>\${3/7}运算结果是${3/7}</li>
<li>\${3 div 7}运算结果是${3 div 7}</li>
<li>\${3/0}运算结果是${3/0}</li>
<li>\${10%4}运算结果是${10%4}</li>
</ul>
</body>
</html>


8.7.2 EL运算中的关系运算符

示例8-10:EL关系运算符示例

源文件:compare.jsp

<%@ page language="java" contentType="text/html; charset=GBK"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>EL关系运算</title></head>
<body>
<h1>关系运算符示例</h1>
<ul>
<li>\${1 < 2}运算结果是${1 < 2}</li>
<li>\${1 > 2}运算结果是${1 > 2}</li>
<li>\${1 < 6}运算结果是${1 < 6}</li>
<li>\${1 > 6}运算结果是${1 > 6}</li>
<li>\${'a' < 'z'}运算结果是${'a' < 'z'}</li>
</ul>
</body>
</html>


8.7.3 EL运算中的逻辑运算符

8.7.4 EL运算中其他常用运算符

8.7.5 EL运算符优先级

8.8 自动类型转换
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: