Jsp2特性
要使用 jsp2 的特性,web.xml必须使用 servlet2.4 版本以上的配置文件。这里列出 servlet3.1 规范的 web-app
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
1.直接配置JSP页面属性
jsp页面属性可以使用<jsp-property-group/>元素配置,常用的如下:
1)<el-ignored/> 不允许使用el表达式吗?默认false
2)<script-invalid/>不允许使用jsp脚本吗?默认false
3)<include-prelude/>隐式导入页面头,<include-coda/>隐式导入页面尾。
4)<page-encoding/>可以确定jsp页面的编码,可以替代每个页面中 page-encoding 部分
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 关于JSP的配置信息 -->
<jsp-config>
<jsp-property-group>
<!-- 对哪些文件应用配置 -->
<url-pattern>/noscript/*</url-pattern>
<!-- 忽略表达式语言 -->
<el-ignored>true</el-ignored>
<!-- 页面编码的字符集 -->
<page-encoding>GBK</page-encoding>
<!-- 不允许使用Java脚本 -->
<scripting-invalid>true</scripting-invalid>
<!-- 隐式导入页面头 -->
<include-prelude>/inc/top.jspf</include-prelude>
<!-- 隐式导入页面尾 -->
<include-coda>/inc/bottom.jspf</include-coda>
</jsp-property-group>
<jsp-property-group>
<!-- 对哪些文件应用配置 -->
<url-pattern>*.jsp</url-pattern>
<el-ignored>false</el-ignored>
<!-- 页面编码字符集 -->
<page-encoding>GBK</page-encoding>
<!-- 允许使用Java脚本 -->
<scripting-invalid>false</scripting-invalid>
</jsp-property-group>
<jsp-property-group>
<!-- 对哪些文件应用配置 -->
<url-pattern>/inc/*</url-pattern>
<el-ignored>false</el-ignored>
<!-- 页面编码字符集 -->
<page-encoding>GBK</page-encoding>
<!-- 不允许使用Java脚本 -->
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
<context-param>
<param-name>author</param-name>
<param-value>yeeku</param-value>
</context-param>
</web-app>
2.表达式语言的内置对象
表达式语言包含如下11个内置对象
pageContext:代表该页面的pageContext 对象。
pageScope:用于获取page范围内的属性值。
requestScope : 用于获取request范围内的属性值。
sessionScope:用于获取session 范围内的属性。
applicationScope : 用于获取application 范围内的属性值。
param:用于获取请求参数的属性值。
paramValues : 用于获取请求的参数值,这个用来获取属性值为数组的属性值。
header:用于获取请求头的属性值。
headerValues : 用于获取请求头的属性值,这个用来获取属性值为数组的属性值。
initParam :用于获取请求Web应用的初始化参数。
cookies:用于获取指定的cookie值。
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> 表达式语言 - 内置对象 </title>
<meta name="website" content="http://www.crazyit.org" />
</head>
<body>
<h2>表达式语言 - 内置对象</h2>
请输入你的名字:
<!-- 通过表单提交请求参数 -->
<form action="implicit-objects.jsp" method="post">
<!-- 通过${param['name']} 获取请求参数 -->
你的名字 = <input type="text" name="name" value="${param['name']}"/>
<input type="submit" value='提交'/>
</form><br/>
<% session.setAttribute("user" , "abc");
// 下面三行代码添加Cookie
Cookie c = new Cookie("name" , "yeeku");
c.setMaxAge(24 * 3600);
response.addCookie(c);
%>
<table border="1" width="660" bgcolor="#aaaadd">
<tr>
<td width="170"><b>功能</b></td>
<td width="200"><b>表达式语言</b></td>
<td width="300"><b>计算结果</b></td>
<tr>
<!-- 使用两种方式获取请求参数值 -->
<td>取得请求参数值</td>
<td>\${param.name}</td>
<td>${param.name} </td>
</tr>
<tr>
<td>取得请求参数值</td>
<td>\${param["name"]}</td>
<td>${param["name"]} </td>
</tr>
<tr>
<!-- 使用两种方式获取指定请求头信息 -->
<td>取得请求头的值</td>
<td>\${header.host}</td>
<td>${header.host}</td>
</tr>
<tr>
<td>取得请求头的值</td>
<td>\${header["accept"]}</td>
<td>${header["accept"]}</td>
</tr>
<!-- 获取Web应用的初始化参数值 -->
<tr>
<td>取得初始化参数值</td>
<td>\${initParam["author"]}</td>
<td>${initParam["author"]}</td>
</tr>
<!-- 获取session返回的属性值 -->
<tr>
<td>取得session的属性值</td>
<td>\${sessionScope["user"]}</td>
<td>${sessionScope["user"]}</td>
</tr>
<!-- 获取指定Cookie的值 -->
<tr>
<td>取得指定Cookie的值</td>
<td>\${cookie["name"].value}</td>
<td>${cookie["name"].value}</td>
</tr>
</table>
</body>
</html>
3.表达式语言的自定义函数
自定义函数的开发步骤:
1)开发函数处理类
函数处理类,此类里有许多静态方法,每个静态方法都可以用于定义成一个函数。
实际上这个步骤也是可以省略的——完全可以使用JDK或其他项目提供的类,只要这个类包含静态方法即可。
public class Func {
public static int countChar(String str){
System.out.println("param-------------------"+str);
return str.length();
}
}
2)使用标签库定义函数
在tag文件 <taglib.../> 元素下增加<tag.../>元素用于定义自定义标签,增加<function.../>元素则用于定义自定义函数,每个 <function.../> 元素只要三个子元素即可。
name:指定自定义函数的函数名
function-class:指定自定义函数的处理类。
function-signature:指定自定义函数对应的方法。
mytaglib.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
web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<!-- 定义该标签库的URI -->
<uri>http://www.wanhao.com/tags</uri>
<!-- 定义第一个函数 -->
<function>
<!-- 定义函数名:reverse -->
<name>countChar</name>
<!-- 定义函数的处理类 -->
<function-class>com.wanhao.Func</function-class>
<!-- 定义函数的实现方法 -->
<function-signature>java.lang.Integer countChar(java.lang.String)
</function-signature>
</function>
</taglib>
3.使用时,在jsp中的el中使用函数,先导入标签库,然后使用
导入标签库两种方式,①uri为tld中配置的uri ②tld文件的相对路径,推荐后者
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="/WEB-INF/mytaglib.tld" prefix="wanhao"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Insert title here</title>
</head>
<body>
${wanhao:countChar(param["username"]) }
${wanhao:countChar("zhangsan") }
</body>
</html>
其中param表示链接中传入参数,name为param[]中字符串,如username,还有一种是直接参数,直接写在函数的括号中。