el和jstl的简单使用

el

el一共有十一个内置对象

    1、pageScope,requestScope,sessionScope,applicationScope。(这四个对象基本差不都,我就一起写了。下面会对这四个对象仔细介绍所以这里就不详细说了)
    2、param,paramValues 接受参数(相当于request.getParameter()    request.getParameterValues())
    3、header.headerValues 获取请求头信息 (相当于request.getHander(name))
    4、initParam 获取全局初始化参数(相当于 this.getServletContext().getInitParmeter(name))
    5、cookie web开发中cookie (相当于 request.getCookies()  cookie.getName() cookie.getValue() )
    6、pageContext WEB开发中的pageContext pageContext 可以获取其它八大对象

因为现在的el主要用来帮助jsp更好的显示页面,所以它最有用的就是上述第一类的对象。主要用el从四大域中来取出数据,下面我将用代码来展示取数据的过程。
注:因为要取出数据,就必须先有数据,为了代码的简洁我就直接在jsp中存储一些数据
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 导入相对应的包,el.*是User的位置 -->
<%@ page import="el.*" %>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<!-- 模拟域中的数据 -->
	<%
		//存储字符串
		
		request.setAttribute("aaa", "bbb");
		pageContext.setAttribute("aaa", "222");
		//存储对象
		User user1 = new User();
		user1.setId(1);
		user1.setName("张三");
		user1.setPassword("123");
		session.setAttribute("user",user1);
		//存储集合
		List<User> list = new ArrayList<User>();
		User user2 = new User();
		user2.setId(2);
		user2.setName("李四");
		user2.setPassword("123");
		User user3 = new User();
		user3.setId(3);
		user3.setName("王五");
		user3.setPassword("123");
		list.add(user2);
		list.add(user3);
		application.setAttribute("list", list);
	%>
	<!-- 脚本取出域中的值 -->
	<%=request.getAttribute("aaa") %>
	<%
		User user = (User)session.getAttribute("user");
		out.write(user.getName());
	%>
	<hr/>
	<!-- 使用el表达式获取域中的值 -->
	${requestScope.aaa }
	${sessionScope.user.name }
	${applicationScope.list[1].name}	
	
	<hr/>
	<!-- 依次从pageContext,request,session,application来找 -->
	${aaa }
	${user.name }
	${list[1].name }
	<!-- 动态获取项目名称 -->
	${pageContext.request.contextPath }
	
	<!-- el可以执行表达式运算 -->
	${1+1 }
	${1 == 1?true:false }
	<!-- 判断某个对象是否是空的 如果是返回true-->
	${empty user}
	
</body>
</html>




jstl


jstl和el的本质是一样的,都是为了让jsp更好的去展示页面效果。
jstl这里主要介绍它的两个最常用的标签就是“if”和“for”,一般是和el从域中取数据的功能配合使用。(因为jstl里面的if和for的功能和一般的if,for是一样的能力但是写法有点不一样下面会介绍)
注:在jsp里面使用jstl的时候需要加入jstl相对应的jar包并且在jsp中导入,关于这个可以留言我会加你qq给你jar包
下面我就先简单的使用以下if和for
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 导入jstl的jar包 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>
	<!-- test代表的返回boolean的表达式 -->
	<c:if test="${1 == 1}">
		xxxx
	</c:if>
	<c:if test="1!=1">
		yyyy
	</c:if>
	<%
		request.setAttribute("count", 0);
	%>
	<c:if test="${requestScope.count == 0}">
		aaaa
	</c:if>
	<c:if test="${requestScope.count == 10}">
		bbbb
	</c:if>
	
	<!-- forEach模拟 
		for(iny i = 0;i < 5; i++) {
			syso(i);
		}
	-->
	<c:forEach begin="0" end="5" var="i">
		${i } <br/>
	</c:forEach>
	<!-- 模拟增强for
		for(Product product : productList) {
			syso(product.getPname());
		}
	 -->
	 <!-- items:一个集合或数组  var:代表集合中的某一个元素-->
	 <c:forEach items="${productList }" var="product">
	 	${product.pname }
	 </c:forEach>
	
	
	
</body>
</html>

因为jstl的for循环遍历有点发杂这里特地写一个关于它的遍历,希望对你们有帮助

<%@page import="javax.swing.text.TabableView"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="el.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
	<%
		//模拟List<String>
		List<String> string = new ArrayList<String>();
		string.add("111");
		string.add("222");
		string.add("333");
		request.setAttribute("strList", string);
		
		//遍历List<User>的值
		List<User> userList = new ArrayList<User>();
		User user1 = new User();
		user1.setId(1);
		user1.setName("1");
		user1.setPassword("1");
		userList.add(user1);
		User user2 = new User();
		user2.setId(2);
		user2.setName("2");
		user2.setPassword("2");
		userList.add(user2);
		application.setAttribute("userList", userList);
		
		//遍历Map<String,String>的值
		Map<String,String> strMap = new HashMap<String,String>();
		strMap.put("name","3");
		strMap.put("age","33");
		strMap.put("addr","333");
		session.setAttribute("strMap", strMap);
		
		//遍历Map<String,User>的值
		Map<String,User> UserMap = new HashMap<String,User>();
		UserMap.put("user1",user1);
		UserMap.put("user2",user2);
		session.setAttribute("UserMap", UserMap);
		
	%>
	<h1>取出List的数据</h1>
	<c:forEach items="${strList }" var="str">
		${str } <br/>
	</c:forEach>
	
	<h1>取出userList的数据</h1>
	<c:forEach items="${userList }" var="user">
	
		<!--全部遍历 user的name:${user.name }<br/> -->
		<!-- 选择遍历 -->
		<c:if test="${user.name == '2' }">
			${user.password }
		</c:if>
	</c:forEach>
	
	<h1>取出strMap的数据</h1>
	<c:forEach items="${strMap }" var="entry">
		${entry.key }   ${entry.value }<br/>
	</c:forEach>
	
	<h1>取出UserMap的数据</h1>
	<c:forEach items="${UserMap }" var="entry">
		${entry.value.name }<br/>
	</c:forEach>
</body>
</html>



猜你喜欢

转载自blog.csdn.net/tomwildboar/article/details/79996987