EL表达式和JSTL标签的用法举例

Person类

package cn.itcast.domain;

import java.util.Date;

public class Person {

	private String name;
	private Address address;

	public Person() {
	}

	public Person(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

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

	public Address getAddress() {
		return address;
	}

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

Address类:

package cn.itcast.domain;

public class Address {

	private String city;

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

}

Servlet要么通过域带过来,对象带过来,集合带过来,所以,EL表达式取出元素的几种方式:

<%@page import="java.util.*"%>
<%@page import="cn.itcast.domain.*"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
		String data = "abcd";
		request.setAttribute("data", data);
	%>
	${data }
	<%--pageContext.findAttribute("data") page request session application --%>
	<br />

	<%
		Person p = new Person();
		p.setName("aaaa");
		request.setAttribute("person", p);
	%>
	${person.name }
	<%--pageContext.findAttribute("person") page request session application --%>
	<br />

	<%
		Person p1 = new Person();
		Address a = new Address();
		a.setCity("成都");
		p1.setAddress(a);
		request.setAttribute("p1", p1);
	%>
	${p1.address.city }
	<br />

	<%
		List<Person> list = new ArrayList<Person>();
		list.add(new Person("aaa"));
		list.add(new Person("bbb"));
		list.add(new Person("ccc"));

		request.setAttribute("list", list);
	%>
	${list[1].name }
	<br />

	<%
		Map<String, Person> map = new HashMap<String, Person>();
		map.put("aa", new Person("aaaaa"));
		map.put("bb", new Person("bbbbb"));
		map.put("cc", new Person("ccccc"));
		map.put("dd", new Person("ddddd"));
		map.put("11", new Person("eeeee"));//一般不以数字作为关键字,若要这么做那么EL表达式的写法为${map['11'].name}

		request.setAttribute("map", map);
	%>
	<!-- 以关键字去找集合的元素 -->
	${map }
	<!-- 找到集合 -->
	<br /> ${map.aa }
	<!-- 用关键字找到对象 -->
	<br /> ${map.bb.name }
	<!-- 找到对象的name -->
	<br /> ${map['11'].name }
	<!-- 用EL表达式取数据一般是用.号,.号取不出数据时用[] -->
	<!-- Servlet要么通过域带过来,对象带过来,集合带过来 -->

	<br />${pageContext.request.contextPath }
	<!-- 获取当前Web应用的名称 -->
	<br /><a href="/Web/index.jsp" />点击</a>	<!-- 一般不能这样写 -->
	
	<a href="${pageContext.request.contextPath }/index.jsp">点击</a>
</body>
</html>

JSTL标签遍历集合列表:

<%@page import="java.util.*"%>
<%@page import="cn.itcast.domain.Person"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL标签遍历集合列表</title>
</head>
<body>
<%
		List<Person> list = new ArrayList<Person>();
		list.add(new Person("aaa"));
		list.add(new Person("bbb"));
		list.add(new Person("ccc"));

		request.setAttribute("list", list);//保存到request域中
	%>
	<br />
	<c:forEach var="person" items="${list }">
		${person.name }<br />
	</c:forEach>
	
	<%
		Map<String, Person> map = new HashMap<String, Person>();
		map.put("aa", new Person("aaaaa"));
		map.put("bb", new Person("bbbbb"));
		map.put("cc", new Person("ccccc"));
		map.put("dd", new Person("ddddd"));
		map.put("11", new Person("eeeee"));//一般不以数字作为关键字,若要这么做那么EL表达式的写法为${map['11'].name}

		request.setAttribute("map", map);
	%>
	<c:forEach var="entry" items="${map }">
		${entry.key }:${entry.value.name }<br />
	</c:forEach>
	
	<!-- 代表用户登录 -->
	<c:if test="${user!=null }">
		欢迎您:${user.username }
	</c:if>
	<c:if test="${user==null }">
		用户名:<input type="text" name="username"><br />
		密   码:<input type="password" name="password"><br />
		<input type="submit" value="登录" />
	</c:if>
</body>
</html>


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。