核心标签库

核心标签库

JSTL标签库

什么是JSTL:

Java Standand Tag Library

Java标准标签库,每个JSP标签在翻译成Servlet之后会转成Java代码。
- 语法:

`<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>`
JSTL标签库分类:

JSTL前缀URI 作用
核心标签库chttp://java.sun.com/jsp/jstl/core 用于页面上的逻辑控制,如:if、forEach
国际化标签fmthttp://java.sun.com/jsp/jstl/fmt 用于网页的国际化,表示层根据浏览器不同的地区,使用不同的语言显示
数据库标签sqlhttp://java.sun.com/jsp/jstl/sql 直接在JSP页面上访问数据库
XML标签xhttp://java.sun.com/jsp/jstl/xml 在JSP上解析XML文件的一组标签
函数标签fnhttp://java.sun.com/jsp/jstl/functions 可在JSP页面上使用的一组字符串函数

JSTL使用步骤

1)在项目中导入JSTL的jar包,默认JSP自带。

2)在页面上使用taglib指令导入标签库

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

3)在JSP页面上使用标签库,
- 语法:

判断标签
  • 作用: 判断表达式是否为真,如果为真,执行if标签主体的内容。

  • 属性名 test

  • 是否支持EL true
  • 属性类型 boolean
  • 属 性 描 述

    如果test中的值返回true,执行if标签主体的内容。没有else标签

示例:

如果用户提交的age值大于18,则显示你已经成年,否则显示未成年。

<form action="demo1.jsp">
  年龄:<input type="text" name="age"  value="${param.age}"/>
  <input type="submit"  value="测试"/>
</form>

<c:if test="${param.age>=18}">
    你已经成年
</c:if>
<c:if test="${param.age<18}">
    未成年,过两年再来
</c:if>
多分支标签
  • 作用:类似于Java中swtich
标签名作用
choose分支标签的容器
when类似于switch中的case,when可以出现多次
otherwise类似于switch中的default,如果上面所有的条件都不满足,则执行这个
<c:if test="${not empty param.age}">
    <c:choose>
        <c:when test="${param.age>=18}">
        你已经成年
        </c:when>
        <c:otherwise>
        未成年,过两年再来
        </c:otherwise>
    </c:choose>
</c:if>
案例:通过输入分数,得到评级

在一个表单grade.jsp输入一个分数,提交给自己。然后在同一个页面中根据分数输出相应的等级,90~100优秀 80~90 良好 60~70中等 60~70 及格 60以下不及格。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>demo2.jsp</title>
  </head>
  <body>
      <form action="demo2.jsp">
    请输入分数:<input type="text" name="score"  value="${param.score}"/>
    <input type="submit"  value="评级"/>
  </form>
  <c:if test="${not empty param.score}">
    <c:choose>
        <c:when test="${param.score>=90 && param.score<=100 }">
            优秀
        </c:when>
        <c:when test="${param.score>=80 && param.score< 90 }">
            良好
        </c:when>
        <c:when test="${param.score>=70 && param.score<80 }">
            中等
        </c:when>
        <c:when test="${param.score>=60 && param.score<70 }">
            及格
        </c:when>
        <c:when test="${param.score>=0 && param.score<60 }">
            不及格
        </c:when>
        <c:otherwise>
            输入有误
        </c:otherwise>
    </c:choose>
  </c:if>
  </body>
</html>
遍历标签
  • 作用: 用于循环标签,遍历元素
属性名是否支持EL属性类型属性描述
varfalseStringvar的变量名代表集合中的每一个元素
varStatusfalseString代表每个元素的状态对象,一共有4个属性,属性的含义见下表
itemstrue数组或集合使用EL表达式,代表集合或数组
begintrueint遍历从哪个元素开始
endtrueint遍历到哪个元素结束
steptrueint代表步长,遍历时每次跳几个元素

varStatus对象属性:

属性数据类型含义
indexint当前遍历到的这个元素索引号,从0开始
countint遍历到当前为止,一共遍历了多少个元素
firstboolean如果当前遍历的是第1个元素,则返回true
lastboolean如果当前遍历的是最后1个元素,则返回true

- 需求:在Servlet中创建1个List集合,List中每个元素是一个Student对象(学号,姓名,性别,分数)。将List集合添加到请求域,转发到JSP页面上,使用forEach标签以表格的方式输出集合中的每个元素。

<%
    List<Student> students = new ArrayList<Student>();
    students.add(new Student(1000, "孙悟空", true, 89.3));
    students.add(new Student(2000, "猪八戒", true, 60.8));
    students.add(new Student(3000, "嫦娥", false, 90.3));
    students.add(new Student(4000, "白骨精", false, 93.3));
    students.add(new Student(5000, "狐狸精", false, 70.8));
    request.setAttribute("students", students);
%>
<table border="1" >
    <caption><h2>学生信息列表</h2></caption>
    <tr>
        <th>序号</th>
        <th>学号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>成绩</th>
    </tr>
    <!-- 循环tr
    stu: 代表一个Student对象,JavaBean
     -->
    <c:forEach items="${requestScope.students}" var="stu" varStatus="row" >
    <tr>
        <td>${row.index}</td>
        <td>${stu.id}</td>
        <td>${stu.name}</td>
        <td>${stu.male?"男":"女"}</td>
        <td>${stu.score}</td>
    </tr>
    </c:forEach>
</table>

遍历各种数据类型

image

6.4.4   遍历数组:<br/>
 <%
    int arr[] = {100,200,56,34,20};
    pageContext.setAttribute("arr", arr);
%>
<c:forEach items="${arr}" var="a">
    ${a} &nbsp;
</c:forEach>

<hr/>
6.4.5   遍历Map: <br/>
<%
    Map<String, String> names = new HashMap<String,String>();
    names.put("no1", "苍老师");
    names.put("no-2", "宋老师");
    names.put("no3", "陈老师");
    pageContext.setAttribute("names", names);
%>
<c:forEach items="${names}" var="entry">
    ${entry.key}:  ${entry.value} <br/>
</c:forEach>

<hr/>
6.4.6   类似于Java中for循环的用法<br/>
示例:输出1~100<br/>
<c:forEach var="i" begin="1" end="100" step="2">
    ${i} &nbsp;
</c:forEach>
<hr/>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值