eclipse下如何引入JSTL
1、==将相关Java包拷贝到当前项目的web-inf下的lib文件夹中
2、在当前项目下的web.xml文件中拷贝如下代码
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/fmt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/fn</taglib-uri>
<taglib-location>/WEB-INF/fn.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/perTag</taglib-uri>
<taglib-location>/WEB-INF/permittedTaglibs.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
<taglib-location>/WEB-INF/sql.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/script</taglib-uri>
<taglib-location>/WEB-INF/scriptfree.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
<taglib-location>/WEB-INF/x.tld</taglib-location>
</taglib>
</jsp-config>
3、在使用JSTL标签的页面中引入如下指令
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
JSTL标签:
1.page.jsp
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="cn.qf.emp.pojo.Dept"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Dept dept = new Dept();
dept.setDeptno("1001");
dept.setDname("a");
dept.setLoc("河口1");
Dept dept2 = new Dept();
dept2.setDeptno("1002");
dept2.setDname("b");
dept2.setLoc("河口2");
List<Dept> depts = new ArrayList<Dept>();
depts.add(dept);
depts.add(dept2);
request.setAttribute("d", dept);
request.setAttribute("ds", depts);
%>
使用JSTL的set标签进行赋值
<c:set var="stuName" value="崔宸" scope="request"></c:set>
<c:set target="${d }" property="dname" value="java开发"></c:set>
输出
<c:out value="${stuName }"></c:out>
<c:out value="${d.dname }" default="部门名称未知"></c:out>
<c:out value="${d.loc }" default="地址未知"></c:out>
<hr/>
删除
<c:remove var="stuName" scope="request"/>
删除后stuName的值:
<c:out value="${stuName }"></c:out>
</body>
</html>
2.page2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
session.setAttribute("userinfo", "");
%>
<c:set var="islogin" value="${empty sessionScope.userinfo}"></c:set>
<c:if test="${islogin}">
<form method="post" action="">
<p>账号<input type="text" name="uid" /></p>
<p>密码<input type="password" name="uid" /></p>
</form>
</c:if>
<c:if test="${!islogin}">
你已经登陆
</c:if>
</body>
</html>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="cn.qf.emp.pojo.Dept"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Dept dept = new Dept();
dept.setDeptno("1001");
dept.setDname("a");
dept.setLoc("河口1");
Dept dept2 = new Dept();
dept2.setDeptno("1002");
dept2.setDname("b");
dept2.setLoc("河口2");
List<Dept> depts = new ArrayList<Dept>();
depts.add(dept);
depts.add(dept2);
request.setAttribute("d", dept);
request.setAttribute("ds", depts);
%>
EL表达式获取部门对象的值:
${requestScope.d.dname }
<hr/>
EL表达式获取集合中的值:
${requestScope.ds[1].loc}
</body>
</html>
<%@page import="cn.qf.emp.pojo.Dept"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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>
主页面
<form method="post" action="QueryDeptServlet">
位置:<input type="text" name="loc"/>
<input type="submit" value="查询"/>
</form>
<hr/>
<table>
<tr>
<td>部门编号</td>
<td>名称</td>
<td>位置</td>
</tr>
<c:forEach items="${requestScope.depts}" var="dept" varStatus="status">
<tr
<c:if test="${status.index%2==0}">
<style="background-color:pink;"/>
</c:if>
>
<td>${dept.deptno}</td>
<td>${dept.dname}</td>
<td>${dept.loc}</td>
</tr>
</c:forEach>
</table>
</body>
</html>