现在的位置: 首页 > 综合 > 正文

JSP基础语法之十五:EL表达式

2013年05月20日 ⁄ 综合 ⁄ 共 1449字 ⁄ 字号 评论关闭

一:输出原理

<!-- 1.${属性键值},输出属性省去null判断-->
<h2>${info}</h2

上边的输出相当于下边这一大堆的表达式

<%
	String takeOut = (String)request.getAttribute("info");
	if(null != takeOut)
	{
%>
		<h2><%=takeOut%></h2>		
<%
	}
%>

二:EL表达式内置对象

<!-- 2.EL表达式内置对象:属性范围 -->
<%
	pageContext.setAttribute("info","pageContext属性");
	request.setAttribute("info","request属性");
	session.setAttribute("info","session属性");
	application.setAttribute("info","application属性");
%>
<h3>pageScope: ${pageScope.info}</h3> <!-- 不指定的话,输出优先级:page > req > session > app -->
<h3>requestScope: ${requestScope.info}</h3>
<h3>sessionScope: ${sessionScope.info}</h3>
<h3>applicationScope: ${applicationScope.info}</h3>


<!-- 3.内置对象pageContext: -->
<h3>IP地址: ${pageContext.request.remoteAddr}</h3>
<h3>SessionID: ${pageContext.session.id}</h3>

<!-- 4.内置对象param:接收参数 --><!-- 地址栏添加 ? = 测试-->
<h3>通过EL获得参数: ${param.p1}</h3>
<h3>通过Scriptlet获得参数: <%=request.getParameter("p1")%> </h3>

<h3>EL获得一组参数_0: ${paramValues.p2[0]}</h3>
<h3>EL获得一组参数_1: ${paramValues.p2[1]}</h3>
<h3>EL获得一组参数_2: ${paramValues.p2[2]}</h3>

三:EL表达式中的集合操作

<!-- EL集合操作 -->
<%
	List all = new ArrayList();
	all.add("1st");
	all.add("2nd");
	request.setAttribute("list",all);
%>
<h3>EL获得List _0: ${list[0]}</h3>
<h3>EL获得List _1: ${list[1]}</h3>

<%
	Map map = new HashMap();
	map.put("1st","first");
	map.put("2nd","second");
	request.setAttribute("map",map);
%>
<h3>EL获得Map _0: ${map["1st"]}</h3>
<h3>EL获得Map _1: ${map["2nd"]}</h3>

四:EL表达式运算符操作

<!-- 2.EL运算符 -->
<!-- 常见的+-*/ mod等都支持 -->
<%
	request.setAttribute("num1",10);
	request.setAttribute("num2",20);
%>
<h3>EL运算符: ${num1>num2 ? num1 : num2}</h3>

抱歉!评论已关闭.