JSTL(Java server pages standarded tag library,即JSP标准标签库)

目录

JSTL的使用

if标签

choose、when和otherwise标签

forEach标签

格式化动作标签

            formatNumber标签

            formatDate标签

             parseNumber标签

            parseDate标签


JSTL的使用

            1.下载jstl所需要的jar包(standard.jar和jstl.jar)
            2.在项目的web目录下的WEB-INF目录中新建lib目录,将jar包拷贝进去
            3.选择“File”,再选择“Project Structure”
            4.选择“Modules”,选择右侧的“Dependencies”,点击“+”将对应的lib目录添加
            5.在需要使用标签库的JSP页面通过taglib标签引入指定库

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--通过taglib标签引入所需要的标签库--%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <title>JSTL的使用</title>
</head>
<body>
    <c:if test="${1==1}">
        Hello JSTL
    </c:if>
</body>
</html>

if标签


        格式:
            <c:if test="<boolean>" var="<string>" scope="<string>">
                ...
            </c:if>

        常用属性:
            test:条件判断,操作的是域对象,接收返回结果是boolean类型的值(必要属性
            var:限域变量名(存放在作用域中的变量名),用来接收判断结果的值(可选属性)
            scope:限域变量名的范围(page、request、session、application)(可选属性)
                注:
                    1.标签操作的一般都是域对象
                    2.if标签没有else,想实现else的效果,需要设置两个完全相反的条件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>if标签</title>
</head>
<body>
<%
    //设置数据
    request.setAttribute("num", 10);
%>
<c:if test="${num>0}">
    数值大于0
</c:if>
<hr>
<c:if test="${num>100}" var="flag" scope="request">
    ${flag}---${requestScope.flag}---${sessionScope.flag}
</c:if>
</body>
</html>

choose、when和otherwise标签

        格式:
            <c:choose>
                <c:when test="<string>">
                    ...
                </c:when>
                <c:when test="<string>">
                    ...
                </c:when>
                ...
                ...
                <c:otherwise>
                    ...
                </c:otherwise>
            </c:choose>

        属性:
                    1.choose标签没有属性
                    2.when标签只有一个test属性,必须属性
                    3.otherwise标签没有属性
        注意:
                    1.choose标签、otherwise标签没有属性,when标签必须有一个test属性
                    2.choose标签中必须包含至少一个when标签,可以没有otherwise标签
                    3.otherwise标签必须设置在最后一个when标签之后
                    4.choose标签中只能设置when标签和otherwise标签
                    5.when标签和otherwise标签中可以嵌套其他标签
                    6.otherwise标签只有在其他when标签都没有执行的情况下才会执行

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>choose、when和otherwise标签</title>
</head>
<body>
    <%
        request.setAttribute("score",80);
    %>
<c:choose>
    <c:when test="${score < 60}">
        <h2>不及格</h2>
    </c:when>
    <c:when test="${score == 60}">
        <h2>及格</h2>
    </c:when>
    <c:when test="${score > 60 && score < 80}">
        <h2>良好</h2>
    </c:when>
    <c:otherwise>
        <h2>优秀</h2>
    </c:otherwise>
</c:choose>
</body>
</html>

forEach标签


            格式:
                <c:forEach
                    items="<object>"
                    begin="<int>"
                    end="<int>"
                    step="<int>"
                    var="<string>"
                    varStatus="<string>">
                </c:forEach>

            属性:
                begin:开始数
                end:结束数
                step:间隔数
                var:限域变量名,用来接收当前遍历的成员
                items:要循环的数据(数组、List、Map等)
                varStatus属性
                            index:当前这次迭代从0开始的迭代索引
                            count:当前这次迭代从1开始的迭代计数
                            first:用来表明当前这轮迭代是否为第一次迭代的标志
                            last:用来表明当前这轮迭代是否为最后一次迭代的标志

            1.迭代主体内容多次
                <c:forEach begin="开始数" end="结束数" step="间隔数" var="限域变量名">
                </c:forEach>

                相当于Java中for...int
                for(int i=0; i < 10; i++){
                }
            2.循环
                <c:forEach items="要被循环的数据" var="限域变量名">
                </c:forEach>

                相当于Java中foreach
                for(String str:list){
                }

package com.xxxx.Po;


public class User {
    private Integer userId;
    private String uname;
    private String upwd;

    public User(Integer userId, String uname, String upwd) {
        this.userId = userId;
        this.uname = uname;
        this.upwd = upwd;
    }

    public User() {
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUpwd() {
        return upwd;
    }

    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }
}
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.xxxx.Po.User" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>forEach标签</title>
</head>
<body>

    <%--迭代主体内容多次--%>
    <c:forEach var="i" begin="1" end="10" step="2" >
        ${i}&nbsp;
    </c:forEach>
    <hr>



    <%--循环List--%>
    <%
        List<String> list=new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            list.add("A:"+i);
        }
        pageContext.setAttribute("list",list);
    %>
    <c:forEach items="${list}" var="item">
        ${item}&nbsp;
    </c:forEach>
    <hr>

<table align="center" width="800" border="1" style="border-collapse: collapse">
    <tr>
        <th>名称</th>
        <th>当前成员下标</th>
        <th>当前成员循环数</th>
        <th>是否第一次被循环</th>
        <th>是否最后一次被循环</th>
    </tr>
    <c:forEach items="${list}" var="item" varStatus="itemp">
        <tr align="center">
            <td>${item}</td>
            <td>${itemp.index}</td>
            <td>${itemp.count}</td>
            <td>${itemp.first}</td>
            <td>${itemp.last}</td>
        </tr>
    </c:forEach>
</table>
    <hr>




    <%--循环对象集合--%>
    <%
        List<User> userList=new ArrayList<User>();
        User user=new User(1,"zhangsan","123456");
        User user2=new User(2,"lisi","44564");
        User user3=new User(3,"wangwu","76944");
        userList.add(user);
        userList.add(user2);
        userList.add(user3);
        //将数据设置到作用域中
        request.setAttribute("userList",userList);
    %>
    <%--判断集合是否为空--%>
    <c:if test="${!empty userList}">
        <%--当集合不为空时遍历集合--%>
        <table align="center" width="800" border="1" style="border-collapse: collapse">
            <tr>
                <th>用户编号</th>
                <th>用户名称</th>
                <th>用户密码</th>
                <th>用户操作</th>
            </tr>
            <c:forEach items="${userList}" var="user">
                <tr>
                    <td>${user.userId}</td>
                    <td>${user.uname}</td>
                    <td>${user.upwd}</td>
                    <td><button>修改</button></td>
                </tr>
            </c:forEach>
        </table>
    </c:if>
    <hr>




    <%--循环Map--%>
    <%
        Map map=new HashMap();
        map.put("map1","aaa");
        map.put("map2","bbb");
        map.put("map3","ccc");
        pageContext.setAttribute("map",map);
    %>
    <c:forEach items="${map}" var="m">
        key:${m.key}&nbsp;value:${m.value}<br>
    </c:forEach>
</table>
</body>
</html>

格式化动作标签


            formatNumber标签


                        将数值型转化为指定格式的字符串
                                语法格式:
                                        <ftm:formatNumber
                                                value="<string>"
                                                type="<string>"
                                                var="<string>"
                                                scope="<string>"/>

                                常用属性:
                                        value:要格式化的数值
                                        type:要格式化的类型
                                                    number      数值型(默认)
                                                    percent     百分比类型
                                                    currency    货币类型
                                        var:限域变量名,用来接收格式化后的结果
                                        scope:var属性的范围(page|request|session|application)
                                    注:1.如果使用了var属性,标签不会输出结果,需要使用EL表达式获取
                                           2.默认的类型(type)的取值为number

            formatDate标签


                        将Date型数据转换成指定格式的字符串
                                    语法格式:
                                        <ftm:formatDate
                                                value="<string>"
                                                type="<string>"
                                                dateStyle="<string>"
                                                timeStyle="<string>"
                                                pattern="<string>"
                                                timeZone="<string>"
                                                var="<string>"
                                                scope="<string>"/>

                                    常用属性:
                                            value:要格式化的日期
                                            type:格式化的类型
                                                        date:日期型,年月日
                                                        time:时间型,时分秒
                                                        both:日期时间型
                                            dateStyle:日期格式(FULL|LONG|MEDIUM|SHORT|DEFAULT)
                                            timeStyle:时间格式(FULL|LONG|MEDIUM|SHORT|DEFAULT)
                                            pattern:自定义模式
                                            timeZone:时区
                                            var:变量名
                                            scope:变量范围

代码 描述
y 不包含纪元的年份。如果不包含纪元的年份小于10,则显示不具有前导零的年份。
M 月份数字。一位数的月份没有前导零。
月中的某一天。一位数的日期没有前导零。
h 12小时制的小时。一位数的小时数没有前导零。
H 24小时制的小时。一位数的小时数没有前导零。
m 分钟。一位数的分钟数没有前导零。
s 秒。一位数的秒数没有前导零。

             parseNumber标签


                        将指定格式的数值字符串转换成数值型
                                    语法格式:
                                        <ftm:parseNumber
                                                 value="<string>"
                                                 type="<string>"
                                                 var="<string>"
                                                 scope="<string>" />


            parseDate标签


                        将日期型的字符串转换成Date型
                                    语法格式:
                                        <ftm:formatDate
                                                value="<string>"
                                                type="<string>"
                                                dateStyle="<string>"
                                                timeStyle="<string>"
                                                pattern="<string>"
                                                var="<string>"
                                                scope="<string>"/>

<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--引入格式化标签库--%>
<%@taglib prefix="ftm" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>格式化动作标签</title>
</head>
<body>
    

    <ftm:formatNumber value="10" type="number" var="num"/>${num}<br>
    <ftm:formatNumber value="10" type="percent"/><br>
    <ftm:formatNumber value="10" type="currency"/><br>
    <%--设置时区--%>
    <ftm:setLocale value="en_US"/>
    <ftm:formatNumber value="10" type="currency"/><br>
    <hr>




    <%--格式化日期--%>
    <%
        request.setAttribute("myDate",new Date());
    %>
    ${myDate}
    <ftm:formatDate value="${myDate}"/><br>
    <ftm:formatDate value="${myDate}" type="date"/><br>
    <ftm:formatDate value="${myDate}" type="time"/><br>
    <ftm:formatDate value="${myDate}" type="both"/><br>
    <ftm:formatDate value="${myDate}" type="both" dateStyle="FULL"/><br>
    <ftm:formatDate value="${myDate}" type="both" timeZone="SHORT"/><br>
    <ftm:formatDate value="${myDate}" pattern="yyyy-MM-dd"/><br>
    <hr>


    <ftm:parseNumber value="100"/><br>
    <ftm:parseNumber value="100" type="number"/><br>
    <ftm:parseNumber value="100%" type="percent"/><br>
    <%--设置时区--%>
    <ftm:setLocale value="zh_CN"/>
    <ftm:parseNumber value="¥10.00" type="currency"/><br>
    <hr>


    <ftm:parseDate value="2021-07-27" type="date"/><br>
    <ftm:parseDate value="2021/07/27" pattern="yyyy/MM/dd"/><br>


</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_45859844/article/details/119208718