一:jstl简介
jstl全名为:JavaServer Pages Standard Tag Library,中文名叫Jsp标准标签函数库。
jstl是一个标准,且已经定制好的标签函数库,可以应用各种领域。如:基本输出、输入、流程控制、循环、XML文件剖析、资料库查询、文字格式标准化得应用。
jstl分为五大类:
核心标签库(Core tab library)
Il8N格式标签库( Il8N-capable formating library)
SQL标签库(SQL tag library)
XML处理(XML tag library)
函数功能(Functions tag library)
JSTL | 前置名称 | URL | 示例 |
核心标签库 | c | http://java.sun.com/jsp/jstl/core | <c:out> |
Il8N格式标签库 | fmt | http://java.sun.com/jsp/jstl/fmt | <fmt:formatDate> |
SQL标签库 | sql | http://java.sun.com/jsp/jstl/sql | <sql:query> |
XML处理 | xml | http://java.sun.com/jsp/jstl/xml | <x:forbach> |
函数功能 | fn | http://java.sun.com/jsp/jstl/functions | <fn:split> |
jstl也支持EL表达式
例如:
//创建一个类
package com.model; public class Chapter1 { private int id; private String username; private String userPwd; public Chapter1() { super(); } public Chapter1(int id, String username, String userPwd) { super(); this.id = id; this.username = username; this.userPwd = userPwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } }
//创建servlet
package com.action; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.model.Chapter1; public class Chapter1Action extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); request.setAttribute("user", new Chapter1(1,"张三","李四")); request.getRequestDispatcher("/chapter1.jsp").forward(request, response); } }
//JSP页面
<%@ page language="java" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>chapter1.jsp</title> </head> <body> <div> <div> <lable>id</lable><br/> <lable>姓名:</lable><br/> <lable>密码:</lable> </div> <div> <lable>${user.id}</lable><br/> <lable>${user.username}</lable><br/> <lable>${user.userPwd}</lable> </div> </div> </body> </html>