JSTL小结

网上有很多内容讲解,这里只是提一些这次用的的小点,重点关于EL的内容。

EL的几个内置变量[1]
1.默认变量pageScope、requestScope、sessionScope、applicationScope
      这4个默认变量包含Scope作用范围的参数集合,相当于被保存在java.util.Map中的某个参数。下面看简单的示例9.2:
例9.2:使用sessionScope变量的EL表达式
<%request.getSession().setAttribute("sampleValue", new Integer(10));%>
${sessionScope.sampleValue}
取得保存在Session中参数的sessionScope变量的EL表达式,“.”是property访问操作符,在这里表示从Session中取得“键”为“sampleValue”的参数,并显示出来。显示结果为“10”。
例如,${requestScope.status}便可获得servlet中通过request.setAttribute()的属性
2.默认变量param、paramValues
      这两个默认变量包含请求参数的集合,param表明请求包含的参数为单一控件,paramValues表明请求包含的参数为控件数组。下面看一个简单示例9.3:
例9.3:提交请求的页面和接受的页面
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
     <form action="SampleJsp.jsp">
    <input type="text" name="sampleValue" value="10">
    <input type="text" name="sampleValue" value="11">
    <input type="text" name="sampleValue" value="12">
    <input type="text" name="sampleSingleValue" value="SingleValue">
    <input type="submit" value="Submit">
    </form>
</body>
</html>
      在这个页面中定义了两组控件,控件名为“sampleValue”的是一套控件数组,控件名为“sampleSingleValue”的是单一控件,通过递交将请求参数传送到SampleJsp.jsp。
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
    ${paramValues.sampleValue[2]}
    ${param.sampleSingleValue}
</body>
</html>
      这是请求转发到的页面,通过EL表达式的paramValues变量得到控件数组中最后一个控件的递交参数,通过EL表达式的param变量得到单一控件的递交参数。控件数组参数的EL表达式使用“[]”来指定数组下标。本示例将显示控件数组中最后一个控件的值“12”和单一控件的值“SingleValue”。
3.默认变量header、headerValues
      这两个默认变量包含请求参数头部信息的集合,header变量表示单一头部信息,headerValues则表示数组型的头部信息。
4.默认变量cookie
      包含所有请求的cookie集合,集合中的每个对象对应javax.servlet.http.Cookie。
比如,这次项目中使用到${cookie.TL_CN.value}从cookie获取登录的用户名
5.默认变量initParam
      包含所有应用程序初始化参数的集合。
6.默认变量pageContext
      等价于page环境类javax.servlet.jsp.PageContext的实例,用来提供访问不同的请求参数。
11个默认变量几乎包含了Web应用的所有基本操作,若一个表达式不使用这些变量而直接使用参数名,那么就采用就近原则。该表达式将使用最近取得的参数值。

三个必须区别的运算符[2]
The [  ] Operator:
The restriction with the EL dot ( . ) operator is that it works only when the value on its right side is either a bean property or a map key for the value on the left. The [ ] operator on the other hand provides much flexibility as by using it we can have a List or an Array in addition to Map and Bean on the left side. So thing on the right or rather inside the [ ] operator can be - A key to some Map, a bean property or an index into some array or list that is on the left side of the operator.
Example : Using [ ] operator
Let we set an array as an attribute in a servlet as -
.......... String [ ] bigFive  = {"U.S.A", "Russia", "France", "China","U.K"};
   request.setAtribute{"bigFive", bigFive};
Now in a JSP having access to scope, we can write - First one is ${bigFive[0]} and that will print U.S.A
() - Used to change the precedence of operators.

[1]很全面,EL表达式和标签库介绍,几乎每个知识点都有例子,文本模式
http://www.javawind.net/help/html/jstl_el.htm
[2]重点对EL失效情况、EL运算符讲解,条理清晰
http://www.roseindia.net/jstl/jstl-el.shtml
[3]官方网站,大牛必读,貌似很卡
http://jstl.java.net/

猜你喜欢

转载自lingceng.iteye.com/blog/1697037
今日推荐