本文主要是介绍EL表达式、Jstl标签,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
获取域值、全域查找、表达式
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ page import="com.itheima.domain.*" %>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body><!-- 模拟域中的数据 --><%pageContext.setAttribute("company", "ssss");//request存储字符串request.setAttribute("company", "程序员");//session存储一个对象User user = new User();user.setId(1);user.setName("zhangsan");user.setPassword("123");session.setAttribute("user", user);//application存储一个集合List<User> list = new ArrayList<User>();User user1 = new User();user1.setId(2);user1.setName("lisi");user1.setPassword("123");list.add(user1);User user2 = new User();user2.setId(3);user2.setName("wangwu");user2.setPassword("123");list.add(user2);application.setAttribute("list", list);%><!-- 脚本法是取出域中的值 --><%=request.getAttribute("company") %><%User sessionUser = (User)session.getAttribute("user");out.write(sessionUser.getName());%><hr/><!-- 使用EL表达式获得域中的值 -->${requestScope.company }${sessionScope.user.name }${applicationScope.list[1].name}<!-- 使用el表达式 全域查找 -->${company }${user.name }${list[1].name}<!-- el可以执行表达式运算 -->${1+1 }${1==1?true:false }<!-- empty 判定某个对象是否是null 是null返回true -->${empty list}</body>
</html>输出如下:
程序员 zhangsan
--------------------------------------------------------------------------------
程序员 zhangsan wangwu ssss zhangsan wangwu 2 true false
${pageContext.request.contextPath}
<form action="${pageContext.request.contextPath }/el/form2.jsp" method="post"><input type="text" name="username"><br><input type="password" name="password"><br><input type="checkbox" name="hobby" value="zq">足球<input type="checkbox" name="hobby" value="pq">排球<input type="checkbox" name="hobby" value="ppq">乒乓球<br><input type="submit" value="提交"><br></form><!-- 通过el表达式获得request对象 -->${pageContext.request }输出如下:
org.apache.catalina.connector.RequestFacade@7d9d2615<%=pageContext.getRequest().getContextPath%> 这句代码不能实现
获得WEB应用的名称
jstl
<body><%request.setAttribute("count", 10);%><!-- jstl标签经常会和el配合使用 --><!-- test代表的返回boolean的表达式 --><c:if test="${count==10 }">xxxx</c:if><c:forEach begin="0" end="5" var="i">${i }<br/></c:forEach><!-- items:一个集合或数组 var:代表集合中的某一个元素--><c:forEach items="${productList }" var="pro">${pro.pname }</c:forEach></body>输出
xxxx 0
1
2
3
4
5
c:if
<!-- 用户没有登录 --><c:if test="${empty user}"><li><a href="login.jsp">登录</a></li><li><a href="register.jsp">注册</a></li></c:if><!-- 用户已经登录 --><c:if test="${!empty user}"><li>${user.name }</li><li><a href="#">提出</a></li></c:if>模拟登陆<%//模拟用户已经登录成功User user = new User();user.setId(100);user.setName("张三");user.setPassword("123");session.setAttribute("user", user);%>
c:forEach
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="com.itheima.domain.*" %>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body><%//request模拟List<String> strListList<String> strList = new ArrayList<String>();strList.add("itcast");strList.add("itheima");strList.add("boxuegu");strList.add("shandingyu");request.setAttribute("strList", strList);//application遍历List<User>的值List<User> userList = new ArrayList<User>();User user1 = new User();user1.setId(2);user1.setName("lisi");user1.setPassword("123");userList.add(user1);User user2 = new User();user2.setId(3);user2.setName("wangwu");user2.setPassword("123");userList.add(user2);application.setAttribute("userList", userList);//session遍历Map<String,String>的值Map<String,String> strMap = new HashMap<String,String>();strMap.put("name", "lucy");strMap.put("age", "18");strMap.put("addr", "西三旗");strMap.put("email", "licy@itcast.cn");session.setAttribute("strMap", strMap);//request遍历Map<String,User>的值Map<String,User> userMap = new HashMap<String,User>();userMap.put("user1", user1);userMap.put("user2", user2);request.setAttribute("userMap", userMap);%><h1>取出strList的数据</h1><c:forEach items="${strList }" var="str">${str }<br/></c:forEach><h1>取出userList的数据</h1><c:forEach items="${userList}" var="user">user的name:${user.name }------user的password:${user.password }<br/></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.key }:${entry.value.name }--${entry.value.password }<br/></c:forEach></body>
</html>输出:
取出strList的数据
itcast
itheima
boxuegu
shandingyu取出userList的数据
user的name:lisi------user的password:123
user的name:wangwu------user的password:123取出strMap的数据
name====lucy
addr====西三旗
age====18
email====licy@itcast.cn取出userMap的数据
user1:lisi--123
user2:wangwu--123
这篇关于EL表达式、Jstl标签的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!