EL表达式 和JSTL标签

EL表达式

Expression  Language 表达式语言:用于简化JSP界面内的java代码

主要功能:获取数据

语法:${experssion}

里面写key

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    //自醒机制
    pageContext.setAttribute("msg","001");
    request.setAttribute("msg","002");
    session.setAttribute("msg","003");
    application.setAttribute("msg","004");
%>
<h1>${msg}</h1>
<h1>${sessionScope.msg}</h1>
</body>
</html>

JavaWeb中的四大域对象:

1:page :当前页面有效

2 request: 当前请求有效

3 session:当前会话有效

4  application: 当前应用有效

EL表达式获取数据,会依次从这四个域中寻找,直到找到为止

创建Servlet类

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 java.io.IOException;

@WebServlet("/dd1")
public class Servlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //模拟数据
        String name="牛马";
        req.setAttribute("info",name);
        req.getRequestDispatcher("/el1.jsp").forward(req,resp);

    }
}

编辑jsp

<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2022/9/8
  Time: 11:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<%--上面哪行代码是让浏览器能够识别el表达式--%>
<html>
<head>
    <title>Title</title>
</head>
<body>
${info}

</body>
</html>

如果你的El表达式原样输出了,就要在jsp文件中加上

<%@ page isELIgnored="false"%>

JSTL标签

 JSP标准标签库(JSP  Standarded Tag  Library) 使用标签取代JSP页面的java代码

使用步骤

导入坐标

    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>

jsp文件中设置

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

c:if标签

创建Servlet

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/a1")
public class Servlet02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //存数据到域
         Integer sex=1;
         request.setAttribute("sex",sex);
         request.getRequestDispatcher("/jstl1.jsp").forward(request,response);
    }
    
}

创建jsp

<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2022/9/8
  Time: 14:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false"%>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%--<c:if test="true">--%>
<%--    true--%>
<%--</c:if>--%>
<%--没有else,写一个逻辑相反就相当于else--%>

<%--<c:if test="false">--%>
<%--    false--%>
<%--</c:if>--%>

${sex}
<%--通过el表达式传值做判断--%>
<c:if test="${sex==1}">
    male
</c:if>

<c:if test="${sex==0}">
    female
</c:if>

</body>
</html>

c:foreach标签 

增强for

Servlet创建

import com.chen.pojo.Student;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/a3")
public class Servlet03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ArrayList<Student>  lists=new ArrayList<>();
        lists.add(new Student("张三",20,1));
        lists.add(new Student("李四",21,0));   //一会判断sex  1为男 ,0为女
        lists.add(new Student("王五",22,1));
        //存数据
        request.setAttribute("stu",lists);
        request.getRequestDispatcher("/foreach.jsp").forward(request,response);
    }
    
}

Jsp文件创建

<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2022/9/8
  Time: 15:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<c:forEach items="${stu}" var="stu1" varStatus="status">
<%--    status.id   需要从零开始--%>
<%--    status.count 序号从1开始--%>
    ${status.count}
    ${stu1.name}
    ${stu1.age}
    <c:if test="${stu1.sex==1}">
        男
    </c:if>
    <c:if test="${stu1.sex==0}">
        女
    </c:if>
    <br>

</c:forEach>

</body>
</html>

普通for循环

<c:forEach begin="0" end="10" step="1" var="i">
<%--    begin:开始   end :结束   step:步长    var:变量--%>
    ${i} <br>

</c:forEach>

 

 forTokens

<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 11/14/22
  Time: 04:43 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<c:forEach items="${msg}" var="i" end="2">
    <h1>${i}</h1>
</c:forEach>
<%
    ArrayList<String> list=new ArrayList<String>();
    list.add("111");
    list.add("222");
    list.add("333");
    list.add("4444");
    session.setAttribute("msg",list);
%>
<%--以逗号为分割,输出字符,var是临时变量,items是要遍历的集合--%>
<c:forTokens items="abc,defg" delims="," var="i">
    <h1>${i}</h1>
</c:forTokens>

</body>
</html>

<c:choose>   相当于Switch

<c:when> 相当于case

<c:otherwise> 相当于default


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<c:set var="a" value="5" scope="request"></c:set>
<c:set var="b" value="2" scope="request"></c:set>

<c:if test="${a>b}"><div style="color: green">a大</div></c:if>
<c:if test="${a<b}"><div style="color: blue">b大</div></c:if>
<hr>
<c:choose>
    <c:when test="${a==1}"><div style="color: red">1</div></c:when>
    <c:when test="${a==2}"><div style="color: yellow">2</div></c:when>
    <c:when test="${a==3}"><div style="color: blue">3</div></c:when>
    <c:otherwise><div style="color:pink; font-size: 100px">啥也不是</div></c:otherwise>
</c:choose>
</body>
</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小萌新上大分

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值