jstl标签库有哪些标签?常用标签介绍

KLQ 2020-04-22 16:01:26 java常见问答 8882

jstl大家都熟悉吗?下面要给大家介绍的就是jstl标签库的相关内容,那么jstl常用标签有哪些呢?一起来了解一下吧。

首先给大家介绍一下jstl

jstl(JSP Standard Tag Library),JSP标准标签库,能够嵌入在jsp页面中使用标签的形式完成业务逻辑等功能。

它出现的目的和el相同,要提到jsp页面中的脚本代码。

JSTL标准标签库有5个子库,可是,随着目前的发展,经常用到的是它的核心库。

JSTL核心库的常用标签

1、<c:if test=" ">标签

test是返回boolean的条件

*引入标签库:<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
            <title>Insert title here</title>
        </head>
        <body><%
request.setAttribute("count",11);
%>
            <!-- test代表的返回 -->
            <c:if test="${count==10}">
xxx
</c:if>
            <c:if test="count!=10">
yyyy
</c:if>
        </body>
    </html>

用法:

将当前用户放到session域,在访问其他资源的时候,能够从session域中方便取到用户,看是不是登陆了

(在没有登录的时候,首页显示登录注册,登录之后显示用户名和退出)

jstl标签库有哪些标签

2、<c:forEach>标签

有2种使用方式

(1)第一种方式

jstl标签库有哪些标签

(2)第二种方式

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%><%@ page import="domain.*"%><%@ page import="java.util.*"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
            <title>Insert title here</title>
        </head>
        <body><%
        //遍历List
            <String> strList
        List
                <String> strList = new ArrayList
                    <String>();
        strList.add("itcast");
        strList.add("itheima");
        strList.add("ithe");
        request.setAttribute("strList", strList);

        //遍历List
                        <User>的值
        List
                            <User> userList = new ArrayList
                                <User>();
        User user2 = new User();
        user2.setId("2");
        user2.setName("lisi");
        user2.setPassword("123");
        userList.add(user2);
        User user3 = new User();
        user3.setId("3");
        user3.setName("wangwu");
        user3.setPassword("123");
        userList.add(user3);
        application.setAttribute("userList", userList);

        //遍历Map
                                    <String,String>的值
        Map
                                        <String, String> strMap = new HashMap
                                            <String, String>();
        strMap.put("name", "lucy");
        strMap.put("age", "18");
        strMap.put("addr", "西三");
        strMap.put("email", "licy@111.com");
        session.setAttribute("strMap", strMap);

        //遍历Map
                                                <String,User>的值
        Map
                                                    <String, User> userMap = new HashMap
                                                        <String, User>();
        userMap.put("user2", user2);
        userMap.put("user3", user3);
        request.setAttribute("userMap", userMap);
    %>
                                                            <!-- List<String> strList -->
                                                            <c:forEach items="${strList }" var="str">
    ${str }
    </c:forEach>
                                                            <br />
                                                            <br />
                                                            <!--     List<User> -->
                                                            <c:forEach items="${userList }" var="user">
    ${user.name} : ${user.password }
    
                                                                <br />
                                                            </c:forEach>
                                                            <br />
                                                            <br />
                                                            <!-- Map<String,String> -->
                                                            <c:forEach items="${strMap }" var="entry">
    ${entry.key } : ${entry.value } 
    
                                                                <br />
                                                            </c:forEach>
                                                            <br />
                                                            <br />
                                                            <!-- Map<String,User> -->
                                                            <!-- entry.key对应 String;entry.value对应User-->
                                                            <c:forEach items="${userMap }" var="entry">
     ${entry.key }: ${entry.value.name }-- ${entry.value.password}
    </c:forEach>
                                                        </body>
                                                    </html>

最终的结果:

jstl标签库有哪些标签

jstl标签库相关的内容大家都了解了吗?更多jstl相关内容,可以继续关注本站的常见问题专栏了解哦!