以下的所有内容都是在MVC_${}项目下进行的

一、表达式的一般输出
Dept.jsva
 
  
  1. package org.ml.eldemo.vo;  
  2. public class Dept {  
  3. private int deptno;  
  4. private String dname;  
  5. private String loc;  
  6. public  int getDeptno() {  
  7. return deptno;  
  8. }  
  9. public void setDeptno( int deptno) {  
  10. this.deptno = deptno;  
  11. }  
  12. public String getDname() {  
  13. return dname;  
  14. }  
  15. public void setDname(String dname) {  
  16. this.dname = dname;  
  17. }  
  18. public String getLoc() {  
  19. return loc;  
  20. }  
  21. public void setLoc(String loc) {  
  22. this.loc = loc;  
  23. }  
print_vo.jsp
 
   
  1. <%@ page language="java" contentType="text/html; charset=GBK" 
  2.     pageEncoding="GBK"%> 
  3. <%@page import="org.ml.eldemo.vo.*" %> 
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  5. <html> 
  6. <head> 
  7. <meta http-equiv="Content-Type" content="text/html; charset=GBK"> 
  8. <title>Insert title here</title> 
  9. </head> 
  10. <body> 
  11. <%  
  12. Dept dept = new Dept();  
  13. dept.setDeptno(10);  
  14. dept.setDname("实践助学部");  
  15. dept.setLoc("贵州大学北区");  
  16. request.setAttribute("deptinfo",dept);  
  17. %> 
  18. <h3>部门编号:${deptinfo.deptno}</h3> 
  19. <h3>部门名称:${deptinfo.dname}</h3> 
  20. <h3>部门位置:${deptinfo.loc}</h3> 
  21. </body> 
  22. </html> 
执行上面的print_vo.jsp可以得到下面的结果:

 
二、表达式中使用MVC设计模式的形式输出参数
以上的操作同样也是使用反射机制的原理,但是同样需要在jsp同样要页面中导入相应的vo包,要想不导入包,那就得使用MVC设计模式才能发挥最好的效果。
ELServlet.java
package org.ml.servlet;  import java.io.IOException;   import javax.servlet.ServletException;  import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;   import org.ml.eldemo.vo.Dept;   public class ELServlet extends HttpServlet {  private static final long serialVersionUID = 1L;   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  Dept dept = new Dept();  dept.setDeptno(10);  dept.setDname("实践助学部");  dept.setLoc("贵州大学北区");  request.setAttribute("deptinfo",dept);  request.getRequestDispatcher("print_MVC_vo.jsp").forward(request, response);  }   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  this.doGet(request, response);  }   }  
print_MVC_vo.jsp
 
<%@ page language="java" contentType="text/html; charset=GBK"     pageEncoding="GBK"%>   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>Insert title here</title> </head> <body>   <h3>部门编号:${deptinfo.deptno}</h3> <h3>部门名称:${deptinfo.dname}</h3> <h3>部门位置:${deptinfo.loc}</h3> </body> </html> 
web.xml文件配置
 
   
  1. <servlet> 
  2.     <servlet-name>el</servlet-name> 
  3.     <servlet-class>   
  4. org.ml.servlet.ELServlet  
  5.     </servlet-class> 
  6. </servlet> 
  7. <servlet-mapping> 
  8.     <servlet-name>el</servlet-name> 
  9.     <url-pattern>/ELServlet</url-pattern> 
  10. </servlet-mapping> 
此时在地址栏中输入http://localhost:8080/MVC_${}/ELServlet,会得到下面的结果(结果中的地址栏看起来有不认识的码,是因为{}在页面运行时进行的编码【MVC_${}是工程名】):

 
三、表达式中使用MVC设计模式的形式输出集合参数
我们还知道集合的输出都必须要使用Iterator,使用El输出集合时由于EL只能操作属性范围,所以将集合通过Iterator找到每一个对象,之后将每个对象存放在page属性范围之中,然后在通过EL取得。
ELServlet_List.java
 
package org.ml.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.ml.eldemo.vo.Dept;   public class ELServlet_List extends HttpServlet {  private static final long serialVersionUID = 1L;   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  List<Dept> all = new ArrayList<Dept>();  Dept dept = null;  dept = new Dept();  dept.setDeptno(10);  dept.setDname("实践助学部");  dept.setLoc("贵州大学北校区");  all.add(dept);  dept = new Dept();  dept.setDeptno(10);  dept.setDname("纪律检查部");  dept.setLoc("贵州大学南校区");  all.add(dept);  dept = new Dept();  dept.setDeptno(10);  dept.setDname("信息中心");  dept.setLoc("贵州大学北校区");  all.add(dept);  request.setAttribute("alldept",all);  request.getRequestDispatcher("print_MVC_List_vo.jsp").forward(request, response);  }   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  this.doGet(request, response);  }  }  
print_MVC_List_vo.jsp
 
   
  1. <%@ page language="java" contentType="text/html; charset=GBK" 
  2.     pageEncoding="GBK"%>   
  3. <%@ page import="java.util.*" %> 
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  5. <html> 
  6. <head> 
  7. <meta http-equiv="Content-Type" content="text/html; charset=GBK"> 
  8. <title>Insert title here</title> 
  9. </head> 
  10. <body>   
  11. <%  
  12. List all = (List)request.getAttribute("alldept");  
  13. if(all!=null){  
  14. %> 
  15. <table border="2" align="center" width="50%"> 
  16.  <tr> 
  17.   <th>部门编号</th> 
  18.   <th>部门名称</th> 
  19.   <th>部门地址</th> 
  20.  </tr>    
  21. <%  
  22. Iterator iter = all.iterator();  
  23. while(iter.hasNext()){  
  24. pageContext.setAttribute("deptinfo",iter.next());  
  25. %> 
  26. <tr> 
  27. <td>${deptinfo.deptno}</td> 
  28. <td>${deptinfo.dname}</td> 
  29. <td>${deptinfo.loc}</td>   
  30. </tr> 
  31. <%   
  32. }  
  33. %>   
  34. </table> 
  35. <%  
  36. }  
  37. %>   
  38. </body> 
  39. </html> 
web.xml文件的配置如下:
    
 
   
  1. <servlet> 
  2.         <servlet-name>el_list</servlet-name> 
  3.         <servlet-class> 
  4. org.ml.servlet.ELServlet_List  
  5. </servlet-class> 
  6.     </servlet> 
  7.     <servlet-mapping> 
  8.        <servlet-name>el_list</servlet-name> 
  9.        <url-pattern>/ELServlet_List</url-pattern>      
  10.     </servlet-mapping> 
得到的页面结果如下: