JSP 和 JSTL
jsp
注释:
1) // 注释,单⾏注释 /* 多⾏注释*/
2)<!-- HTML⻛格的注释 -->
3)<%-- JSP注释 --%>
Scriptlet:
Scriptlet的作用是可以在jsp中写java代码:
第⼀种:<% %>: java 脚本段,可以定义局部变量、编写语句
第⼆种:<%! %>:声明,可以定义全局(成员)变量、⽅法、类
第三种:<%= %>:表达式,数据⼀个变量或具体内容
实例:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Scriptlet</title>
</head>
<body>
<%
String str = "Hello JSP";
System.out.println(str);
response.getWriter().write(str);
%>
<%!
String memberStr = "a member String";
%>
<%=memberStr%>
<h1>This is a JSP page!!</h1>
</body>
</html>
JSP的指令标签:
用法是为了 JSP ⻚⾯(HTML)都包含⼯具栏、头部信息、尾部信息、具体内容拼接在一起,防止冗余。
include 静态包含:
<%@ include file="要包含的⽂件路径" %> <!-- 相对路径 -->
include 动态包含:
<jsp:include page="include.html"></jsp:include>
<%
String a = "hello.jsp";
%>
<jsp:include page="<%=a %>"></jsp:include>
第一个页面:
<jsp:include page="hello.jsp" flush="true">
<jsp:param name="uname" value="zhangsan"/>
</jsp:include>
第二个页面:
<!-- 接收参数 -->
<%=request.getParameter("uname")%>
EL表达式的使⽤
语法结构⾮常简单: ${expression}
EL表达式的使⽤
<%
pageContext.setAttribute("uname","zhangsan"); // page作⽤域
request.setAttribute("uname","lisi"); // request作⽤域
session.setAttribute("uname","wangwu"); // session作⽤域
application.setAttribute("uname","zaholiu"); // application
%>
拿取值
${pageScope.uname} <!-- page作⽤域 -->
${requestScope.uname} <!-- request作⽤域 -->
${sessionScope.uname} <!-- session作⽤域 -->
${applicationScope.uname} <!-- application作⽤域 -->
<%--
获取Map中指定值
${map["key"] } 或 ${map.key }
注:
map代表的是存在域对象中的变量名(限域变量名)
--%>
${map.aaa }
${map["bbb"]}
<%-- JavBean中的属性字段需要提供get⽅法 --%>
${user} <%-- 获取对象 --%>
${user.uname} <%--获取对象中的属性--%>
empty
<%--
empty
判断域对象是否为空。为空,返回true;不为空返回false;
${empty 限域变量名 }
判断对象是否不为空。
${!empty 限域变量名 }
--%>
${empty uname}
${empty list}
${empty map}
${empty user}
JSTL
标签的加入为了方便一些常见的问题:比如迭代⼀个映射或者集合、条件测试、XML 处理,甚⾄数据库和访问数据库操作等。
需要的jar包:
官⽅下载地址:jar包
实例:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
条件动作标签:
if 标签:
<c:if test="<boolean>" var="<string>" scope="<string>">
...
</c:if>
choose、when 和 otherwise 标签:
<c:choose>
<c:when test="<boolean>">
...
</c:when>
<c:when test="<boolean>">
...
</c:when>
...
...
<c:otherwise>
...
</c:otherwise>
</c:choose>
迭代标签:
<c:forEach
items="<object>"
begin="<int>"
end="<int>"
step="<int>"
var="<string>"
varStatus="<string>">
</c:forEach>
实例:
<%
List<String> list = new ArrayList<String>();
for (int i = 1; i <= 10; i++) {
list.add("A:" + i);
}
pageContext.setAttribute("li", list);
%>
<!-- 循环集合 -->
<c:forEach items="${li }" var="item">
${item }
</c:forEach>
<hr>
<table align="center" width="800" border="1" style="border-collapse:
collapse;">
<tr><th>名称</th>
<th>当前成员下标</th>
<th>当前成员循环数</th>
<th>是否第⼀次被循环</th>
<th>是否最后⼀次被循环</th>
</tr>
<c:forEach items="${li }" var="item" varStatus="itemp">
<tr>
<td>${item }</td>
<td>${itemp.index }</td>
<td>${itemp.count }</td>
<td>${itemp.first }</td>
<td>${itemp.last }</td>
</tr>
</c:forEach>
</table>
<!-- 循环对象集合 -->
<%
List<User> userList = new ArrayList<User>();
User user = new User(1,"zhangsan","123456");
User user2 = new User(2,"lisi","123321");
User user3 = new User(3,"wangwu","654321");
userList.add(user);
userList.add(user2);
userList.add(user3);
// 将数据设置到作⽤域中
request.setAttribute("userList", userList);
%>
<c:if test="${!empty userList }">
<table align="center" width="800" border="1" style="border-collapse:
collapse;">
<tr>
<th>⽤户编号</th>
<th>⽤户名称</th>
<th>⽤户密码</th>
<th>⽤户操作</th>
</tr>
<c:forEach items="${userList }" var="user">
<tr align="center">
<td>${user.userId }</td>
<td>${user.uname }</td>
<td>${user.upwd }</td>
<td>
<button>修改</button>
<button>删除</button>
</td>
</tr>
</c:forEach>
</table>
</c:if>
<!-- 遍历Map -->
<%
Map<String,Object> map = new HashMap<String,Object>();
map.put("map1", "aaa");
map.put("map2", "bbb");
map.put("map3", "ccc");
pageContext.setAttribute("map", map);
%>
<c:forEach items="${map }" var="mymap">
键:${mymap.key }-值:${mymap.value } <br>
</c:forEach>