EL表达式(一)

来源:互联网 发布:建行网络贷款怎么过 编辑:程序博客网 时间:2024/05/19 00:36


EL (ExpressionLanguage)

1) El 表达式的格式:

 ${data} 它的转义后的代码 pageContext.findAttribute(“data”);  这句代码会在jsp的4个域中找data这个变量(pageContext、requestContext、SessionContext、aplicationContext 都找不找返回 “”)

 

<% Person p = new Person();

     p.setName("this is a test"); 

     request.setAttribute("person", p); 

 

     %>

${person}

 

2) El表达式 获取对象的属性:

${person} 对应的是Person对象成员name的getName属性。

 

3)request.setAttribute("person“, p); 这段代码表明

 Request 域设置了一个名字为person的属性 ${person} 获取的值是先在pageContext查找是否有 包含属性person 再查找 request ,找到了属性person所以可以说当前表达式${person} 在 request域中获取了person属性的值。

 

4)获取域中存的list集合的

     List list = new ArrayList();

     list.add(new Person("aaa"));

     list.add(new Person("bbb"));

${list["0"].name}   //[]用于获取数组对象

 

5)

     Map map = new HashMap();

     Map.put("aa",newPerson("aaa"));

     Map.put("bb",newPerson("aaa"));

     Request.setAttribute("mapList",map);

 

${mapList.bb.name}//获取到map的关键值bb对应的对象,再获取到对应对象的成员 name 的属性 getName()的值

 

6)几个常用el表达式

获取当前web应用的路径:

${pageContext.request.contextPath}

<a href="${pageContext.request.contextPath}/index.jsp">点击</a>

 

 

 

 

7)判断是否为空

<%

     request.setAttribute("list",null);

 %>

${empty(list) } //检查对象是否为空 返回值为boolean

 

8)El表达式的 < 二元运算符 >也可以运用在数据回显上

<%

 request.setAttribute("gender","female");

%>

<inputtype="radio"name="gender"value="male"${gender=='male'?'checked':""}>

<inputtype="radio"name="gender"value="female"${gender=='female'?'checked':""}>

 

//不带EL标签的写法:

<inputtype="radio"name="gender"value="male"checked >

 

 

                                 ----笔记根据黑马老师方立勋JavaWeb开发视频整理

 

 

 

 

 

 

 

 

 

 

 

    

 

 

 

 

 

 

 

 

 

0 0
原创粉丝点击