JavaWeb基础入门--JSTL和EL表达式

本文介绍了JavaWeb中的JSTL和EL表达式,讲解了EL表达式的使用,包括其简化jsp输出的功能,以及EL的作用域对象。接着详细阐述了JSTL标签库的下载、安装和应用,如核心标签库中的判断标签和遍历集合,以及fmt格式化标签库在日期和数字格式化中的应用。通过实例展示了如何在实际开发中运用这些技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JSTL和EL表达式:

EL表达式:为了替换out.println(),用于简化jsp的输出


测试案例:
Student.java类:

package com.imooc.el;

public class Student {
	//属性
	private String name;//姓名
	private String mobile;//手机号
	
	//setter getter
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

}

StudentServlet:

package com.imooc.el;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class StudentServlet
 */
@WebServlet("/info")
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Student stu = new Student();
		stu.setName("子墨"); 
		stu.setMobile(null);
		String grade = "A";
		
		request.setAttribute("student", stu);
		request.setAttribute("grade", grade); 
		
		//将请求转发到el_info.jsp页面
		request.getRequestDispatcher("/el_info.jsp").forward(request, response);
	} 

}

el_info.jsp:EL表达式只能在jsp页面使用,不可以用在HTML页面中

<%@ 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>
		<!-- 不需要考虑导入问题,而且格式也相当于HTML格式 -->
		<h1>姓名: ${requestScope.student.name}</h1>
		<h2>手机: ${requestScope.student.mobile}</h2>
		<h2>评级: ${requestScope.grade}</h2>
</body>
</html>

输出结果:值为null的会输出空白

 


EL的作用域对象:
 

测试案例:
StudentServlet:

package com.imooc.el;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class StudentServlet
 */
@WebServlet("/info")
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Student stu = new Student();
		stu.setName("子墨"); 
		stu.setMobile(null);
		String grade = "A";
		
		//请求作用域
		request.setAttribute("grade", "B"); 
		//全局作用域
		request.getServletContext().setAttribute("grade", "C");
		
		HttpSession session = request.getSession(); 
		//session作用域
		session.setAttribute("student", stu);
		session.setAttribute("grade", grade); 
		
		//将请求转发到el_info.jsp页面
		request.getRequestDispatcher("/el_info.jsp").forward(request, response);
	} 

}

el_info.jsp:不写作用域,或从范围从小到大依次匹配

<%@ 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>
		<!-- 不需要考虑导入问题,而且格式也相当于HTML格式 -->
		<h1>姓名: ${student.name}</h1>
		<h2>手机: ${student.mobile}</h2>
		<h2>评级: ${grade}</h2>
</body>
</html>

输出结果:输出作用域范围最小的request的值
 


EL表达式输出:


EL输出参数值:
 

测试案例:

StudentServlet:

package com.imooc.el;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class StudentServlet
 */
@WebServlet("/info")
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取传来的参数
		String teacher = request.getParameter("teacher");
		
		Student stu = new Student();
		stu.setName("子墨"); 
		stu.setMobile(null);
		String grade = "A";
		
		
		HttpSession session = request.getSession(); 
		//session作用域
		session.setAttribute("student", stu);
		session.setAttribute("grade", grade); 
		
		//将请求转发到el_info.jsp页面
		request.getRequestDispatcher("/el_info.jsp").forward(request, response);
	} 

}

el_info.jsp:

<%@ 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>
		<!-- 不需要考虑导入问题,而且格式也相当于HTML格式 -->
		<h1>姓名: ${student.name}</h1>
		<!--  EL表达式不输出null,会使用空字符串替代 --> 
		<h2>手机: ${student.mobile}</h2>
		<!-- 获取传来的参数  -->
		<h2>讲师: ${param.teacher}</h2>
		<h2>评级: ${grade}</h2>
		<h2>概要: ${student}</h2>
</body>
</html>

在浏览器输入:

http://localhost:8080/el/info?teacher=sammy

输出结果:
 


JSTL标签库:
 

 

下载JSTL标签库:

已经下载成功:


安装JSTL标签库:
 

1、只需要将两个jar包复制到lib目录下,就会自动出现在Web App Libraries里,只在本工程有效

2、或者直接将两个jar包复制到tomcat的lib目录


JSTL的标签库:
 

核心标签库:

引用JSTL核心库:

core.jsp:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!-- 在java或者jsp文件输入 Alt+/ 可出现智能提示 -->
 <%@ 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>
	<c:if test=""></c:if>  

</body>
</html>

JSTL判断标签:

单分支判断的使用:
JstlServlet:

package com.imooc.jstl;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class JstlServlet
 */
@WebServlet("/jstl")
public class JstlServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JstlServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("score",58);
		request.setAttribute("grade","B");
		request.getRequestDispatcher("/core.jsp").forward(request, response);
	}

}

core.jsp:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!-- 在java或者jsp文件输入 Alt+/ 可出现智能提示 -->
 <%@ 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>
	<h1>${requestScope.score}</h1>
	<c:if test="${score>=60 }">
		<h1 style="color:green">恭喜,你已通过测试</h1>
	</c:if>
	<c:if test="${score<60 }">
		<h1 style="color:red">sorry,你未通过测试</h1>
	</c:if>
</body>
</html>

成绩为58时,输出结果:
 

成绩为90时,输出结果:


多分支判断的使用:
core.jsp:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!-- 在java或者jsp文件输入 Alt+/ 可出现智能提示 -->
 <%@ 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>
	<h1>${requestScope.score}</h1>
	<!-- if -->
	<!-- 进行单分支判断 -->
	<c:if test="${score>=60 }">
		<h1 style="color:green">恭喜,你已通过测试</h1>
	</c:if>
	<c:if test="${score<60 }">
		<h1 style="color:red">sorry,你未通过测试</h1>
	</c:if>
	
	<!-- choose when otherwise -->
	<!-- 进行多分支判断 -->
	<h1>${grade}</h1>
	<c:choose>
		<c:when test="${grade =='A' }">
			<h2 style="color:pink">你很优秀</h2>
		</c:when>
		
		<c:when test="${grade =='B' }">
			<h2 style="color:red">不错哦</h2>
		</c:when>
		
		<c:when test="${grade =='C' }">
			<h2 style="color:orange">继续加油</h2>
		</c:when>
		
		<c:when test="${grade =='D' }">
			<h2 style="color:green">辣鸡</h2>
		</c:when>
		
		<c:otherwise>
			<h2 style="color:green">一切随缘吧</h2>
		</c:otherwise>
	
	
	
	</c:choose>
	
	
	
	
</body>
</html>

输出结果:
 


JSTL遍历集合:
 

案例测试:
Company.java:
 

package com.imooc.jstl;

public class Company {
	//属性
	private String cname;//mingzi 
	private String url;
	
	//带参构造
	public Company(String cname, String url) {
		super();
		this.cname = cname;
		this.url = url;
	}
	//setter getter
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
}

JstlServlet:
 

package com.imooc.jstl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class JstlServlet
 */
@WebServlet("/jstl")
public class JstlServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JstlServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		List list = new ArrayList();
		list.add(new Company("腾讯","www.baidu.com"));
		list.add(new Company("阿里","www.ali.com"));
		list.add(new Company("华为","www.huawei.com"));
		//将集合存储在请求中
		request.setAttribute("companies", list);
		request.getRequestDispatcher("/core.jsp").forward(request, response);
	}

}

core.jsp:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!-- 在java或者jsp文件输入 Alt+/ 可出现智能提示 -->
 <%@ 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>
	
	<!-- forEach 标签用于遍历集合 
		List companies = (List)request.getAttribute("companies");
		for(Company c:companies){
			out.println(...);
		}
	-->
	<c:forEach items="${requestScope.companies }" var="c" varStatus="idx">
		<h2 style="color:pink">${idx.index+1}.${c.cname }-${c.url }</h2>
	
	</c:forEach>
	
</body>
</html>

输出结果:
 


fmt格式化标签库:比较重要的就是日期标签库和数字标签库

日期标签库的用处:
fmt.jsp:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setAttribute("amt",1980983.894);
		request.setAttribute("now",new java.util.Date());
		request.setAttribute("html","<a href='index.html'>index</a>");
		request.setAttribute("nothing",null);
	
	%>
	
	<!-- 
		formData pattern
	yyyy - 四位年
	MM - 两位月
	dd - 两位日
	HH - 24小时制
	hh - 12小时制
	mm - 分钟
	ss - 秒数
	SSS - 毫秒
	 -->
	<h2>原始数据:${now }</h2>
	<h2>格式化后数据:<fmt:formatDate value="${requestScope.now }" pattern="yyyy年mm月dd日 HH时mm分ss秒SSS毫秒"/></h2>

</body>
</html>

输出结果:


综合测试:
fmt.jsp:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
 <%@ 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>
	<%
		
		request.setAttribute("now",new java.util.Date());
		request.setAttribute("amt",1980983.897);
		request.setAttribute("html","<a href='index.html'>index</a>");
		request.setAttribute("nothing",null);
	
	%>
	
	<!-- 
		formData pattern
	yyyy - 四位年
	MM - 两位月
	dd - 两位日
	HH - 24小时制
	hh - 12小时制
	mm - 分钟
	ss - 秒数
	SSS - 毫秒
	 -->
	<h2>原始数据:${now }</h2>
	<h2>格式化后数据:<fmt:formatDate value="${requestScope.now }" pattern="yyyy年mm月dd日 HH时mm分ss秒SSS毫秒"/></h2>

	<h2>原始金额:${amt }元</h2>
	<h2>格式化后金额:¥<fmt:formatNumber value="${requestScope.amt }" pattern="0,000.00"/>元</h2>

	<h2>null默认值:<c:out value="${nothing }" default="无"></c:out></h2>

	<!-- 使得显示HTML源代码 -->
 	<h2>${html }</h2>
 	<h2><c:out value="${html }" escapeXml="true"></c:out></h2>


</body>
</html>

输出结果:
 


开发ali员工信息表:
暂时搁置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值