7.eclipse下引入JSTL和EL表达式

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>

EL表达式和JSTL表达式概念

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值