表达式语言(EL)可以方便的访问标志位(JSP中有四种标志位 :page(pageContext), request, session, application)中的属性,可以避免许多scriptlet代码。
在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>
显示结果:<h3><%=request.getParameter("ref") %></h3>
<h3>${param.ref }</h3>也可以通过${paramValues.参数名称}
集合操作
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>
结果如下:
349



被折叠的 条评论
为什么被折叠?



