JSTL标签

JSTL标签

 

引用核心标签库的语法如下:

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

标签 描述

<c:out> 用于在JSP中显示数据,就像<%= ... >

<c:set> 用于保存数据

<c:remove> 用于删除数据

<c:if> 与我们在一般程序中用的if一样

<c:choose> 本身只当做<c:when><c:otherwise>的父标签

<c:when>   <c:choose>的子标签,用来判断条件是否成立

<c:otherwise>  <c:choose>的子标签,接在<c:when>标签后,当<c:when>标签判断为false时被执行

<c:forEach> 基础迭代标签,接受多种集合类型

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
    <!-- taglib指令引入一个自定义标签集合的定义,包括库路径、自定义标签。
    uri属性确定标签库的位置,prefix属性指定标签库的前缀。 -->
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%-- 

1/在页面开头加上taglib指令
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

2/jstl.jar放入web-inf的lib目录,添加至环境路径里


c:out-->用于内容的输出,等同与out.println("")
属性:
    value:放入输出到网页的内容
    default:放入默认的内容,value值如果没有获取得到,那么就可以设置默认值进行显示。
    --%>
    
    
    <c:out value="${abc }" default="默认值:no abc"></c:out>
    
    
    
    <%-- 
c:set-->用于设置数据
属性:
    var:等同于request.setAttribute(key, value);里面的key值
    value:等同于request.setAttribute(key, value);里面的value值
    scope:在哪个隐式对象上,contextPage,request,session
    --%>
    
    
    <c:set var="name" value="杨超越" scope="request"></c:set>
    <c:set var="age" value="20" scope="request"></c:set>
    <c:out value="${name }" default="默认值:no abc"></c:out>
    <c:out value="${age }" default="默认值:no abc"></c:out>
    
    
    <%--
c:remove-->用于删除数据
属性:
    var:request.removeAttribute(key)等同于key
    scope:request.removeAttribute(key)等同于request,设置在哪个隐式对象上删除
    --%>
    
    <c:remove var="name" scope="request"></c:remove>
    <%--<c:remove var="age" scope="request"></c:remove>--%>
    <c:out value="${age }" default="默认值:no abc"></c:out>
    
    
    <%--
c:if-->根据条件判断是否显示内容
属性:
    test:里面一般跟上${}el表达式
--%>


<c:if test="${age>18}"> <h1>恭喜成为大人</h1>  </c:if>


    <%--
    c:choose-->类似与if...else...,具有c:when和c:otherwise的子元素
c:when-->当什么条件下可以执行
    属性:test-->等同于c:if
    --%>
    <%--
c:otherwise-->在其余条件都不满足的情况下执行
--%>

<c:choose>
<c:when test="${age<18 }"><h1>还未成年,很多电影看不了</h1></c:when>
<c:when test="${age<28 }">    <h1>青年啦,可以谈恋爱啦</h1></c:when>
<c:when test="${age<38 }"><h1>男人还是一枝花</h1></c:when>
<c:otherwise><h1>慢慢养老</h1></c:otherwise>
</c:choose>

    <%--
c:foreach
    属性:
    begin:从哪个数字开始
    end:从哪个数字结束
    varStatus:设置1个变量名称,里面具有每一次循环的相对应的信息
        index: 这是循环的索引值
         count: 当前这次迭代从 1 开始的迭代计数
         first: 用来表明当前这轮迭代是否为第一次迭代的标志
         last: 用来表明当前这轮迭代是否为最后一次迭代的标志
     step:步数,每次循环,数字变量加多少步数
     
     items:设置需要循环的对象
     var:每次循环的对象
     
    
--%>
<c:forEach begin="5" end="50" varStatus="i" step="5">
<h1>${i.count}nihao${i.index }--->${i.first }---->${i.last }</h1>
</c:forEach>
<c:forEach items="${newList}" var="item" varStatus="i">
    <h1>${i.count}新闻列表:${item }</h1>
    
</c:forEach>

</body>
</html>

package cn.sxt;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



/**
 * Servlet implementation class lianxi2
 */
@WebServlet("/lianxi2")
public class lianxi2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public lianxi2() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("北师大:珠海分校停办后将设相应机构保障毕业生权益");
        arrayList.add("恒大客场2比0击败上港,三名受伤国脚全部复出上场");
        arrayList.add("陆克文:特朗普为中国送上一手贸易战好牌");
        request.setAttribute("newList", arrayList);
    
        request.getRequestDispatcher("File2.jsp").forward(request, response);
    }
    

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}


 
原文地址:https://www.cnblogs.com/406070989senlin/p/11028747.html