ch10.表达式语言
来源:互联网 发布:前锦网络信息技术校招 编辑:程序博客网 时间:2024/04/29 16:18
表达式语言(EL)可以方便的访问标志位(JSP中有四种标志位 :page(pageContext), request, session, application)中的属性,可以避免许多scriptlet代码。
在JSP中,只有三种代码可以出现:
- 接收属性
- 判断语句
- 迭代输出
但是开发中这点很难做到,因为JSP的输出要接收VO对象,所以,要通过表达式来避免导入vo包
表达式语言
四种标识为就是四种属性范围,可以直接通过表达式语言访问:
语法格式:
${属性名称}
表达式语言的主要功能就是进行内容的显示,通过不同内置对象的设置,表达式语言可以输出不同的内容:
内置对象的定义:
本图来自《Java Web 开发实战经典 --基础篇》视频
如果在不同的属性范围中保存了同名属性,则EL的查找范围就是 page --> request ---> session--->application,这是就可以通过内置对象区分四种属性
代码如下:ELdemo.jsp
<%@ page contentType = "text/html" pageEncoding="GBK"%><html> <head> <title>My JSP 'ELdemo.jsp' starting page</title> </head> <body> <% pageContext.setAttribute("info", "page属性"); request.setAttribute("info", "request属性") ; session.setAttribute("info", "session属性"); application.setAttribute("info", "application属性"); %> <h3>${pageScope.info}</h3> <h3>${requestScope.info}</h3> <h3>${sessionScope.info }</h3> <h3>${applicationScope.info }</h3> </body></html>显示结果:
接收请求参数:
这个功能与request.getParameter()类似,语法如下:
${param.参数名称}
<h3><%=request.getParameter("ref") %></h3><h3>${param.ref }</h3>
也可以通过${paramValues.参数名称}
接收一组参数
集合操作
集合有 Collection(List, Set), Map, Iterator
所有的集合都要通过Iterator方法进行输出,而且List和Set的本质区别在于List接口对Collection接口进行扩充,而Set接口并没有扩充
Map每次保存一对内容,而且都是用Map.Entry接口对象
在MVC中应用:
在:org.thystar.eldemo.vo包中写入Dept.java
package org.thystar.eldemo.vo;public class Dept {private int deptno;private String dname;private String loc;public int getDeptno() {return deptno;}public void setDeptno(int deptno) {this.deptno = deptno;}public String getDname() {return dname;}public void setDname(String dname) {this.dname = dname;}public String getLoc() {return loc;}public void setLoc(String loc) {this.loc = loc;}}
在:org.thystar.eldemo.servlet包中写入ELServlet.java
package org.thystar.eldemo.servlet;import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.thystar.eldemo.vo.Dept;public class ELServlet extends HttpServlet {/** * */private static final long serialVersionUID = 1L;/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {List<Dept> all = new ArrayList<Dept>();Dept dept = null;dept = new Dept();dept.setDeptno(1);dept.setDname("xxxx");dept.setLoc("yyyy");all.add(dept);dept = new Dept();dept.setDeptno(2);dept.setDname("xxxxxx");dept.setLoc("yyyyyyyy");all.add(dept);request.setAttribute("alldept", all);request.getRequestDispatcher("ELdemo.jsp").forward(request, response);}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doGet(request, response);}}
在:ELdemo.jsp中写入
<%@ page contentType = "text/html" pageEncoding="GBK" import = "java.util.*"%><html> <head> <title>My JSP 'ELdemo.jsp' starting page</title> </head> <body> <% List all = (List)request.getAttribute("alldept"); if(all != null){ %> <table border = "1" width = "90%"> <tr> <td>部门编号</td> <td>部门名称</td> <td>部门位置</td> </tr> <% Iterator iter = all.iterator(); while(iter.hasNext()){ pageContext.setAttribute("dept", iter.next()); %> <tr> <td>${dept.deptno }</td> <td>${dept.dname }</td> <td>${dept.loc }</td> </tr> <% } %> </table> <% } %> </body></html>
结果如下:
运算符:
简单的运算符可以用表达式完成,复杂的应该交给Servlet完成
主要用到的是三目运算符:
${ ? : }
0 0
- ch10.表达式语言
- PHP自学经典----CH10.正则表达式
- tangowithdjango ch10
- ch10 流控制
- California Mathmetics-G6-Ch10
- C++ Primer CH9 CH10
- Clean Code CH9 CH10
- Accelerated C++ Exercises Ch10
- CH10 面向对象思想
- CH10,日期与时间,
- 表达式语言
- 表达式语言
- 表达式语言
- 表达式语言
- 表达式语言
- 表达式语言
- 表达式语言
- 读RFC2326RTSP ch10方法定义
- iOS程序中调用系统自带应用(短信,邮件,浏览器,地图,appstore,拨打电话)
- ImageView重复使用的时候导致的OutOfMemoryError
- c++中友元的一些总结
- Xcode 快捷键大全
- LeetCode:Best Time to Buy and Sell Stock II
- ch10.表达式语言
- Oracle expdp/impdp的使用方法
- SQL Server 存储过程编写
- 正则表达式语法
- Java垃圾回收机制
- CloudStack系统虚拟机启动但不在Host表中
- OC--内存管理
- 有没有研究opennms的大神
- C++语言习题 用递归方法求 f(n)