JSP标签、JSTL标签、EL表达式

1、EL表达式


        <dependency>
            <groupId>javax.servlet.jsp.jstlgroupId>
            <artifactId>jstl-apiartifactId>
            <version>1.2version>
        dependency>

        
        <dependency>
            <groupId>taglibsgroupId>
            <artifactId>standardartifactId>
            <version>1.1.2version>
        dependency>

EL表达式:${}

  • 获取数据
  • 执行运算
  • 获取web开发的常用对象

jsp标签

<%--<jsp:include page="" />--%>

<jsp:forward page="/jsptag2.jsp">
    <jsp:param name="name" value="rygar"/>
    <jsp:param name="age" value="18"/>
</jsp:forward>

JSTL表达式
JSTL标签库的使用就是为了弥补HTML标签的不足;它自定义许多标签,可以供我们使用,标签的功能和java代码一样!

<%--取出参数--%>
名字:<%=request.getParameter("name")%>
年龄:<%=request.getParameter("age")%>

核心标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
JSP标签、JSTL标签、EL表达式_第1张图片
JSTL标签库使用步骤

  • 引入对应的taglib
  • 使用其中的方法
    在使用tomcat10版本会遇到引入jstl一直报错问题;需要导入依赖如下:
<dependencies>
       
        
        <dependency>
            <groupId>jakarta.servletgroupId>
            <artifactId>jakarta.servlet-apiartifactId>
            <version>5.0.0version>
        dependency>

        <dependency>
            <groupId>taglibsgroupId>
            <artifactId>standardartifactId>
            <version>1.1.2version>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>4.0.1version>
            <scope>providedscope>
        dependency>

        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.2version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>

        <dependency>
            <groupId>org.glassfish.webgroupId>
            <artifactId>jakarta.servlet.jsp.jstlartifactId>
            <version>2.0.0version>
        dependency>

        <dependency>
            <groupId>org.apache.taglibsgroupId>
            <artifactId>taglibs-standard-specartifactId>
            <version>1.2.5version>
        dependency>

        <dependency>
            <groupId>org.apache.taglibsgroupId>
            <artifactId>taglibs-standard-implartifactId>
            <version>1.2.5version>
        dependency>
    dependencies>

具体解决方案参考tomcat10+JSTL
创建coreif.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--引入JSTL核心标签库,我们才能使用JSTL标签 core--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>


<html>
<head>
    <title>Title</title>
</head>
<body>

<h4>if测试</h4>

<hr>

<form action="coreif.jsp" method="get">
<%--
    EL表达式获取表单中的数据
    ${
     param.参数名}
--%>
    <input type="text" name="username" value="${param.username}">
    <input  type="submit" value="登录">

</form>

<%--判断如果提交的用户名是管理员,则登录成功--%>
<c:if test="${param.username='admin'}" var="isAdmin" >
    <c:out value="管理员欢迎您!" />
</c:if>

<c:out value="${isAdmin}"/>


</body>
</html>

创建corewhen.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%--定义一个变量score,值为85--%>
<c:set var="score" value="85" />

<c:choose>
    <c:when test="${score>=90}">你的成绩优秀!</c:when>
    <c:when test="${score>=80}">你的成绩良好!</c:when>
    <c:when test="${score>=70}">你的成绩一般!</c:when>
    <c:when test="${score>=60}">及格!</c:when>
    <c:when test="${score<=60}">不及格!</c:when>
</c:choose>



</body>
</html>

创建coreforeach.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%
    ArrayList<String> people = new ArrayList<>();
    people.add(0,"张三");
    people.add(1,"李四");
    people.add(2,"王五");
    people.add(3,"赵六");
    people.add(4,"田七");
    request.setAttribute("list",people);
%>

<%--
        var,声明变量,每一次遍历出来的变量
        items,要遍历的对象
--%>
<c:forEach var="people" items="${list}">
    <c:out value="${people}" />
    <%--<br>换行--%>
    <br>
</c:forEach>

<hr>

<c:forEach var="people" items="${list}" begin="1" end="3" step="2">
    <c:out value="${people}" />
</c:forEach>

</body>
</html>

你可能感兴趣的:(jsp,jstl)