引用JSTL核心库
1.介绍
JSTL安装功能分为5类标签库
| 类别 |
| 核心标签库——core |
| 格式化输出标签库——fmt |
| SQL操作标签库——sql |
| XML操作标签库——xml |
| 函数标签库——functions |
2.抒写规则
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
JSTL标签库在taglibs-standard-impl.jar由META-INF/c.tld定义。
3.使用JSTL
导入相对应的jar和在JSP中抒写对应规则,然后在<body>标签中抒写"<c:"就会出现如下选项,说明配置成功。
4.判断标签
JSTL核心库提供了两种判断的标签
<c:if>——单分支判断
<c:choose>、<c:when>、<c:otherwise>——多分支判断
JstlServlet.java
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();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("score", 46);
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"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL</title>
</head>
<body>
<h1>${requestScope.score }</h1>
<c:if test="${requestScope.score>=60 }">
<h2 style="color:green">成绩合格!</h2>
</c:if>
<c:if test="${requestScope.score<60 }">
<h2 style="color:red">成绩不合格!</h2>
</c:if>
</body>
</html>
虽然实现了功能,但是使用单分支判断对于程序的可读性来说太差了。JSTL提供了多分支判断的语句
<%@ 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>JSTL</title>
</head>
<body>
<h1>${requestScope.score }</h1>
<c:if test="${requestScope.score>=60 }">
<h2 style="color:green">成绩合格!</h2>
</c:if>
<c:if test="${c.score<60 }">
<h2 style="color:red">成绩不合格!</h2>
</c:if>
<!-- 多分支判断 choose when otherwise-->
<c:choose>
<c:when test="${requestScope.grade == 'A'}">
<h2>优秀</h2>
</c:when>
<c:when test="${requestScope.grade == 'B'}">
<h2>良</h2>
</c:when>
<c:when test="${requestScope.grade == 'C'}">
<h2>中</h2>
</c:when>
<c:when test="${requestScope.grade == 'D'}">
<h2>差</h2>
</c:when>
<c:otherwise>
<h2>GG</h2>
</c:otherwise>
</c:choose>
</body>
</html>
5.JSTL遍历集合
<c:forEach>标签用于遍历集合中的每一个对象
items:遍历是集合
var:集合中的内容
varStatus:集合遍历的次数——idx.index=0为初始值
Company.java
package com.imooc.company;
public class Company {
private String cname;
private String url;
//构造方法,方便写入数据
public Company(String cname,String url) {
this.cname = cname;
this.url = url;
}
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;
}
}
CompanySevlet.java
package com.imooc.company;
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 CompanySevlet
*/
@WebServlet("/CompanySevlet")
public class CompanySevlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CompanySevlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//创建集合
List<Company> list = new ArrayList<Company>();
//存入信息
list.add(new Company("百度","www.baidu.com"));
list.add(new Company("腾讯", "www.tencent.com"));
list.add(new Company("网易", "www.imooc.com"));
//将集合存入request作用域
request.setAttribute("companys", list);
//进行转发
request.getRequestDispatcher("/company.jsp").forward(request, response);
}
}
company.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>Company</title>
</head>
<body>
<!-- 通过forEach循环
List companylist = request.getAttribute("companys");
for(Company c : companylist){
out.println("...");
}
-->
<c:forEach items="${requestScope.companys }" var="c" varStatus="idx">
<h2>${idx.index+1}.${c.cname } ${c.url }</h2>
</c:forEach>
</body>
</html>