首页 > 代码库 > 第8章 JSP和EL----补充
第8章 JSP和EL----补充
8.1 El及其在JSP中的重要地位
引入EL(Expresin Language)主要原因之一是,希望不依赖脚本元素就能创建表示层Jsp页面。脚本元素一般是用Java编写的代码,可以嵌入到一个
Jsp页面中。之所以想在Jsp中嵌入脚本元素,主要是受应用需求的驱使。要求使用脚本元素的主要应用需求如下:
为Jsp提供流程控制
设置Jsp页面局部变量,并在以后访问
要提供复杂表达式(涉及Java对象)的值
访问一个任意Java对象的属性
调用JavaBean或其它Java对象的方法
经验告诉我们,在Jsp中使用脚本元素,会使大型项目从长远来看很难维护。理想情况下,应尽一切可能创建没有脚本元素的Jsp
为了创建完全没有脚本元素也能正常工作的Jsp,它应满足上述5个应用需求,而且无需使用嵌入的Java代码。前两项由JSTL处理,后三项由EL解决
8.2 EL语法
${expression}
由${ 开头,接着expression是EL的表达式,最后以 }结尾。在这里${ 符号被视为EL的起始点,所以如果在Jsp网页中要显示${ 字符串,必须在前面加
上反斜杠符号\,亦即写成\${ 的格式,或者写成${ ‘ ${ ’ },也就是用EL来输出${ 符号。在EL中要输出一个字符串,可将此字符串放在一对单引
号或双引号内。
8.3 EL运算符
1.算术运算符
2.关系运算符
3.逻辑运算符
4.条件运算符
5.验证运算符
6. 运算符的优先级
补充:EL内置对象:
1..与[]运算符:
2.与存储有关的内置对象
applicationScope
sessionScope
requetScope
pageScope
但是4个隐含对象只能有业取得某个范围的属性值即jsp中的getAttribute(String name),不能取得其他信息.
3.与输入有关的内置对象
param
paramValues
4.其它内置对象
cookis
header内置对象
initParam内置对象
pageContext内置对象
例8-3 访问javaBean属性
eighth_example2.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link href=http://www.mamicode.com/"css/style.css" type="text/css" rel="stylesheet">
<title>用户注册</title>
</head><body>
<table width="659" height="425" border="0" align="center" cellpadding="0" cellspacing="0"background="image/backgrounb.jpg">
<tr>
<td height="71" align="center">用户注册</td>
</tr>
<tr>
<td valign="top">
<form name="form" method="post" action="getInfo.jsp">
<table width="346" border="1" align="center" cellpadding="0" cellspacing="0">
<tr align="center">
<td width="113" height="30">用 户 名:</td>
<td width="227"><input type="text" name="account"></td>
</tr>
<tr align="center">
<td height="30">密 码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr align="center">
<td height="30">姓 名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr align="center">
<td height="30">年 龄:</td>
<td><input type="text" name="age"></td>
</tr>
<tr align="center">
<td height="30">性 别:</td>
<td><input type="text" name="sex"></td>
</tr>
</table>
<table width="346" border="0" align="center">
<tr align="center">
<td>
<input type="submit" name="Submit" value=http://www.mamicode.com/"提交">
<input type="reset" name="Submit2" value=http://www.mamicode.com/"清除">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
getInfo.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link href=http://www.mamicode.com/"css/style.css" type="text/css" rel="stylesheet">
<jsp:useBean id="userBean" scope="request" class="pfc.UserBean"/>
<%
request.setCharacterEncoding("gb2312");
userBean.setAccount(request.getParameter("account"));
userBean.setPassword(request.getParameter("password"));
userBean.setAge(request.getParameter("age"));
userBean.setSex(request.getParameter("sex"));
userBean.setUsername(request.getParameter("username"));
%></head>
<body>
<table width="659" height="425" border="0" align="center" cellpadding="0" cellspacing="0"background="image/backgrounb.jpg">
<tr>
<td height="71" align="center">用户注册信息是</td>
</tr>
<tr>
<td valign="top">
<table width="346" border="1" align="center" cellpadding="0" cellspacing="0">
<tr align="center">
<td width="113" height="30">用 户 名:</td>
<td width="227">${userBean.account}</td>
</tr>
<tr align="center">
<td height="30">密 码:</td>
<td>${userBean.password}</td>
</tr>
<tr align="center">
<td height="30">姓 名:</td>
<td>${userBean.username}</td>
</tr>
<tr align="center">
<td height="30">年 龄:</td>
<td>${userBean.age}</td>
</tr>
<tr align="center">
<td height="30">性 别:</td>
<td>${userBean.sex}</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
UserBean.java
package pfc;
public class UserBean {
public String account = "";
public String password = "";
public String username= "";
public String age= "";
public String sex= "";
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
运行结果一
例4:pageContext隐含对象的应用,eighth_example3.jsp
<%@ page pageEncoding="gb2312"%>
<html>
<head>
<title>pageContext隐含对象的调用</title>
<link href=http://www.mamicode.com/"css/style.css" type="text/css" rel="stylesheet">
<body>
<p align="center">pageContext隐含对象的调用</p>
<table width="737" border="1" align="center">
<tr align="center">
<td>pageContext隐含对象的调用</td>
<td >说明</td>
<td >调用结果</td>
</tr>
<tr>
<td>\${pageContext.request.queryString}</td>
<td>获取参数</td>
<td>${pageContext.request.queryString}</td>
</tr>
<tr>
<td>\${pageContext.request.requestURL}</td>
<td>获取当前网页的地址,但是包含请求的参数</td>
<td>${pageContext.request.requestURL}</td>
</tr>
<tr>
<td>\${pageContext.request.contextPath}</td>
<td>web应用的名称</td>
<td>${pageContext.request.contextPath}</td>
</tr>
<tr>
<td>\${pageContext.request.method}</td>
<td>获取HTTP的方法(GET或POST)</td>
<td>${pageContext.request.method}</td>
</tr>
<tr>
<td>\${pageContext.request.remoteAddr}</td>
<td>获取用户的IP地址</td>
<td>${pageContext.request.remoteAddr}</td>
</tr>
</table>
</body>
</html>
例5:获取参数隐含对象的应用,eighth_example4.jsp
eighth_example4.jsp
<%@ page pageEncoding="gb2312"%>
<html>
<link href=http://www.mamicode.com/"css/style.css" type="text/css" rel="stylesheet">
<body>
<p align="center">param和paramValues隐含对象的应用</p><form name="form" method="post" action="getPara.jsp">
<table width="426" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="108" height="30">姓名</td>
<td width="298" height="30"><input type="text" name="name"></td>
</tr>
<tr>
<td height="30">性别</td>
<td height="30">
<input type="radio" name="sex" value=http://www.mamicode.com/"男">
男
<input type="radio" name="sex" value=http://www.mamicode.com/"女">
女</td>
</tr>
<tr>
<td height="30">年龄</td>
<td height="30"><input type="text" name="age"></td>
</tr>
<tr>
<td height="30">职业</td>
<td height="30"><input type="text" name="profession"></td>
</tr>
<tr>
<td height="30">您喜欢的高校</td>
<td height="30">
<input type="checkbox" name="school" value=http://www.mamicode.com/"清华">
清华
<input type="checkbox" name="school" value=http://www.mamicode.com/"北大">
北大
<input type="checkbox" name="school" value=http://www.mamicode.com/"北大方正软件学院">
北大方正软件学院
<input type="checkbox" name="school" value=http://www.mamicode.com/"其他">
其他</td>
</tr>
<tr align="center">
<td height="30" colspan="2"><input type="submit" name="Submit" value=http://www.mamicode.com/"提交">
<input type="reset" name="Submit2" value=http://www.mamicode.com/"清除">
</td>
</tr>
</table></form>
</body>
</html>getPara.jsp:
<%@ page pageEncoding="gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link href=http://www.mamicode.com/"css/style.css" type="text/css" rel="stylesheet">
<%request.setCharacterEncoding("gb2312");%></head>
<body>
<p align="center">param和paramValues隐含对象的参数</p>
<table width="416" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="108" height="30">姓名</td>
<td width="298" height="30">${param.name}</td>
</tr>
<tr>
<td height="30">性别</td>
<td height="30">${param.sex}</td>
</tr>
<tr>
<td height="30">年龄</td>
<td height="30">${param.age}</td>
</tr>
<tr>
<td height="30">职业</td>
<td height="30">${param.profession}</td>
</tr>
<tr>
<td height="30">您喜欢的高校</td>
<td height="30">${paramValues.school[0]} ${paramValues.school[1]} ${paramValues.school[2]} ${paramValues.school[3]}</td>
</tr>
</table>
</body>
</html>
例8-6 访问作用域范围隐含对象的应用
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>EL中pageScope、requestScope、sessionScope、applicationScope隐含对象</title>
</head>
<%
pageContext.setAttribute("name","作用域为page",PageContext.PAGE_SCOPE);
pageContext.setAttribute("name","作用域为request",PageContext.REQUEST_SCOPE);
pageContext.setAttribute("name","作用域为session",PageContext.SESSION_SCOPE);
pageContext.setAttribute("name","作用域为application",PageContext.APPLICATION_SCOPE);
%>
<body>
\${name}:${name}<br>
\${pageScope}:${pageScope.name}<br>
\${requestScope}:${requestScope.name}<br>
\${sessionScope}:${sessionScope.name}<br>
\${applicationScope}:${applicationScope.name}
</body>
</html>
例7 访问Servlet中的作用域,eighth_example6.jsp
eighth_example6.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><link href=http://www.mamicode.com/"css/style.css" rel="stylesheet" type="text/css">
<title>访问Servlet</title>
<style type="text/css">
<!--
body {
background-color: #FFCC00;
}
-->
</style></head>
<body>
<p align="center">访问Servlet</p>
<table width="402" height="100" border="0" align="center">
<tr>
<td width="143">作用域为request:</td>
<td width="306">${university1}</td>
</tr><tr>
<td>作用域为session:</td>
<td>${university2}、${university3}</td>
</tr>
<tr>
<td>作用域为application:</td>
<td>${university4}</td>
</tr>
<tr>
<td>访问数组:</td>
<td>${city[0]}、${city[1]}、${city[2]}</td>
</tr>
<tr>
<td>访问List集合:</td>
<td>${majorList[0]}、${majorList[1]}、${majorList[2]}</td>
</tr>
</table>
</body>
</html>UserInfoServlet.java
package pfc;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class UserInfoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 作用域为request
request.setAttribute("university1", "清华");
// 作用域为session
HttpSession session = request.getSession();
session.setAttribute("university2", "北大");
session.setAttribute("university3", "南大");
// 作用域为application
ServletContext application = getServletContext();
application.setAttribute("university4", "天大");
// 作用域为session,访问数组
String city[] = { "北京", "天津", "南京" };
session.setAttribute("city", city);
// 作用域为session,访问List容器
java.util.List<String> majorList = new java.util.ArrayList<String>();
majorList.add("软件技术");
majorList.add("软件测试");
majorList.add("游戏软件");
session.setAttribute("majorList", majorList);
RequestDispatcher dispatcher = request
.getRequestDispatcher("eighth_example6.jsp");
dispatcher.forward(request, response);
}
}
8.5 函数
由于在Jsp 2.0中,程序代码是独立放在Java class里, EL必须要有一个机制可以调用Java class 的方法,而这个机制即是函数。一个函数可以对应到一个Java class的public static method,这个对应是通过中介的文件,即标签库描述文件(Tag Library Descriptor,TLD)来完成。类必须声明为public 的,而类内被调用的方法必须声明为public static的
例 8-8 eighth_example7.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<%@ taglib prefix="fun" uri ="/WEB-INF/function.tld"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<link href=http://www.mamicode.com/"css/style.css" type="text/css" rel="stylesheet" />
<title>自定义函数的应用</title>
</head>
<style type="text/css">
<!--
body {
background-color: #FFCC00;
}
-->
</style>
<body>
<p align="center">自定义函数的应用</p>
<form name="form1" method="post" action="eighth_example7.jsp"><table width="300" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="272" align="center">
第一个字符串<input type="text" name="first" value=http://www.mamicode.com/"${param.first}">
</td>
</tr>
<tr>
<td width="272" align="center">
第二个字符串<input type="text" name="second" value=http://www.mamicode.com/"${param.second}">
</td>
</tr>
<tr>
<td width="272" align="center">
<input type="submit" name="Submit" value=http://www.mamicode.com/"提交">
</td>
</tr>
</table>
</form>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="center">
<td width="189" height="25">说明</td>
<td width="111">输出结果</td>
</tr>
<tr align="center">
<td height="25">将第一个字符串内容反向输出</td>
<td>${fun:reverse(param.first)}</td>
</tr>
<tr align="center">
<td height="25">将第一个字符串内容转换为大写字母</td>
<td>${fun:cape(param.first)}</td>
</tr>
<tr align="center">
<td height="25">将两个字符串内容连接</td>
<td>${fun:connect(param.first,param.second)}</td>
</tr>
</table>
</body>
</html>KindMethod.java
package pfc;
public class KindMethod {
// 反向输出
public static String reverse(String text) {
return new StringBuffer(text).reverse().toString();
}
// 转换成大写字母
public static String cape(String text) {
return text.toUpperCase();
}
public static String connect(String x, String y) {
return x+y;
}
}function.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>library</description>
<display-name>functions</display-name>
<tlib-version>1.1</tlib-version>
<short-name>fun</short-name>
<function>
<description>reverse</description>
<name>reverse</name>
<function-class>pfc.KindMethod</function-class>
<function-signature>java.lang.String reverse( java.lang.String )</function-signature>
</function>
<function>
<description>cape</description>
<name>cape</name>
<function-class>pfc.KindMethod</function-class>
<function-signature>java.lang.String cape( java.lang.String )</function-signature>
</function>
<function>
<description>connect</description>
<name>connect</name>
<function-class>pfc.KindMethod</function-class>
<function-signature>java.lang.String connect( java.lang.String, java.lang.String)</function-signature>
</function>
</taglib>表8.5 function.tld的说明
description 函数说明 name 函数名称 function-class 实现此函数java类名称 function-signature 用来实现此函数的java mthod声明
第8章 JSP和EL----补充