`
wj603201
  • 浏览: 62275 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

JSP中使用EL调用集合

    博客分类:
  • JSP
阅读更多
JSP中使用EL调用集合
在JSP中使用EL调用集合时,可以将集合中的每一个元素逐一设置到pageContext属性中,再使用EL调用。

实例:

El.java:
view plaincopy to clipboardprint?
1. package mgc.el.bean.test;  
2.  
3. public class El {  
4.  
5.     private String name;  
6.       
7.     public void setName(String name) {  
8.           
9.         this.name=name;  
10.     }  
11.       
12.     public String getName() {  
13.           
14.         return this.name;  
15.     }  
16. } 


ListServlet.java:
view plaincopy to clipboardprint?
1. package mgc.el.servlet.test;  
2.  
3. import java.util.*;  
4. import java.io.*;  
5. import javax.servlet.*;  
6. import javax.servlet.http.*;  
7. import mgc.el.bean.test.*;  
8.  
9. public class ListServlet extends HttpServlet {  
10.       
11.     public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {  
12.           
13.         this.doPost(request,response);  
14.     }  
15.       
16.     public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {  
17.           
18.         List l=new ArrayList();  
19.         El e=null;  
20.         e=new El();  
21.         e.setName("Magci");  
22.         l.add(e);  
23.         e=new El();  
24.         e.setName("J2EE");  
25.         l.add(e);  
26.         e=new El();  
27.         e.setName("Magci's BLOG");  
28.         l.add(e);  
29.         request.setAttribute("list", l);  
30.         request.getRequestDispatcher("el05.jsp").forward(request, response);  
31.     }  
32. } 


web.xml:
view plaincopy to clipboardprint?
1.   <servlet> 
2.     <servlet-name>ListServlet</servlet-name> 
3.     <servlet-class>mgc.el.servlet.test.ListServlet</servlet-class> 
4.   </servlet> 
5.  
6.   <servlet-mapping> 
7.     <servlet-name>ListServlet</servlet-name> 
8.     <url-pattern>/el/list.mgc</url-pattern> 
9.   </servlet-mapping> 


el05.jsp:
view plaincopy to clipboardprint?
1. <%@page contentType="text/html;charset=GB2312" %> 
2. <%@page import="java.util.*" %> 
3. <html> 
4.   <head> 
5.     <title>el05</title> 
6.   </head> 
7.     
8.   <body> 
9. <%  
10.     List l=(List)request.getAttribute("list");  
11.     Iterator iter=l.iterator();  
12.     while(iter.hasNext()) {  
13.       
14.         //将集合中的每一个元素逐一设置到pageContext属性中  
15.         pageContext.setAttribute("iter",iter.next());  
16. %> 
17.         <h1>${iter.name }</h1> 
18. <%  
19.     }  
20. %> 
21.   </body> 
22. </html> 


分享到:
评论
1 楼 762236694 2011-11-10  
请问,如果不用JSP小脚本用EL怎么调用呢??

相关推荐

Global site tag (gtag.js) - Google Analytics