以下的所有内容都是在MVC_${}项目下进行的
一、表达式的一般输出
Dept.jsva
- package org.ml.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;
- }
- }
print_vo.jsp
- <%@ page language="java" contentType="text/html; charset=GBK"
- pageEncoding="GBK"%>
- <%@page import="org.ml.eldemo.vo.*" %>
- <!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>
- <%
- Dept dept = new Dept();
- dept.setDeptno(10);
- dept.setDname("实践助学部");
- dept.setLoc("贵州大学北区");
- request.setAttribute("deptinfo",dept);
- %>
- <h3>部门编号:${deptinfo.deptno}</h3>
- <h3>部门名称:${deptinfo.dname}</h3>
- <h3>部门位置:${deptinfo.loc}</h3>
- </body>
- </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文件配置
- <servlet>
- <servlet-name>el</servlet-name>
- <servlet-class>
- org.ml.servlet.ELServlet
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>el</servlet-name>
- <url-pattern>/ELServlet</url-pattern>
- </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
- <%@ page language="java" contentType="text/html; charset=GBK"
- pageEncoding="GBK"%>
- <%@ page import="java.util.*" %>
- <!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>
- <%
- List all = (List)request.getAttribute("alldept");
- if(all!=null){
- %>
- <table border="2" align="center" width="50%">
- <tr>
- <th>部门编号</th>
- <th>部门名称</th>
- <th>部门地址</th>
- </tr>
- <%
- Iterator iter = all.iterator();
- while(iter.hasNext()){
- pageContext.setAttribute("deptinfo",iter.next());
- %>
- <tr>
- <td>${deptinfo.deptno}</td>
- <td>${deptinfo.dname}</td>
- <td>${deptinfo.loc}</td>
- </tr>
- <%
- }
- %>
- </table>
- <%
- }
- %>
- </body>
- </html>
web.xml文件的配置如下:
- <servlet>
- <servlet-name>el_list</servlet-name>
- <servlet-class>
- org.ml.servlet.ELServlet_List
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>el_list</servlet-name>
- <url-pattern>/ELServlet_List</url-pattern>
- </servlet-mapping>
得到的页面结果如下:
转载于:https://blog.51cto.com/2838221/792992