JSP:
1. 指令
2. 注释
3. 内置对象
MVC开发模式
EL表达式
JSTL标签
三层架构
今日源码:
链接:https://pan.baidu.com/s/10NUQnyfXycPzNVaHpAU7UQ
提取码:6nnn
JSTL需要的JAR包
链接:https://pan.baidu.com/s/1BlvodzGL6vlXtyGRQZpgng
提取码:zzfd
变量名 | 真实类型 | 作用 |
---|---|---|
pageContext | PageContext | 当前页面共享数据,还可以获取其他八个内置对象 |
request | HttpServletRequest | 一次请求访问的多个资源(转发) |
session | HttpSession | 一次会话的多个请求间 |
application | ServletContext | 所有用户间共享数据 |
response | HttpServletResponse | 响应对象 |
page | Object | 当前页面(Servlet)的对象 this |
out | JspWriter | 输出对象,数据输出到页面上 |
config | ServletConfig | Servlet的配置对象 |
exception | Throwable | 异常对象 |
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>el运算符</title>
</head>
<body>
${
3>4}
\${
3>4} <%-- 忽略当前el表达式--%>
<hr>
<h3>算数运算符</h3>
${
3 + 4}<br>
${
3 / 4}<br>
${
3 div 4}<br>
${
3 % 4}<br>
${
3 mod 4}<br>
<h3>比较运算符</h3>
${
3 == 4}<br>
<h3>逻辑运算符</h3>
${
3>4 || 3<4}<br>
${
3>4 and 3<4}<br>
<h3>empty空运算符</h3>
<%
String str = "abc";
request.setAttribute("str",str);
List list = new ArrayList();
request.setAttribute("list",list);
%>
${
empty str}
${
empty list}
</body>
</html>
域名称 | 获取域 |
---|---|
pageScope | pageContext |
requestScope | request |
sessionScope | session |
applicationScope | application(ServletContext) |
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>el获取域中数据</title>
</head>
<body>
<%
//在域中存储数据
session.setAttribute("name","李四");
request.setAttribute("name","张三");
session.setAttribute("age",23);
%>
<h3>el获取值</h3>
${
requestScope.name}
${
sessionScope.age}
${
sessionScope.haha}
${
name}
${
sessionScope.name}
</body>
</html>
${键名}:表示依次从最小的域中查找是否有该键对应的值,直到找到为止。
获取对象、List集合、Map集合的值
<%@ page import="cn.wbslz.domain.User" %>
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>获取对象数据</title>
</head>
<body>
<%
User user = new User();
user.setName("张三");
user.setAge(23);
user.setBirthday(new Date());
request.setAttribute("u",user);
List list = new ArrayList();
list.add("aaa");
list.add("bbb");
list.add(user);
request.setAttribute("list",list);
Map map = new HashMap();
map.put("sname","李四");
map.put("gender","男");
map.put("user",user);
request.setAttribute("map",map);
%>
<h3>获取对象的值</h3>
${
requestScope.u}<br>
<%--
通过的是对象的属性来获取
setter或getter,去掉set或get,在将剩余部分首字母变小写
setName -->Name -->name
--%>
${
requestScope.u.name}<br>
${
requestScope.u.age}<br>
${
requestScope.u.birthday}<br>
${
requestScope.u.birthday.month}<br>
${
u.birStr}<br>
<h3>获取list的值</h3>
${
list}<br>
${
list[0]}<br>
${
list[1]}<br>
${
list[10]}<br>
${
list[2].name}<br>
<h3>获取map的值</h3>
${
map.gender}<br>
${
map["gender"]}<br>
${
map.user.name}<br>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>隐式对象</title>
</head>
<body>
${
pageContext.request}<br>
<h4>在jsp页面动态获取虚拟目录</h4>
${
pageContext.request.contextPath}<br>
</body>
</html>
用于简化和替换jsp页面上的java代码
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>if标签</title>
</head>
<body>
<c:if test="true">
<h1>我是真...</h1>
</c:if>
<br>
<%
//判断request域中的一个list集合是否为空,如果不为null则显示遍历集合
List list = new ArrayList();
list.add("aaaa");
request.setAttribute("list",list);
request.setAttribute("number",4);
%>
<c:if test="${not empty list}">
遍历集合...
</c:if>
<br>
<c:if test="${number % 2 != 0}">
${
number}为奇数
</c:if>
<c:if test="${number % 2 == 0}">
${
number}为偶数
</c:if>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>choose标签</title>
</head>
<body>
<%
request.setAttribute("number",51);
%>
<c:choose>
<c:when test="${number == 1}">星期一</c:when>
<c:when test="${number == 2}">星期二</c:when>
<c:when test="${number == 3}">星期三</c:when>
<c:when test="${number == 4}">星期四</c:when>
<c:when test="${number == 5}">星期五</c:when>
<c:when test="${number == 6}">星期六</c:when>
<c:when test="${number == 7}">星期天</c:when>
<c:otherwise>数字输入有误</c:otherwise>
</c:choose>
</body>
</html>
完成重复的操作
for(int i = 0; i < 10; i ++){}
* 属性:
begin:开始值
end:结束值
var:临时变量
step:步长
varStatus:循环状态对象
index:容器中元素的索引,从0开始
count:循环次数,从1开始
遍历容器
List list;
for(User user : list){
}
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>foreach标签</title>
</head>
<body>
<%--
foreach:相当于java代码的for语句
--%>
<c:forEach begin="1" end="10" var="i" step="2" varStatus="s">
${
i} <h3>${
s.index}</h3> <h4> ${
s.count} </h4><br>
</c:forEach>
<hr>
<%
List list = new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
request.setAttribute("list",list);
%>
<c:forEach items="${list}" var="str" varStatus="s">
${
s.index} ${
s.count} ${
str}<br>
</c:forEach>
</body>
</html>
示例代码
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="cn.wbslz.domain.User" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>test</title>
</head>
<body>
<%
List list = new ArrayList();
list.add(new User("张三",23,new Date()));
list.add(new User("李四",24,new Date()));
list.add(new User("王五",25,new Date()));
request.setAttribute("list",list);
%>
<table border="1" width="500" align="center">
<tr>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>生日</td>
</tr>
<%-- 数据行--%>
<c:forEach items="${list}" var="user" varStatus="s">
<c:if test="${s.count % 2 ==0}">
<tr bgcolor="#7fffd4">
<td>${
s.count}</td>
<td>${
user.name}</td>
<td>${
user.age}</td>
<td>${
user.birStr}</td>
</tr>
</c:if>
<c:if test="${s.count % 2 !=0}">
<tr bgcolor="#7fff00">
<td>${
s.count}</td>
<td>${
user.name}</td>
<td>${
user.age}</td>
<td>${
user.birStr}</td>
</tr>
</c:if>
</c:forEach>
</table>
</body>
</html>
([点开我的主页系统的学习java])