首页 > 其他 > 详细

el表达式实例

时间:2014-03-26 19:21:36      阅读:283      评论:0      收藏:0      [点我收藏+]
package bean;

public class Person {
	public String name;
	public int age;
	public Address address;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

}
package bean;

public class Address {
	public String name="aa我是地址啊";
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="bean.*,java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>el表达式</title>
</head>
<body>
	<%
		request.setAttribute("name", "aaa");
	%>
	<!-- pageContext.findAttribute("name")   page request session application-->
	${name }
	<!-- 在jsp页面中,使用el表达式可以获取bean的属性 -->
	<%
		Person p = new Person();
		p.setAge(12);
		request.setAttribute("person", p);
	%>
	${person.age }

	<!-- 在jsp页面中,使用el表达式可以获取bean中的。。。。。。。。。的属性 -->
	<%
		Person person = new Person();
		Address address = new Address();
		person.setAddress(address);

		request.setAttribute("person", person);
	%>
	${person.address.name }
	<!-- 在jsp页面中,使用el表达式获取list集合中指定位置的数据 -->
	<%
		Person p1 = new Person();
		p1.setName("aa111");
		Person p2 = new Person();
		p2.setName("bb");
		List list = new ArrayList();
		list.add(p1);
		list.add(p2);
		request.setAttribute("list", list);
	%>
	${list[0].name }
	<!-- 在jsp页面中,使用el表达式获取map集合的数据 -->
	<%
		Map map = new HashMap();
		map.put("a", "aaaaxxx");
		map.put("b", "bbbb");
		map.put("c", "cccc");
		map.put("1", "aaaa1111");
		request.setAttribute("map", map);
	%>

	${map.a } ${map["1"] }
	<!-- 利用el表达式获取web应用的名称 -->
	<a href="${pageContext.request.contextPath }/index.jsp">点点</a>
	<!-- 注意:如果访问bean不存在的属性,会抛 Property ‘username‘ not found on type cn.itcast.Person -->
	<%-- ${person.username }--%>
</body>
</html>


el表达式实例,布布扣,bubuko.com

el表达式实例

原文:http://blog.csdn.net/jkxqj/article/details/22176391

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!