EL表达式:
1、 普通字符串
<%=request.getAttribute(“hello”)%>
结果:直接输出hello world
2、 采用“ . ”进行导航,也称存取器
姓名:$ {user.username }
年龄:$ {user.age }
所属组:${user.group.name }
结果:输出属性名对应的属性值(EL表达式特性)
3、 输出map
${mapvalue.key1 }
结果:从map中拿到key为key1的属性值
4、 输出数组
${str[1] }
结果:取出属性名为str,数组下标为1的数组元素
5、 输出对象数组,采用[]和下标
${usersArray[2].username }
结果:输出数组中第二个元素的username属性值
6、 输出list,采用[]和下标
${userlist[4].username }
结果:输出集合中第四个username的属性值
7、 el表达式对运算符的支持
${1+2 }
${10/0 }<! – 发现异常,转换成实型运算 -->
${10 div 5 }
${10 % 3 }
${10 mod 3 }
${abcd+ 3 }
结果:输出结果与正常四则运算无异,而且EL表达式会自动处理异常
8、 测试empty
${empty value1 }
${empty value2 }
${empty value3 }
${empty value4 }
${!empty value4 }
结果:从scop中取出属性名为value1的值,如果为空输出true。不为空,则为false
测试类:
public class JstlElAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 普通字符串
request.setAttribute("hello", "hello world");
// 结构
Group group = new Group();
group.setName("zte");
User user = new User();
user.setUsername("张三");
user.setAge(18);
user.setGroup(group);
request.setAttribute("user", user);
// map
Map mapValue = new HashMap();
mapValue.put("key1", "value1");
mapValue.put("key2", "value2");
request.setAttribute("mapvalue", mapValue);
// 字符串数组
String[] strArray = null;
strArray = new String[] { "a", "b", "c" };
request.setAttribute("str", strArray);
User[] users = new User[10];
for (int i = 0; i < 10; i++) {
User u = new User();
u.setUsername("U_" + i);
users[i] = u;
}
request.setAttribute("usersArray", users);
List userList = new ArrayList();
for (int i = 0; i < 10; i++) {
User uu = new User();
uu.setUsername("UU_" + i);
userList.add(uu);
}
request.setAttribute("userlist", userList);
// empty
request.setAttribute("abcd", 6);
request.setAttribute("value1", null);
request.setAttribute("value2", "");
request.setAttribute("value3", new ArrayList());
request.setAttribute("value4", "123456");
return mapping.findForward("success");
}
}
**
• JSTL_ 标签库
**
一、 配置
将jstl.jar和standard.jar拷贝到WEB-INF/lib下
二、 导入
在jsp页面中采用taglib指令引入标签
例:
库核心标签库指令引入: <%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core”%>
格式化标签库指令引入: <%@ taglib prefix=“fmt” uri=“http://java.sun.com/jsp/jstl/fmt”%>
**
测试jstl核心库(见代码注释)
**
1、 直接输出(测试c:out)
hello(default):<c:out value="hello"/><br><!-- 直接输出hello -->
hello(default):<c:out value="${hello}"/><br><!-- jstl标签一定要接EL表达式 -->
hello(el表达式):${hello }<br>
hello(default="123"):<c:out value="${abcd}" default="123" /><br><!-- 如果没有找到abcd的值,用默认值替代 -->
hello(default="123"):<c:out value="${abcd}">123</c:out><br><!-- 输出了123 -->
bj(defalut):<c:out value="${bj}"/><br><!-- 不解析html标签 -->
bj(escapeXml="true"):<c:out value="${bj}" escapeXml="true"/><br><!-- 控制浏览器解析 -->
bj(escapeXml="false"):<c:out value="${bj}" escapeXml="false"/><br><!-- 控制浏览器不解析 -->
bj(el表达式):${bj }<br><!-- EL表达式会解析html标签 -->
2、 改变内置对象属性值(测试c:set和c:remove)
<c:set value="123" var="temp"/><!-- 直接往内置对象设值(这里默认page,可以换成scop=request) -->
temp:${temp }<br>
<c:remove var="temp"/><!-- 从内置对象中移除属性名为temp的值 -->
temp:${temp }<br>
3、 条件判断标签(测试条件控制标签c:if)
<c:if test="$ {v1 lt v2}" var="v"><! -- 从scop中取出v1,v2。比较V1<V2。(这里为true)-->
v1小于v2<br>v=$ {v }<br>
</c:if>
<c:if test="$ {empty v3}"var="v"><! -- 判断属性名为V3的值是否为空 -->
v3为空<br>v=$ {v }<br>
</c:if>
<c:if test="$ {empty v4}">
v4为空<br>
</c:if>
<c:if test="$ {empty v5}">
v5为空<br>
</c:if>
(将结果赋给var,结果为真则执行其后语句,否则不执行)
4、 条件控制标签(测试条件控制标签c:choose,c:when,c:otherwise)
<c:choose>
<c:when test="${v1 lt v2}"><!-- 如果V1<v2 -->
v1小于v2<br>
</c:when>
<c:otherwise>
v1大于v2<br>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${empty v4}">
v4为空<br>
</c:when>
<c:otherwise>
v4不为空<br>
</c:otherwise>
</c:choose>
注意:c:when,c:otherwise必须定义在choose中。
结果:根据结果选择输出when语句或者otherwise语句
5、循环控制标签c:forEach
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<c:choose>
<c:when test="${empty userlist}">
<tr>
<td colspan="3">没有符合条件的数据!</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach items="${userlist}" var="u">
<tr>
<td>${u.username }</td>
<td>${u.age }</td>
<td>${u.group.name }</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
注:这里先判断集合是否为空,不为空则用标签遍历所有属性到var对象“u”,之后用EL表达式输出“u”中属性名对应属性。
6、 循环控制标签c:forEach,varstatus
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<c:choose>
<c:when test="${empty userlist}">
<tr>
<td colspan="3">没有符合条件的数据!</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach items="${userlist}" var="user" varStatus="vs">
<c:choose>
<c:when test="${vs.count % 2 == 0}">
<tr bgcolor="red">
</c:when>
<c:otherwise>
<tr>
</c:otherwise>
</c:choose>
<td>
<c:out value="${user.username}"/>
</td>
<td>
<c:out value="${user.age}"/>
</td>
<td>
<c:out value="${user.group.name}"/>
</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
Varstatus:可以对特定的值进行输出控制,例子表明偶数用红色显示
7、 循环控制标签c:forEach,begin,end,step
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<c:choose>
<c:when test="${empty userlist}">
<tr>
<td colspan="3">没有符合条件的数据!</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach items="${userlist}" var="user" begin="2" end="8" step="2">
<tr>
<td>${user.username}</td>
<td>${user.age}</td>
<td>${user.group.name }</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
注:设定循环的起点begin,终点end,以及每次循环的间隔step(不设定默认为1)
8、循环控制标签c:forEach
<c:forEach begin="1" end="10">
${hello} <br>
</c:forEach>
结果:输出10个值
9、 循环控制标签c:forEach,输出map
<c:forEach items="$ {mapvalue}" var="v">
$ {v.key }=$ {v.value }<br>
</c:forEach>
注:将map对象赋给var,拿到map集合的entry,输出指定key的value
测试JSTL格式库
1、测试日期的格式化
${today}<br><!-- 输出日期 -->
today(default):<fmt:formatDate value="${today}"/><br>
today(type="date"):<fmt:formatDate value="${today}" type="date"/><br>
today(type="time"):<fmt:formatDate value="${today}" type="time"/><br>
today(type="both"):<fmt:formatDate value="${today}" type="both"/><br>
today(dateStyle="short"):<fmt:formatDate value="${today}" dateStyle="short"/><br>
today(dateStyle="medium"):<fmt:formatDate value="${today}" dateStyle="medium"/><br>
today(dateStyle="long"):<fmt:formatDate value="${today}" dateStyle="long"/><br>
today(dateStyle="full"):<fmt:formatDate value="${today}" dateStyle="full"/><br>
today(pattern="yyyy/MM/dd HH:mm:ss"):<fmt:formatDate value="${today}" pattern="yyyy/MM/dd HH:mm:ss"/><br><!-- 直接定义格式 -->
today(pattern="yyyy/MM/dd HH:mm:ss"):<fmt:formatDate value="${today}" pattern="yyyy/MM/dd HH:mm:ss" var="d"/><br>
.......${d }<br格式化value的值必><!-- 加了var则输出var才输出值 -->
2、测试数字的格式化
${n}<br><!-- 123456.123 -->
n(default):<fmt:formatNumber value="${n}"/><br><!-- 默认分位 -->
n(pattern="###,###.##"):<fmt:formatNumber value="${n}" pattern="###,###.##"/><br><!-- 定义格式 -->
n(pattern="###,###.0000"):<fmt:formatNumber value="${n}" pattern="###,###.0000"/><br><!-- 小鼠不足部分用0补齐 -->
n(groupingUsed="false"):<fmt:formatNumber value="${n}" groupingUsed="false"/><br> <!-- 默认不分位 -->
n(minIntegerDigits="10"):<fmt:formatNumber value="${n}" minIntegerDigits="10"/><br><!-- 最小整数为10位 -->
n(type="currency"):<fmt:formatNumber value="${n}" type="currency"/><br><!-- 以美元开头输出 -->
n(type="currency"):<fmt:formatNumber value="${n}" type="currency" currencySymbol="¥"/><br><!-- 以人民币符号开头输出 -->
n(type="percent"):<fmt:formatNumber value="${p}" type="percent"/><br><!-- 以百分号输出,默认输出 -->
n(type="percent"):<fmt:formatNumber value="${p}" type="percent" maxFractionDigits="2" minFractionDigits="2"/><br>
自定义 JSTL 函数标签库
第一步:自定义一个public类和static方法
public class MyFunctions {
/**
* 方法必须是public static
* @param name
* @return
*/
public static String sayHello(String name) {
return "Hello " + name;
}
}
第二步:写一个标签库描述符(tld)文件,在 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>my functions library</description>
<display-name>my functions</display-name>
<tlib-version>1.0</tlib-version>
<short-name>my</short-name><!-- 随便写 -->
<uri>http://www.zte.com/functions</uri><!-- 你的方法所在,通过它找tld -->
<function>
<name>say</name><!-- 标签名 -->
<function-class>com.bjsxt.struts.MyFunctions</function-class><!-- 调用该方法的类(包名+类名) -->
<function-signature>java.lang.String sayHello(java.lang.String)</function-signature><!-- 真正的函数名 -->
</function>
</taglib>
第三步:编写一个JSP实现Tag接口
<?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>my functions library</description>
<display-name>my functions</display-name>
<tlib-version>1.0</tlib-version>
<short-name>my</short-name><!-- 随便写 -->
<uri>http://www.zte.com/functions</uri><!-- 你的方法所在,通过它找tld -->
<function>
<name>say</name><!-- 标签名 -->
<function-class>com.bjsxt.struts.MyFunctions</function-class><!-- 调用该方法的类(包名+类名) -->
<function-signature>java.lang.String sayHello(java.lang.String)</function-signature><!-- 真正的函数名 -->
</function>
</taglib>