JSTL的使用
1.首先下载JSTL的jar包 我上传到了网盘地址:链接: http://pan.baidu.com/s/1boU1xnx 密码: tkdj 里面有2个文件 jstl.jar standard.jar 这两个文件
2.接下来把这两个文件引用到自己的web项目中的lib目录下记得一定要弄到这个目录中,拖进来之后就如图Myeclipse 就会自动引用jar包如果没有自动引用就自己手动添加,请向下看手动添加方法
手动添加jar包
右键自己的项目,点击properties如果是中文版应该是叫设置,点add jars ,把这两个文件添加进来即可。
3.继续向下看怎么在jsp页面中使用 JSTL 一般项目中使用较多的为 if 标签 和 forEach 标签所以我就着重介绍这两个标签,后续为大家介绍自定义JSTL标签。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%//用法就跟java中一样 判断语句要写在 ${} 中 %>
<c:if test="${2==2}"> <span>2 </span></c:if>
<c:if test="${2>=1}"> <span>2 </span></c:if>
<c:if test="${2<=1}"> <span>1 </span></c:if>
<c:if test="${2!=2}"> <span>1 </span></c:if>
<!-- ne为不包含则怎么样 -->
<c:if test="${abc ne abc }"><span>abc </span></c:if>
<!-- eq为包含则怎么样-->
<c:if test="${qwe eq qwe }"><span>qwe </span></c:if>
<% List<String> list=new ArrayList<>();
list.add("123");
list.add("234");
list.add("345");
list.add("456");
list.add("567");
request.setAttribute("list",list);
%>
<c:forEach var="u" items="${list}">
<span> ${u}</span>
</c:forEach>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%//用法就跟java中一样 判断语句要写在 ${} 中 %>
<c:if test="${2==2}"> <span>2 </span></c:if>
<c:if test="${2>=1}"> <span>2 </span></c:if>
<c:if test="${2<=1}"> <span>1 </span></c:if>
<c:if test="${2!=2}"> <span>1 </span></c:if>
<!-- ne为不包含则怎么样 -->
<c:if test="${abc ne abc }"><span>abc </span></c:if>
<!-- eq为包含则怎么样-->
<c:if test="${qwe eq qwe }"><span>qwe </span></c:if>
<% List<String> list=new ArrayList<>();
list.add("123");
list.add("234");
list.add("345");
list.add("456");
list.add("567");
request.setAttribute("list",list);
%>
<c:forEach var="u" items="${list}">
<span> ${u}</span>
</c:forEach>
</body>
</html>
上面是使用JSTL的源码,
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
这句话为引用 JSTL的核心包,要使用JSTL的表达式,就必须引用它的核心包进来。
注意在实际开发中尽量避免 <% %>的使用,我是为了方便操作才这样写,大家在使用中尽量使用 MVC的思想来写。