以下的所有内容都是在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
  1. package org.ml.servlet;  
  2. import java.io.IOException;   
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.HttpServlet;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;   
  7. import org.ml.eldemo.vo.Dept;   
  8. public class ELServlet extends HttpServlet {  
  9. private static final long serialVersionUID = 1L;   
  10. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  11. Dept dept = new Dept();  
  12. dept.setDeptno(10);  
  13. dept.setDname("实践助学部");  
  14. dept.setLoc("贵州大学北区");  
  15. request.setAttribute("deptinfo",dept);  
  16. request.getRequestDispatcher("print_MVC_vo.jsp").forward(request, response);  
  17. }   
  18. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  19. this.doGet(request, response);  
  20. }   
  21. }  
print_MVC_vo.jsp
 
  1. <%@ page language="java" contentType="text/html; charset=GBK" 
  2.     pageEncoding="GBK"%>   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  4. <html> 
  5. <head> 
  6. <meta http-equiv="Content-Type" content="text/html; charset=GBK"> 
  7. <title>Insert title here</title> 
  8. </head> 
  9. <body>   
  10. <h3>部门编号:${deptinfo.deptno}</h3> 
  11. <h3>部门名称:${deptinfo.dname}</h3> 
  12. <h3>部门位置:${deptinfo.loc}</h3> 
  13. </body> 
  14. </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
 
  1. package org.ml.servlet;    
  2. import java.io.IOException;   
  3. import java.util.ArrayList;  
  4. import java.util.List;   
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;   
  9. import org.ml.eldemo.vo.Dept;   
  10. public class ELServlet_List extends HttpServlet {  
  11. private static final long serialVersionUID = 1L;   
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  13. List<Dept> all = new ArrayList<Dept>();  
  14. Dept dept = null;  
  15. dept = new Dept();  
  16. dept.setDeptno(10);  
  17. dept.setDname("实践助学部");  
  18. dept.setLoc("贵州大学北校区");  
  19. all.add(dept);  
  20. dept = new Dept();  
  21. dept.setDeptno(10);  
  22. dept.setDname("纪律检查部");  
  23. dept.setLoc("贵州大学南校区");  
  24. all.add(dept);  
  25. dept = new Dept();  
  26. dept.setDeptno(10);  
  27. dept.setDname("信息中心");  
  28. dept.setLoc("贵州大学北校区");  
  29. all.add(dept);  
  30. request.setAttribute("alldept",all);  
  31. request.getRequestDispatcher("print_MVC_List_vo.jsp").forward(request, response);  
  32. }   
  33. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  34. this.doGet(request, response);  
  35. }  
  36. }  
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> 
得到的页面结果如下: