• EL&JSTL:JSTL总结



    目录:

    (1)set标签使用1

    (2)set标签和EL的表达式联合使用

    (3)if标签

    (4)choose标签 相当于多个判断语句

    (5)forEach循环

     (6)forEach遍历List集合

     (7)forEach遍历Map集合


     

    JSTL:披着标签的java工具类,java命令变成标签的形式,来满足需求,不让java命令落在jsp上

    为了jsp页面中看不见java代码,替换原来在jsp页面中的<% java代码%>,一个标签相当于一个java命令

    产生你原因:

    Java命令不能落在jsp上面去,所以把java命令披上一个标签,封装成一个标签使用

    另一个是EL表达式功能过于弱小

     

     

    创建04Model导入响应的jar包:

     

     

    (1)set标签使用1

     index.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/20
    5. Time: 7:11
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%--需要加jstl的标签--%>
    10. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    11. set scope="application" var="sid" value="10">set>
    12. set scope="session" var="sname" value="mike">set>
    13. set scope="request" var="sage" value="26">set>
    14. set scope="page" var="home" value="上海">set>
    15. 学员编号:${applicationScope.sid}
    16. 学员姓名:${sessionScope.sname}
    17. 学员年龄:${requestScope.sage}
    18. 学员籍贯:${pageScope.home}

    (2)set标签和EL的表达式联合使用

     index_2.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/20
    5. Time: 8:19
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%--需要加jstl的标签--%>
    10. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    11. <%--设置用户今年的年龄--%>
    12. set scope="request" var="age" value="20">set>
    13. <%--设置用户两年之后的年龄
    14. 执行顺序,限制性EL表达式,再执行JSTL,EL表达式先从request客户端读取数据+2
    15. 然后并没有写到响应体中,而是写到value属性中,然后再执行set标签,
    16. 把request作用域中的20更新为22
    17. --%>
    18. set scope="request" var="age" value="${requestScope.age+2}">set>
    19. 两年之后的年龄:${age}

    (3)if标签

      

    index_3.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/20
    5. Time: 8:19
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%--需要加jstl的标签--%>
    10. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    11. <c:set scope="request" var="age" value="23">c:set>
    12. <c:if test="${sessionScope.age ge 18}">
    13. <font color="red">欢迎光临font>
    14. c:if>
    15. <c:if test="${sessionScope.age lt 18}">
    16. <font color="red">过两年再来font>
    17. c:if>

    在运行项目的时候报了500:

     

    JSTL标签库报500错误解决方案

    原因分析
    Tomcat缺少jstl-api-1.2.jar和standard-1.1.2.jar和这两个jar包。

    解决方案
    将jstl-api-1.2.jar和standard-1.1.2.jar这两个包放入到D:\Program\apache-tomcat-8.0.53\lib目录下

    加入之后正常运行: 

     

    (4)choose标签 相当于多个判断语句

    index_4.jsp:

     

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/20
    5. Time: 9:18
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%--需要加jstl的标签--%>
    10. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    11. <c:set scope="page" var="salary" value="15000"></c:set>
    12. <c:choose>
    13. <c:when test="${salary ge 50000}">高工资</c:when>
    14. <c:when test="${salary ge 20000}">正常工资</c:when>
    15. <c:when test="${salary ge 10000}">低工资</c:when>
    16. <c:otherwise>不正常工资</c:otherwise>
    17. </c:choose>

     

     (5)forEach循环

     index_5.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/20
    5. Time: 9:42
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%--需要加jstl的标签--%>
    10. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    11. <select>
    12. <%
    13. for (int i=1;i<=5;i++){
    14. %>
    15. <option><%=i%>option>
    16. <%
    17. }
    18. %>
    19. select>
    20. <hr>
    21. JSTL语法替换上方的java语句<br>
    22. <select>
    23. <c:forEach var="i" begin="1" end="5" step="1">
    24. <option>第${pageScope.i}页option>
    25. c:forEach>
    26. select>

     

     (6)forEach遍历List集合

     

     Student类:

    1. package com.bjpowernode.model;
    2. public class Student {
    3. private Integer sid;
    4. private String sname;
    5. public Student(Integer sid, String sname) {
    6. this.sid = sid;
    7. this.sname = sname;
    8. }
    9. public Integer getSid() {
    10. return sid;
    11. }
    12. public void setSid(Integer sid) {
    13. this.sid = sid;
    14. }
    15. public String getSname() {
    16. return sname;
    17. }
    18. public void setSname(String sname) {
    19. this.sname = sname;
    20. }
    21. }

    创建Servlet:SixServlet:

    1. package com.bjpowernode.controller;
    2. import com.bjpowernode.model.Student;
    3. import javax.servlet.ServletException;
    4. import javax.servlet.http.HttpServlet;
    5. import javax.servlet.http.HttpServletRequest;
    6. import javax.servlet.http.HttpServletResponse;
    7. import java.io.IOException;
    8. import java.util.ArrayList;
    9. import java.util.List;
    10. public class SixServlet extends HttpServlet {
    11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    12. }
    13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    14. Student stu=new Student(10,"mike");
    15. Student stu2=new Student(20,"allen");
    16. List list=new ArrayList();
    17. list.add(stu);
    18. list.add(stu2);
    19. request.setAttribute("key",list);
    20. request.getRequestDispatcher("/index_6.jsp").forward(request,response);
    21. }
    22. }

     在web.xml:中指定访问方式:

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    4. version="4.0">
    5. <servlet>
    6. <servlet-name>SixServletservlet-name>
    7. <servlet-class>com.bjpowernode.controller.SixServletservlet-class>
    8. servlet>
    9. <servlet-mapping>
    10. <servlet-name>SixServletservlet-name>
    11. <url-pattern>/six.dourl-pattern>
    12. servlet-mapping>
    13. web-app>

    index_6.jsp:

    1. <%@ page import="java.util.List" %>
    2. <%@ page import="com.bjpowernode.model.Student" %><%--
    3. Created by IntelliJ IDEA.
    4. User: DELL
    5. Date: 2022/7/20
    6. Time: 10:11
    7. To change this template use File | Settings | File Templates.
    8. --%>
    9. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    10. <%
    11. String path = request.getContextPath();
    12. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    13. %>
    14. <%--需要加jstl的标签--%>
    15. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    16. <html>
    17. <head>
    18. <title>Titletitle>
    19. <base href="<%=basePath%>">
    20. head>
    21. <body>
    22. java命令获取数据<br>
    23. <table border="2">
    24. <tr>
    25. <td>学员编号td>
    26. <td>学员姓名td>
    27. tr>
    28. <%
    29. List stuList=(List)request.getAttribute("key");
    30. for(Student stu:stuList){
    31. %>
    32. <tr>
    33. <td><%=stu.getSid()%>td>
    34. <td><%=stu.getSname()%>td>
    35. tr>
    36. <%
    37. }
    38. %>
    39. table>
    40. <hr>
    41. 通过JSTL标签来获取数据显示<br>
    42. <table border="2">
    43. <tr>
    44. <td>学员编号td>
    45. <td>学员姓名td>
    46. tr>
    47. <c:forEach items="${requestScope.key}" var="stu">
    48. <tr>
    49. <td>${stu.sid}td>
    50. <td>${stu.sname}td>
    51. tr>
    52. c:forEach>
    53. table>
    54. body>
    55. html>

     

     (7)forEach遍历Map集合

    控制器:SixServlet:

    1. package com.bjpowernode.controller;
    2. import com.bjpowernode.model.Student;
    3. import javax.servlet.ServletException;
    4. import javax.servlet.http.HttpServlet;
    5. import javax.servlet.http.HttpServletRequest;
    6. import javax.servlet.http.HttpServletResponse;
    7. import java.io.IOException;
    8. import java.util.ArrayList;
    9. import java.util.HashMap;
    10. import java.util.List;
    11. import java.util.Map;
    12. public class SixServlet extends HttpServlet {
    13. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    14. }
    15. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    16. Student stu=new Student(10,"mike");
    17. Student stu2=new Student(20,"allen");
    18. //List集合
    19. List list=new ArrayList();
    20. list.add(stu);
    21. list.add(stu2);
    22. //Map集合
    23. Map map=new HashMap();
    24. map.put("一班",stu);
    25. map.put("二班",stu2);
    26. request.setAttribute("mapKey",map);
    27. request.setAttribute("key",list);
    28. request.getRequestDispatcher("/index_6.jsp").forward(request,response);
    29. }
    30. }

    index_6.jsp:

    1. <%@ page import="java.util.List" %>
    2. <%@ page import="com.bjpowernode.model.Student" %><%--
    3. Created by IntelliJ IDEA.
    4. User: DELL
    5. Date: 2022/7/20
    6. Time: 10:11
    7. To change this template use File | Settings | File Templates.
    8. --%>
    9. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    10. <%
    11. String path = request.getContextPath();
    12. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    13. %>
    14. <%--需要加jstl的标签--%>
    15. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    16. <html>
    17. <head>
    18. <title>Titletitle>
    19. <base href="<%=basePath%>">
    20. head>
    21. <body>
    22. java命令获取数据<br>
    23. <table border="2">
    24. <tr>
    25. <td>学员编号td>
    26. <td>学员姓名td>
    27. tr>
    28. <%
    29. List stuList=(List)request.getAttribute("key");
    30. for(Student stu:stuList){
    31. %>
    32. <tr>
    33. <td><%=stu.getSid()%>td>
    34. <td><%=stu.getSname()%>td>
    35. tr>
    36. <%
    37. }
    38. %>
    39. table>
    40. <hr>
    41. 通过JSTL标签来获取数据显示<br>
    42. <table border="2">
    43. <tr>
    44. <td>学员编号td>
    45. <td>学员姓名td>
    46. tr>
    47. <c:forEach items="${requestScope.key}" var="stu">
    48. <tr>
    49. <td>${stu.sid}td>
    50. <td>${stu.sname}td>
    51. tr>
    52. c:forEach>
    53. table>
    54. <hr>
    55. <h1>遍历map集合h1>
    56. <table border="2">
    57. <tr>
    58. <td>班级名称td>
    59. <td>学员编号td>
    60. <td>学员名称td>
    61. tr>
    62. <%--遍历Map集合每次从Map集合中得到的是一个【键值对】
    63. 再把【键值对】交给循环变量
    64. 循环变量.key获得【键值对】中关键字名字 班级名称
    65. 循环变量.value 获得【键值对】中的内容 stu对象
    66. --%>
    67. <c:forEach items="${mapKey}" var="key_Value">
    68. <tr>
    69. <td>${key_Value.key}td>
    70. <td>${key_Value.value.sid}td>
    71. <td>${key_Value.value.sname}td>
    72. tr>
    73. c:forEach>
    74. table>
    75. body>
    76. html>

  • 相关阅读:
    鹏城AI靶场助力大规模高质量中文语料数据集安全开放
    Win11+RTX3060+Anconda+CUDA11.3+cuDNN8.2+Pytorch1.8一条龙服务
    浅谈Oracle数据库调优(1)
    项目开始前要明确的几个注意事项
    【Docker】搭建MySQL主从复制,详细的图文展示
    实用的窗口管理软件:Display Maid for Mac
    基于vue.js的招聘系统
    chatgpt赋能python:Python文件名字替换-优化SEO的必备技巧
    Java dom4j生成XML文件的方法分享
    CentOS 7.9 安装 nginx
  • 原文地址:https://blog.csdn.net/dengfengling999/article/details/125884427