Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
jsp
EL expression
To simplify our jsp code, the specific point is to simplify those in jsp java code written inside.
- Written format
${表达式 }
If the value from the scope, it will first start taking small scope, if not, take down a scope. He has taken the four scopes are not finished, there is no display.
- how to use
- Remove four values stored in scope.
<%
pageContext.setAttribute("name", "page");
request.setAttribute("name", "request");
session.setAttribute("name", "session");
application.setAttribute("name", "application");
%>
按普通手段取值<br>
<%= pageContext.getAttribute("name")%>
<%= request.getAttribute("name")%>
<%= session.getAttribute("name")%>
<%= application.getAttribute("name")%>
<br>使用EL表达式取出作用域中的值<br>
${ pageScope.name }
${ requestScope.name }
${ sessionScope.name }
${ applicationScope.name }
- If the field is stored in an array
<%
String [] a = {"aa","bb","cc","dd"};
pageContext.setAttribute("array", a);
%>
使用EL表达式取出作用域中数组的值<br>
${array[0] } , ${array[1] },${array[2] },${array[3] }
- If the domain is the set of latches
<br>---------集合数据------------<br>
<%
List list=new ArrayList();
list.add("11");
list.add("12");
list.add("13");
list.add("14");
pageContext.setAttribute("li", list);
%>
使用EL表达式取出作用域中的数组的值<br>
${li[0] }, ${li[1] }, ${li[2] } ,${li[3] }
- Takes a value set Map
<%
Map map = new HashMap();
map.put("name", "zhangsna");
map.put("age",18);
map.put("address","北京..");
map.put("address.aa","深圳..");
pageContext.setAttribute("map", map);
%>
使用EL表达式取出作用域中Map的值<br>
${map.name } , ${map.age } , ${map.address } , ${map["address.aa"] }
Value details:
- The value from the domain. We have pre-existing value.
从域中取值。 得先存值。
<%
//pageContext.setAttribute("name", "zhangsan");
session.setAttribute("name", "lisi...");
%>
<br>直接指定说了,到这个作用域里面去找这个name<br>
${ pageScope.name }
<br>//先从page里面找,没有去request找,去session,去application <br>
${ name }
<br>指定从session中取值<br>
${ sessionScope.name }
- Value ways
If this value is under the target, then the direct use []
<%
String [] array = {"aa","bb","cc"}
session.setAttribute("array",array);
%>
${ array[1] } --> 这里array说的是attribute的name
If there is no index, used directly. The way to get
<%
User user = new User("zhangsan",18);
session.setAttribute("u", user);
%>
${ u.name } , ${ u.age }
EL generally used expression, the more used, it is removed from an object attribute value, such as a student's name removed.
EL expressions of the 11 built-in objects.
$ {Object name. Members}
- pageContext
Scope related objects
- pageScope
- requestScope
- sessionScope
- applicationScope
Header information related objects
- header
- headerValues
Parameter information related objects
-
param
-
paramValues
-
cookie
global initialization parameters -
Initfrm