一:流程控制包含四个标签
<c:if>,<c:shoose>,<c:when>,<c:otherwise>
二:<c:if>
<c:set var="userName" value="张三"/>
<c:if test="${sessionScope.userName != null}" var="flag">
欢迎${sessionScope.userName}光临!
</c:if>
${flag}
三:<c:choose/>
<c:shoose>本身只当做<c:when>和<c:otherwise>的父标签
当使用<c:when>和<c:otherwise>做流程控制时,它们两者必须为</c:shoose>的子标签
<c:choose>
<c:when>
</c:when>
<c:when>
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>
when必须有test属性,当其运行结果为true时,则执行其中的内容,否则不会执行。
otherwise当其中所有条件没有成立时,则执行otherwise中的内容,它必须排在when之后
<c:set var="month" value="-12" />
<c:choose>
<c:when test="${month>0 && month<4}">
春
</c:when>
<c:when test="${month>3 && month<7}">
夏
</c:when>
<c:when test="${month>6 && month<10}">
秋
</c:when>
<c:when test="${month>9 && month<13}">
冬
</c:when>
<c:otherwise>
错误
</c:otherwise>
</c:choose>
四:循环<c:forEach/>
属性介绍:
var:存放集合中的元素的变量
items:要循环的集合
begin:开始的位置
end:结束的位置
step:增加的间隔
varStatus:存储特定信息
<%
List<String> list = null;
//List<String> list = new ArrayList<String>();
//list.add("aaa1");
//list.add("aaa2");
//list.add("aaa3");
session.setAttribute("list",list);
%>
<c:forEach items="${list}" var="str">
${str}<br/>
</c:forEach>
观察一下空指针异常,标签帮我们处理了,
<%
String[] nums = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
request.setAttribute("nums",nums);
%>
<c:forEach items="${nums}" var="num">
${num}<br />
</c:forEach>
记住:集合一定要在四个范围之一中存在
实例二:
<c:forEach items="${nums}" var="num" begin="2" end="3">
${num}<br />
</c:forEach>
实例三:
<c:forEach items="${nums}" var="num" step="2">
${num}<br />
</c:forEach>
//循环表格示例:
model层
package com.model;
public class User {
private int id;
private String name;
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
public User() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//servlet页面:
package com.action;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.model.User;
public class Chapter4Action extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
List<User> list = new ArrayList<User>();
list.add(new User(1,"张三"));
list.add(new User(2,"李四"));
list.add(new User(3,"王五"));
list.add(new User(4,"赵六"));
list.add(new User(5,"林奇"));
request.setAttribute("list", list);
request.getRequestDispatcher("chapter4.jsp").forward(request, response);
}
}
//jsp页面
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>chapter4.jsp</title>
</head>
<body>
<div>
<div>
<label>id</label>
<label>姓名</label>
</div>
<div>
<c:forEach items="${list}" var="user">
<label>${user.id}</label>
<label>${user.name}</label>
</c:forEach>
</div>
</div>
</body>
</html>