JavaWeb_JSTL标签数据的存储

  菜鸟教程  传送门

  JSTL  jar包下载

  JSTL【百度百科】:(JavaServer Pages Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库

JSP页面使用JSTL标签

  (一)引入JSTL的jar包

  (二)引入JSTL的标签库

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

  

使用JSTL标签做简单数据存储

<%@ 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("username","Gary"); %> --%>
<!-- 常用作数据显示 -->
<!--使用jstl 给"username"设"Gary"值        作用域request -->
<c:set var="username" value="Gary" scope="request"></c:set>

<!-- 使用jstl 输出值 -->
<c:out value="${username}" ></c:out>
<c:out value="123" ></c:out>

<!-- 移除username的值 -->
<c:remove var="username"/>
<!-- 测试username中的值 -->
<c:out value="${username}" ></c:out>

</body>
</html>
jstl.jsp

输出

Gary 123 

JSTL中条件标签和循环标签

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!-- 第二步:引入标签库 -->
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="com.Gary.model.User"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<c:set var="age" value="20" scope="request"></c:set>
<c:if test="${age>=18}">
成年人<br>
</c:if>

<!-- JSTL中if else语句 -->
<c:choose>
    <c:when test="${age>=18}">
    这是成年人        <br>
    </c:when>
<c:otherwise>
    这是未成年人    <br>
</c:otherwise>
</c:choose>

<!-- for(int i=1;i<=10;i++) -->
<c:forEach var="i" begin="1" end="10">
这是第${i}次<br>
</c:forEach>

<%
List<User> list = new ArrayList<User>();
list.add(new User("Gary0","0",10,"",false));
list.add(new User("Gary1","1",20,"",false));
list.add(new User("Gary2","2",30,"",false));
list.add(new User("Gary3","3",40,"",false));
list.add(new User("Gary4","4",50,"",false));
request.setAttribute("list", list);
%>
<!-- 循环标签通过el表达式取出数据 -->
<c:forEach items="${list}" var="u">
    ${u.username}<br>
</c:forEach>









</body>
</html>
jstl.jsp
package com.Gary.model;

public class User {
    private String username;
    private String password;
    private int age;
    private String sex;
    private boolean isAdmin=false;    //判断是否是管理员账号
    
    public User(String username, String password, int age, String sex, boolean isAdmin) {
        super();
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
        this.isAdmin = isAdmin;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public boolean isAdmin() {
        return isAdmin;
    }
    public void setAdmin(boolean isAdmin) {
        this.isAdmin = isAdmin;
    }
    
}
User.java

输出

成年人
 这是成年人 
 这是第1次
 这是第2次
 这是第3次
 这是第4次
 这是第5次
 这是第6次
 这是第7次
 这是第8次
 这是第9次
 这是第10次
 Gary0
 Gary1
 Gary2
 Gary3
 Gary4

  JSTL的if else语句

<c:choose>
    <c:when test="${}">
    
    </c:when>
<c:otherwise>
    
</c:otherwise>
</c:choose>

  JSTL循环取出复杂数据时需要通过el表达式

<%
List<User> list = new ArrayList<User>();
list.add(new User("Gary0","0",10,"",false));
list.add(new User("Gary1","1",20,"",false));
list.add(new User("Gary2","2",30,"",false));
list.add(new User("Gary3","3",40,"",false));
list.add(new User("Gary4","4",50,"",false));
request.setAttribute("list", list);
%>
<!-- 循环标签通过el表达式取出数据 -->
<c:forEach items="${list}" var="u">
    ${u.username}<br>
</c:forEach>
(如需转载学习,请标明出处)
原文地址:https://www.cnblogs.com/1138720556Gary/p/10311404.html