Web编程进阶之jstl标准标签库

Web编程进阶(1)之jstl标准标签库

jstl(jsp标准标签库)

1.作用:代替jsp脚本

2.使用步骤:

  1. 导入标签库的jar包,并依赖
  2. 在jsp页面上引入jar包,使用taglib引入jsp标签库
  3. 在jsp页面使用c(自己设置的前缀)标签,一般配合el表达式来用

3.c:if:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--导入jar包--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--进行判断--%>
    <c:if test="${xingming !=null}">
        ${xingming}
    </c:if>
    <c:if test="${empty xingming}">
        <a href="index.jsp">未登录请登陆</a>
    </c:if>
</body>
</html>

4.c:foreach:

1.普通for循环:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <%
      int arr[]={1,2,3,4,5,6};
      pageContext.setAttribute("arr",arr);
    %>
 <%--begin起始的索引值,end终止的索引值,step步长,var为当前遍历的索引值--%>
<c:forEach  begin="0" end="5" step="1" var="index">
  ${arr[index]}
</c:forEach>
  </body>
</html>
2.新式for循环:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <%
      int arr[]={1,2,3,4,5,6};
      pageContext.setAttribute("arr",arr);
    %>
<%--items是当前容器,var是当前的对象,varstatus是当前对象的属性(索引值等)--%>
<c:forEach items="${arr}" var="ele" varStatus="v">
    ${ele}
  ${v.index}
</c:forEach>
  </body>
</html>

3.list的遍历:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
<%
  ArrayList<Integer> list = new ArrayList<>();
  list.add(1);
  list.add(2);
  list.add(3);
  pageContext.setAttribute("list",list);
%>
  <c:forEach items="${list}" var="ele">
    ${ele}
  </c:forEach>
  <body>
  </body>
</html>
4.自定义对象的遍历:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
<%
  User user1 = new User("zhangsan", 20);
  User user2 = new User("lisi", 21);
  User user3 = new User("wangwu", 22);
  ArrayList<User> list = new ArrayList<>();
  list.add(user1);
  list.add(user2);
  list.add(user3);
  pageContext.setAttribute("list",list);
%>
  <c:forEach items="${list}" var="ele">
    ${ele.name}---${ele.age}<br>
  </c:forEach>
  <body>
  </body>
</html>
5.map的遍历:

实例1

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
<%
  HashMap<Integer, String> map = new HashMap<>();
  map.put(1,"zhangsan");
  map.put(2,"lisi");
  map.put(3,"wangwu");
  pageContext.setAttribute("map",map);
%>
  <%--ele为集合中的键值对--%>
  <c:forEach items="${map}" var="ele">
    ${ele.key}---${ele.value}<br>
  </c:forEach>
  <body>
  </body>
</html>

6.c:redirect url=?重定向跳转:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <c:redirect url="/demo"></c:redirect>
  <body>
  </body>
</html>

7.c:set标签(往域中存数据):

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <%--scope设置什么域,var定义键,定义值--%>
  <c:set scope="application" var="username" value="username"></c:set>
  <body>
  ${username}
  </body>
</html>

8.c:out标签(从域中取出的数据打印到页面):

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <c:set scope="application" var="name" value="username1"></c:set>

  <%--default 设置默认值--%>
  <c:out value="${name}" default="lisi"></c:out>
  <%--escapeXml设置是否转译为html标签--%>
  <c:out value="

你好

"
escapeXml="ture"></c:out> <body> </body> </html>

9.c:choose多重条件语句(if/else):

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <%--设置域中的值为60--%>
  <c:set var="value" value="80" scope="page"></c:set>
  <c:choose>
    <c:when test="${value>=60&&value<80}">
      及格
    </c:when>
    <c:when test="${value>=80&&value<100}">
      优秀
    </c:when>
    <c:otherwise>
      不及格
    </c:otherwise>
  </c:choose>
  <body>
  </body>
</html>

你可能感兴趣的:(java基础)