JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能。
JSTL支持通用的、结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签,SQL标签。 除了这些,它还提供了一个框架来使用集成JSTL的自定义标签。
获取请求参数中的值使用${param.username}
JSTL taglib 支持的jar包地址:http://tomcat.apache.org/download-taglibs.cgi
1. 简单的web例子
0.需要的jar包
taglibs-standard-compat-1.2.5.jar
taglibs-standard-impl-1.2.5.jar
taglibs-standard-jstlel-1.2.5.jar
taglibs-standard-spec-1.2.5.jar
1.工程预览
2.Viewer层的jsp代码
WebContent/01/01.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 导入JSTL的标签库 -->
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- 不允许忽略EL表达式 -->
<%@ page isELIgnored="false"%>
<!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>
<%pageContext.setAttribute("username", "<h1>张三</h1>"); %>
${hello }<br />
${param.hello}<br />
${username }<br />
<c:out value="${username}" escapeXml="true"></c:out><br/>
<c:out value="${username}" escapeXml="false"></c:out><br/>
<c:out value="${username1 }" default="<font color='red'>查无此人</font>" escapeXml="false"></c:out><br />
</body>
</html>
结果:
WebContent/WEB-INF/01/user.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<!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>user.jsp</title>
<style type="text/css">
table{
border-right: 1px solid #000;
border-bottom: 1px solid #000;
}
table td{
border-left: 1px solid #000;
border-top: 1px solid #000;
padding: 4px;
}
</style>
</head>
<body>
<c:if test="${empty aa}">no "aa" property!</c:if>
<c:if test="${not empty user}">have "user" property</c:if> <br />
${user.nickname },${user.age }<br/>
<c:if test="${user.age lt 18}">未成年</c:if><br />
<c:choose>
<c:when test="${user.age eq 18}">18 year</c:when>
<c:when test="${user.age lt 18}">nonage</c:when>
<c:otherwise>adult</c:otherwise>
</c:choose>
<table align="center" border="1" width="900" cellpadding="0" cellspacing="0">
<thead>
<tr><td>用户名</td><td>昵称</td><td>年龄</td></tr>
</thead>
<tbody>
<c:forEach var="u" items="${userList}" begin="0" end="4" step="1" varStatus ="cur">
<tr <c:if test="${cur.index%2==0}">style="background:#ff0;color:#45f;"</c:if>>
<td>${u.name}</td><td>${u.nickname}--${cur.index}</td><td>${u.age}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
结果:
3.controller层[servlet基于Annotation]
HelloServlet.java:
package com.my.mvc.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 3997354772023831828L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("HelloServlet doGet");
request.setAttribute("hello", "world!");
//客户端跳转
// request.getSession().setAttribute("hello", "world");
// response.sendRedirect(request.getContextPath()+"/01/01.jsp");
//服务器端跳转
//dispatcher 会自动加上 request.getContextPath()路径
RequestDispatcher dispatcher= request.getRequestDispatcher("/01/01.jsp");
dispatcher.forward(request, response);
// response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
UserServlet.java:
package com.my.mvc.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
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 com.my.mvc.model.User;
@WebServlet("/user.do")
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 8293399267378109285L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet");
User user=new User("zs", "张三", 14);
req.setAttribute("user", user);
List<User> userList=new ArrayList<>();
for(int i=0;i<5;++i) {
userList.add(new User("wukong", "悟空"+(i==0? "":""+i), 500));
}
req.setAttribute("userList", userList);
RequestDispatcher requestDispatcher=req.getRequestDispatcher("/WEB-INF/01/user.jsp");
requestDispatcher.forward(req, resp);
System.out.println(user);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doPost");
this.doGet(req, resp);
}
}
model层:
User.java:
package com.my.mvc.model;
public class User {
private String name;
private String nickname;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public User(String name, String nickname, int age) {
super();
this.name = name;
this.nickname = nickname;
this.age = age;
}
public User() {
}
@Override
public String toString() {
return "User [name=" + name + ", nickname=" + nickname + ", age=" + age + "]";
}
}