<%@page import="domain.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="domain.User" %>
<%@page import="java.util.*" %>
<%@ 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>
<%--!!!!!if中的test和foreach中的items的值要用el来取,且双引号中不能有空格!!!!! --%>
<%--jstl配合el做判断 --%>
<%
User user = new User();
user.setUsername("testUsername");
request.setAttribute("user", user);
%>
<c:if test="${requestScope.user == null }">
不存在
</c:if>
<c:if test="${requestScope.user != null }">
存在
<br>${user.username }
</c:if>
<%--类似普通for循环 --%>
<c:forEach begin="0" end="5" var="i">
${i }
</c:forEach>
<%
List<String> list = new ArrayList<>();
list.add("str1");
list.add("str2");
list.add("str3");
pageContext.setAttribute("list", list);
%>
<%--遍历List<String>的值 --%>
<c:forEach items="${list }" var="str">
<p>${str }</p>
</c:forEach>
<%--遍历List<User>的值 --%>
<%
User user2 = new User();
user2.setUsername("testUsername2");
List<User> userList = new ArrayList<>();
userList.add(user);
userList.add(user2);
pageContext.setAttribute("userList", userList);
%>
<c:forEach items="${userList }" var="user">
<p>${user.username }</p>
</c:forEach>
<%--遍历Map<String,String>的值 --%>
<%
Map<String,String> map = new HashMap<>();
map.put("mapKey1", "mapValue1");
map.put("mapKey2", "mapValue2");
map.put("mapKey3", "mapValue3");
map.put("mapKey4", "mapValue4");
pageContext.setAttribute("strMap", map);
%>
<c:forEach items="${strMap }" var="map">
${map.key } == ${map.value }<br>
</c:forEach>
<%--遍历Map<String,User>的值 --%>
<%
Map<String,User> userMap = new HashMap<>();
userMap.put("usermapKey1", user);
userMap.put("usermapKey2", user2);
pageContext.setAttribute("userMap", userMap);
%>
<c:forEach items="${userMap }" var="usermap">
${usermap.key } : ${usermap.value.username }<br>
</c:forEach>
</body>
</html>
JAVA-WEB 动态页面技术(JSP/EL/JSTL)--- jstl代码演示
最新推荐文章于 2019-09-11 12:48:25 发布