1.什么是EL表達式?
EL(Expression Language) 是為了使JSP寫起來更加簡單。表達式語言的靈感來自於 ECMAScript 和 XPath 表達式語言,它提供了在 JSP 中簡化表達式的方法,讓Jsp的代碼更加簡化。
3.EL表達式的語法:
語法:${ EL表達式 }
4.EL表達式有哪些用途?
1.獲取數據
<h1>EL獲取數據</h1> <% pageContext.setAttribute("pname", "王守義"); request.setAttribute("rname", "王鳳兒"); session.setAttribute("sname", "王如花"); application.setAttribute("aname", "王芙蓉"); %> <h3>傳統方式</h3> <%= pageContext.getAttribute("pname")%> <%= request.getAttribute("rname")%> <%= session.getAttribute("sname")%> <%= application.getAttribute("aname")%> <h3>EL的方式</h3> ${ pageScope.pname } ${ requestScope.rname } ${ sessionScope.sname } ${ applicationScope.aname } <hr/> <% //pageContext.setAttribute("name", "王守義"); //request.setAttribute("name", "王鳳兒"); session.setAttribute("name", "王如花"); application.setAttribute("name", "王芙蓉"); %> ${ name } <h3>EL獲得數組的數據</h3> <% String[] arrs = {"王守義","王如花","王鳳兒"}; pageContext.setAttribute("arrs", arrs); %> ${ arrs[1] } <h3>EL獲得List集合的數據</h3> <% List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); pageContext.setAttribute("list", list); %> ${ list[1] } <h3>獲得Map集合的數據</h3> <% Map<String,String> map = new HashMap<String,String>(); map.put("aaa", "111"); map.put("bbb", "222"); map.put("ccc.ddd", "333"); pageContext.setAttribute("map", map); %> ${ map["ccc.ddd"] } <h3>EL獲得JavaBean中的數據</h3> <% Person person = new Person(); person.setId(1); person.setName("王美麗"); pageContext.setAttribute("person", person); %> ${ person.name }
2.EL執行運算
3.EL獲得WEB開發的常用的對象:(EL的內置對象)