JSP&&EL表示式
阿新 • • 發佈:2019-01-09
1EL表示式
2.1簡介
EL表示式替代jsp表示式,因為開發jsp頁面的時候遵守原則:在jsp頁面中儘量少寫甚至不寫java程式碼。 EL表示式作用: 向瀏覽器輸出域物件中的變數或表示式計算結果 基本語法${ 變數或表示式} 代替<%=變數或表示式%> 1)EL從四個域中自動選擇 pageContext.setAttribute("name",name); $[name]等價於 <%=pageContext.findAttribute("name")%> 2)EL從指定的域中獲取 pageScope:page域 requestScope: request域 sessionScope: session域 applicationScope:application域 指定域獲取的EL: $[requestScope.name]; 2.2EL獲取不同型別的資料 1)普通的字串 2)普通的物件 EL:表示式的屬性表示呼叫了物件的getXXX方法,例如 <% Student student=new Student("eric","1234"); pageContext.setAttribute("student',student); %> student.name呼叫了student物件的getName()方法 ${student} 物件的屬性: ${student.name} 陣列或者是集合List集合 <% Student[] student=new Student[3]; student[0]=new student("1",‘123456“); student[0]=new student("1",‘123456“); student[0]=new student("1",‘123456“); pageContext.setAttribute(stus",stus); %> 陣列:${student[0].name} List集合 <% List<Student> list=new ArrayList<Student>(); list.add(new student("1",‘123456“)); list.add(new student("1",‘123456“)); list.add(new student("1",‘123456“)); pageContext.setAttribute(list",list); %> ${ list[0].name] $list{list.password} Map集合 <% Map<String,Student> map=new HashMap<String,Student>(); map.put("001",(new student("1",‘123456“)); map.put("001",(new student("1",‘123456“)); map.put("001",(new student("1",‘123456“)); pageContext.setAttribute(lmap",map); %> ${map['001'].name} 2.3執行表示式 算術表示式 <% int a=2; int b=3; pageContext.setAttribute(la"a); pageContext.setAttribute("b",b); %> ${a+b} ${a*b} ${a/b} ${a-b} 比較表示式 ${a>b} ${a==b} 邏輯表示式 ${true&&true} 判空表示式 empty null:==null; 空字串;==""; null或空字串 ${name==null || name==""} ${empty name}既可以判斷null也可以判斷字串空 2.4EL的11個內建物件 pageContext 等價於 jsp的pageContext內建物件 <%=pageContext.getRequest()%> 拿到上下文路徑 ${pageContext.request.contextPath} pageScope 從指定的域中獲取資料 requestScope sessionScope applocationScope param 獲取引數 <%=request.getParameter("name")%> 注意:param返回所有引數Map集合 ${param['name ']} paramValues <%=request.getParameterValues("name")[0]%> ${paramValues['name'][0]} header 獲取請求頭 <%=request.getHeader("host")%> ${header['host']} headerValues <%=request.getHeaders("host").nextElement()]%> ${headerValues['host'][0]} cookie <%=request.getCookies()[0].getValue()%> ${cookie['JSESSIONID'].name} ${cookie['JSESSIONID'].value} initParam獲取全域性引數 <%=applocation.getInitParameter(’AAA')%> ${initParam['AAA']}