EL表达式

来源:互联网 发布:靓邦素化妆品淘宝 编辑:程序博客网 时间:2024/06/06 13:21

1.使用EL表达式改写乘法口诀实例。

<%@ page Xlanguage="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'a9.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<%@ page contentType="text/html; charset=GBK" %>

<body >
<form>
<table border="1">
<%for(int i=1;i<=9;i++){%>
<tr>
<%pageContext.setAttribute("i",String.valueOf(i));%>
<%for(int j=1;j<=9;j++){%>
<%pageContext.setAttribute("j",String.valueOf(j));%>
<td><${pageScope.i * pageScope.j}</td>
<%}%>
</tr>
<%}%>
</table>
</form>
</body>
</html>

2.写一个实例,使用EL表达式分别获取 某个 web 域 中的对象,访问 javabean 的属性、访问 list 集合、访问 map 集合、访问数组

<%@ page Xlanguage="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.hbsi.el.*" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>a7</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->


</head>

<body>
<%
String data ="abcd";
request.setAttribute("data1",data);
%>
${data1 }
<br/>
<%
UserBean user = new UserBean();
user.setName("zhangsan");
request.setAttribute("user1",user);
%>
${user1.name }
<br/>
<%
UserBean user2 = new UserBean();
Address add = new Address();
add.setCity("北京");
user2.setAddress(add);
request.setAttribute("user2",user2);
%>
${user2.address.city }
<br/>
<%
List list = new ArrayList();
list.add(new UserBean("aaaa"));
list.add(new UserBean("bbbb"));
list.add(new UserBean("cccc"));
request.setAttribute("list",list);
%>
${list[1].name }
<br/>
<%
Map map = new HashMap();
map.put("a",new UserBean("aaaa"));
map.put("b",new UserBean("bbbb"));
map.put("c",new UserBean("cccc"));
map.put("d",new UserBean("dddd"));
request.setAttribute("map1",map);
%>
${map1["d"].name }
</body>
</html>

原创粉丝点击