JSTL标签
1.JSTL介绍
JSTL(JavaServer Pages Standard Tag Library,JSP标准标签库)是一个JSP标签集合,它封装了JSP应用的通用核心功能。
2.常用标签的使用
(1).<c:if>标签
jstl写判断 jstl表达式if语句,当test值为true则执行标签内容
<%
char c='男';
if(c!='男'){
request.setAttribute("message","女");
}else{
request.setAttribute("message","男");
}
%>
<c:if test="${message=='女'}">对</c:if>
<c:if test="${message=='男'}">错</c:if>
执行结果为“错”
(2).<c:choose>标签
choose标签when可以出现一次或者多次,表示一个otherwise可以出现0次或者1次,当test为true时,执行标签题体的内容,可以使用el表达式给该属性赋值,在这期间不能使用与choose标签无关的代码,该标签相当于java中的if(){}else if(){}…else{}。
<%
request.setAttribute("age","20");
%>
<c:choose>
<c:when test="${age<20}">少年</c:when>
<c:when test="${age==20}">青年</c:when>
<c:when test="${age>20}">成年</c:when>
<c:otherwise>不是人</c:otherwise>
</c:choose>
执行结果为青年。
(3)<c:forEach>标签
items属性:用来执行要遍历的集合或者数组,可以使用el表达式来赋值
var属性:用来执行绑定名(每遍历出来一条数据赋给var)
begin:开始遍历的下标
end:遍历结束的下标
index属性:varStatus中的index是数据遍历的下标
count属性:varStatus中的count是数据遍历的次数
<c:forEach items="${list}" var="s" begin="2" end="6" varStatus="t">
<!-- 获取对象的名字 -->
${s.name};
下标:${t.index};
次数:${t.count};<br>
</c:forEach>
获取后端的数据并返回给jsp页面
1.后端数据处理
public class someServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
List<User> list=new ArrayList<User>();
for(int i=1;i<=10;i++){
User u=new User();
u.setName("admin"+i);
u.setAge(20+i);
list.add(u);
}
//数据绑定
req.getSession().setAttribute("list",list);
//重定向
res.sendRedirect(req.getContextPath()+"/DemoJstl02.jsp");
}
}
2.jsp页面
<table width="80%" border="2" cellspacing="0">
<tr>
<th>序号</th>
<th>用户名</th>
<th>用户年龄</th>
</tr>
<c:forEach items="${list}" var="s" varStatus="t">
<tr class="row${t.count%2+1}">
<td>${t.count}</td>
<td>${s.name}</td>
<td>${s.age}</td>
</tr>
</c:forEach>
</table>
3.运行结果
(4)fmt:formatDate标签
日期转化格式:
value:获取的值可以用el表达式赋值
type值为time:时分秒
type值为date:年月日
type值为both:年月日时分秒
pattern:日期转换格式
<%
Date date=new Date();
request.setAttribute("date",date);
%>
现在的时间:<fmt:formatDate type="date" value="${date}" pattern="yyyy/MM/dd"/>
3.自定义标签
(1)自定义标签类
如果要写一个标签类需要我们去实现SimpleTagSupport属性
//自定义一个日期转换标签类
public class MyDateTag extends SimpleTagSupport{
private String pattern;
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
@Override
public void doTag() throws JspException, IOException {
//获取当前时间
Date date=new Date();
//日期转换格式
SimpleDateFormat sdf=new SimpleDateFormat(pattern);
String d=sdf.format(date);
//获取jsp上下文
JspContext js=getJspContext();
//获取jsp上下文的输出流
JspWriter out=js.getOut();
//输出该内容
out.print(d);
}
}
(2)配置标签tld文件
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>m</short-name>
<uri>http://chaochao.com/core</uri>
<tag>
<!-- 标签名-->
<name>date</name>
<tag-class>web.MyDateTag</tag-class>
<!--
标签内容有三个值:
empty:该标签没有标签体;
JSP:该标签可以有标签体,并且标签体的内容可以使用java代码,但是只有复杂标签
scriptless:该标签体可以有标签体,但是标签体对的内容不能出来java代码
-->
<body-content>empty</body-content>
<attribute>
<!--处理标签类中的属性-->
<name>pattern</name>
<!--true:该属性是一个必选的属性-->
<required>true</required>
<!-- true:该属性可以动态赋值,可以使用el表达式来复制-->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
(3)jsp页面显示
要导入我们刚才自定义的标签配置文件:
<%@taglib uri=“http://chaochao.com/core” prefix=“d”%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://chaochao.com/core" prefix="d"%>
<!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>获取当前时间</title>
</head>
<body>
当前时间:<d:date pattern="yyyy-MM-dd HH:mm:ss"/>
</body>
</html>