JSP2.0中的表达式语言(EL表达式)
来源:互联网 发布:好用的面膜推荐知乎 编辑:程序博客网 时间:2024/06/05 17:23
例:
${sessionScope.user.sex}
所有EL都是以 ${ 为起始、以} 为结尾的。
上述EL范例的意思是:从Session取得用户的性别。如果使用之前JSP代码的写法如下:
<%
User user = (User)session.getAttribute("user");
String sex = user.getSex( );
%>
两者相比较之下,可以发现EL的语法比传统JSP代码更为方便、简洁。
${sessionScope.user.sex}
等于
${sessionScope.user["sex"]}
${sessionScope.shoppingCart[0].price}
返回结果为shoppingCart中第一项物品的价格。
= = 或 eq | 等于 |${ 5 = = 5 } 或 ${ 5 eq 5 } | true
!= 或 ne | 不等于 |${ 5 != 5 } 或 ${ 5 ne 5 } | false
< 或 lt | 小于 |${ 3 < 5 }或 ${ 3 lt 5 } | true
> 或 gt | 大于 |${ 3 > 5 }或 ${ 3 gt 5 } | false
<= 或 le | 小于等于 |${ 3 <= 5 }或 ${ 3 le 5 } | true
>= 或 ge | 大于等于 |${ 3 >= 5 }或 ${ 3 ge 5 } | false
${ empty param.name }
接下来说明Empty运算符的规则:
{empty} A
如果A为null时,返回true
如果A不存在时,返回true
如果A为空字符串时,返回true
如果A为空数组时,返回true
如果A为空的Map时,返回true
如果A为空的Collection时,返回true
否则,返回false
在使用EL关系运算符时,不能够写成:
${param.password1} = = ${param.password2}
或者
${ ${param.password1 } = = ${ param.password2 } }
而应写成
${ param.password1 = = param.password2 }
request.getParameter(String name)
request.getParameterValues(String name)
${param.name}
${paramValues.name}可以取得所有同名参数的值
${paramValues.hobbies[0]}可以通过指定下标来访问特定的参数的值
<name>reverse</name><!--函数名-->
<function-class>jsp2.examples.el.Functions</function-class><!--函数所在的类-->
<function-signature>java.lang.String reverse( java.lang.String )</function-signature>
<!--函数原型,也就是函数的返回值类型,函数名,参数表,注意一定要写类型的全名-->
</function>
${sn:upper('abc')}
Page | pageScope
Request | requestScope
Session | sessionScope
Application | applicationScope
${requestScope.user}
等价于
<%request.getAttribute("user")%>
如果不写出特定的范围 ,那就会在不同的范围间进行搜索了
例:{user}(user是在request范围 request.setAttribute("user",user))
也就等于
${requestScope.user}
<%request.getAttribute("user")%>
EL的隐含对象
PageContext | javax.servlet.ServletContext |表示此JSP的PageContext
-----------------------------------------------------------------------------------
PageScope | java.util.Map |取得Page范围的属性名称所对应的值
-------------------------------------------------------------------------------------
RequestScope | java.util.Map |取得Request范围的属性名称所对应的值
-------------------------------------------------------------------------------------
sessionScope | java.util.Map |取得Session范围的属性名称所对应的值
-------------------------------------------------------------------------------------
applicationScope | java.util.Map |取得Application范围的属称所对应的值
-------------------------------------------------------------------------------------
param | java.util.Map |如同ServletRequest.getParameter(String | |name)返回String类型的值
----------------------------------------------------------------------------------------
paramValues | java.util.Map |如同ServletRequest.getParameterValues | |(String name)。返回String []类型的值
-----------------------------------------------------------------------------------------
header | java.util.Map |如同ServletRequest.getHeader(String name) | |返回String类型的值
------------------------------------------------------------------------------------------
headerValues | java.util.Map |如同ServletRequest.getHeaders(String name) | |。返回String []类型的值
------------------------------------------------------------------------------------------
cookie | java.util.Map |如同HttpServletRequest.getCookies( )
------------------------------------------------------------------------------------------
initParam | java.util.Map |如同ServletContext.getInitParameter(String | |name)。返回String类型的值
-------------------------------------------------------------------------------------------
注意:因为User-Agent中包含“-”这个特殊字符,所以必须使用“[]”,而不能写成${header.User-Agent}。
<web-app 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-app_2_4.xsd"
version="2.4">
<context-param>
<param-name>userid</param-name>
<param-value>mike</param-value>
</context-param>
</web-app>
那么我们就可以直接使用 ${initParam.userid}来取得名称为userid,其值为mike的参数。下面是之前的做法:String userid = (String)application.getInitParameter("userid");
${pageContext.request} |取得请求对象
${pageContext.session} |取得session对象
${pageContext.request.queryString} |取得请求的参数字符串
${pageContext.request.requestURL} |取得请求的URL,但不包括请求之参数字符串
${pageContext.request.contextPath} |服务的web application的名称
${pageContext.request.method} |取得HTTP的方法(GET、POST)
${pageContext.request.protocol} |取得使用的协议(HTTP/1.1、HTTP/1.0)
${pageContext.request.remoteUser} |取得用户名称
${pageContext.request.remoteAddr } |取得用户的IP地址
${pageContext.session.new} |判断session是否为新的,所谓新的session,表示刚由 server产生而client尚未使用
${pageContext.session.id} |取得session的ID
${pageContext.servletContext.serverInfo}|取得主机端的服务信息
JSTL(JSP标准标签库)
Core
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set> 设置属性
<c:remove> 移除设置的属性
例:
<c:if test="${!(empty user.age)}">
<h1>hello</h1>
</c:if>
<c:choose>choose和when是组合在一起使用的,有点类似于swith case的语法 。
<c:when test="...">when也是条件判断标签,test属性的值为true是才会执行标签体。
例:
<c:choose>
<c:when test="${param.age<18}">
<h1>you is a child<h1>
</c:when>
<c:when test="${param.age>18 and param.age<50 }">
<h1>you is a young person</h1>
</c:when>
<c:when test="${param.age>50}">
<h1>you is a old person</h1>
</c:when>
</c:choose>
例:
<c:forEach var="book" item="${store.books}" varStatus="status">
<h1>${book.parice}</h1>
</c:forEach>
<c:forEach begin="1" end="5" step="1">
<h1>hello</h1>
</c:forEach>
<c:import url="引入内容的url" var="别名">
${别名}
<c:url value="...">
<c:param name="..." value="..."/>
</c:url>
<c:url value="...">
使用url标签可以实现URL回写
<fmt:lauguage>
<fmt:bundel>资源指定标签
<fmt:message>消息标签
例:
<fmt:setLocale value="zh"/>
<fmt:bundel basename="message.MessageResources">
<fmt:message>name</fmt:message>
</fmt:bundel>
name=/0060/0700/
1 在jsp中计算两个数的乘积
例子:
${price}
${number}
可以在jsp中计算两个数的乘积:${price*number}
2 if..else的应用
1) struts2不支持EL表达式,所以要用到JSTL(JSP标准标签库),在用JSTL时需要在JSP头加:<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
如:<c:if text="${status}==0" >
显视满足if条件的内容
</c:if>
2) struts2中的if..else语句的表达方式
<s:if test="#name == 'Max'">
Max's file here
</s:if>
<s:elseif test="#name == 'Scott'">
Scott's file here
</s:elseif>
<s:else>
Other's file here
</s:else>
3) struts2支持OGNL表达式,OGNL 相当于EL表达式,但据说性能没有EL表达式好
一个例子:
Struts2.0
1.遍历Map
Java代码
<s:iterator value="#request.map" status="idx">
<tr>
<td > <input type="checkbox" id=" <s:property value="#idx.index"/>" value="checkbox" onclick="checkParentBox(' <s:property value="#idx.index"/>', <s:property value="value.size"/>);"/> </td>
<td > <s:property value="key.resName"/> </td>
<td >
<s:iterator value="value" id="perm" status="idxp">
<input type="checkbox" <s:if test="#perm.permId in #request.permList">checked="checked" </s:if> id=" <s:property value="#idx.index"/>_ <s:property value="#idxp.index"/>" name="permIds" value=" <s:property value="#perm.permId"/>" onclick="checkSubbox(' <s:property value="#idx.index"/>', <s:property value="value.size"/>)"/> <s:property value="#perm.permName"/>
</s:iterator>
</td>
</tr>
</s:iterator>
<s:iterator value="#request.map" status="idx">
<tr>
<td > <input type="checkbox" id=" <s:property value="#idx.index"/>" value="checkbox" onclick="checkParentBox(' <s:property value="#idx.index"/>', <s:property value="value.size"/>);"/> </td>
<td > <s:property value="key.resName"/> </td>
<td >
<s:iterator value="value" id="perm" status="idxp">
<input type="checkbox" <s:if test="#perm.permId in #request.permList">checked="checked" </s:if> id=" <s:property value="#idx.index"/>_ <s:property value="#idxp.index"/>" name="permIds" value=" <s:property value="#perm.permId"/>" onclick="checkSubbox(' <s:property value="#idx.index"/>', <s:property value="value.size"/>)"/> <s:property value="#perm.permName"/>
</s:iterator>
</td>
</tr>
</s:iterator>
2.遍历LIST
Java代码
<s:iterator value="#request.page.data" status="sta">
lt;tr>
<td> <input type="checkbox" name="ids" value=' <s:property value="equipmentId" />' onclick="checkState('f1',this)"/> </td>
<td > <s:property value="equipmentNo"/> </a> </td>
<td > <a href="${ctx}/resource/equip!detail.action?ids= <s:property value="equipmentId"/>"> <s:property value="equipmentName"/> </a> </td>
<td>
<c:if test="${borrowState eq 0 }">未借 </c:if>
<c:if test="${borrowState eq 1 }">已借 </c:if>
</td>
<td>
<c:if test="${useState eq 0 }">完好 </c:if>
<c:if test="${useState eq 1 }">报修 </c:if>
<c:if test="${useState eq 2 }">报废 </c:if>
</td>
</tr>
</s:iterator>
- JSP2.0中的表达式语言(EL表达式)
- JSP2.0 EL表达式详解
- JSP2.0表达式语言
- 在JSP2.0中使用EL表达式语言
- 在JSP2.0中使用EL表达式语言
- 在JSP2.0中使用EL表达式语言
- 在JSP2.0中使用EL表达式语言
- EL(表达式语言 ) jsp2.0新增的功能
- jsp2 特性 表达式语言
- JSP2 之表达式语言
- JSP2.0 EL语言
- EL表达式 jsp2.0 jsp1.2 与 tomcat
- resin 4 jsp2.0 不能解析JSTL的EL表达式
- JSP2.0 表达式
- 表达式语言和JSTL -JSP2.0应用教程学习笔记
- jsp相关-jsp2.0表达式
- java中的el——表达式语言
- EL(JSP中的表达式语言)
- vbscript/vb6 中实现异步请求
- SQLServer和Oracle的常用函数对比
- 关于验证码的相关文章
- platform builder的Catalog中项目前的符号的意义解读
- 关于ELF文件格式的实验
- JSP2.0中的表达式语言(EL表达式)
- Tomcat6/conf目录下配置文件中各元素浅析
- 统计数值,如果隔行则算一行
- 是或否 思维导图
- ASP.NET跨页面传值技巧总结
- 寄存器
- 【总结】oracle恢复误删除数据,解除锁定的等sql语句
- GridView数据的导入导出
- PHP 初识